🔗 Clase Link — generador de URLs

Actualizado: 2024-12-01

La clase Link de PrestaShop genera todas las URLs de la tienda teniendo en cuenta la configuracion de URLs amigables (rewrite), HTTPS, multitienda y el idioma activo. Nunca construyas URLs manualmente — usa siempre Link.

#Obtener la instancia de Link

Acceder al objeto Link
php
<?php

// Desde el Context
$link = Context::getContext()->link;

// Desde dentro de un modulo
$link = $this->context->link;

// Crear nueva instancia (raro, normalmente usa el del Context)
$link = new Link();

#URLs de contenido

URLs de productos, categorias, CMS y paginas estandar
php
<?php

$link = Context::getContext()->link;

// ── PRODUCTOS ──
// Por objeto o ID
$url = $link->getProductLink($product);          // objeto Product
$url = $link->getProductLink(42);                // ID
$url = $link->getProductLink(42, null, null, null, (int)Context::getContext()->language->id);
// Con combinacion
$url = $link->getProductLink(42, 'mi-producto', 'es', 3);  // id, rewrite, lang_iso, id_attribute

// ── CATEGORIAS ──
$url = $link->getCategoryLink(2);                // Home category
$url = $link->getCategoryLink($category);        // objeto Category

// ── CMS ──
$url = $link->getCMSLink(1);                     // ID pagina CMS
$url = $link->getCMSCategoryLink(1);             // Categoria CMS

// ── PAGINAS ESTANDAR ──
$url = $link->getPageLink('cart');               // /carrito (o ?controller=cart)
$url = $link->getPageLink('order');              // /proceso-compra
$url = $link->getPageLink('authentication');     // /login
$url = $link->getPageLink('my-account');         // /mi-cuenta
$url = $link->getPageLink('contact');            // /contacto
$url = $link->getPageLink('sitemap');            // /mapa-del-sitio

// Con parametros GET
$url = $link->getPageLink('order', true, null, 'step=1');

// ── FABRICANTES Y PROVEEDORES ──
$url = $link->getManufacturerLink(1);
$url = $link->getSupplierLink(1);

// ── BUSCADOR ──
$url = $link->getSearchLink();

#URLs de modulos y admin

URLs de FrontControllers de modulos y del BO
php
<?php

$link = Context::getContext()->link;

// ── MODULO FRONT CONTROLLER ──
$url = $link->getModuleLink('mymodule', 'mypage');
// Con parametros
$url = $link->getModuleLink('mymodule', 'mypage', ['id' => 5, 'action' => 'view']);
// Forzar SSL
$url = $link->getModuleLink('mymodule', 'mypage', [], true);
// Idioma especifico
$url = $link->getModuleLink('mymodule', 'mypage', [], null, 2);  // id_lang = 2

// ── BACK OFFICE (ADMIN) ──
$adminUrl = $link->getAdminLink('AdminOrders');
$adminUrl = $link->getAdminLink('AdminProducts', true);          // con token
$adminUrl = $link->getAdminLink('AdminOrders', true, [], ['id_order' => 5]);

// ── URL BASE DE LA TIENDA ──
$baseUrl  = $link->getBaseLink();
$baseUrl  = $link->getBaseLink(null, null, true);  // forzar HTTPS

#URLs de imagenes

URLs de imagenes de productos, categorias y logos
php
<?php

$link = Context::getContext()->link;

// ── IMAGENES DE PRODUCTO ──
// $rewrite = URL amigable del producto
// $ids     = cadena de IDs de imagen (p.ej. '1-2-3')
$imgUrl = $link->getImageLink('mi-producto', '1-large_default');
$imgUrl = $link->getImageLink('mi-producto', '1-medium_default');
$imgUrl = $link->getImageLink('mi-producto', '1-home_default');
$imgUrl = $link->getImageLink('mi-producto', '1-small_default');

// Imagen de categoria
$catImgUrl = $link->getCatImageLink('electronics', 3, 'category_default');

// Logo de la tienda
$logoUrl = _PS_IMG_ . Configuration::get('PS_LOGO');

// ── TIPOS DE IMAGEN TIPICOS ──
// large_default  = 800x800
// medium_default = 452x452
// home_default   = 250x250
// small_default  = 98x98
// cart_default   = 80x80
// thumbnail      = 35x35 (admin)

#Metodos utiles adicionales

Otros metodos de Link frecuentemente usados
php
<?php

$link = Context::getContext()->link;

// Verificar si el protocolo activo es HTTPS
$isSSL = $link->isProtectedURL();

// Obtener URL amigable de un producto sin objeto completo
$rewriteUrl = Product::getProductLink(
    42,           // id_product
    'es',         // iso_code del idioma
    2             // id_lang
);

// URL de la pagina actual con parametros extras
$currentUrl = Tools::getCurrentUrlProtocolPrefix() . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

// En Smarty, el objeto $link esta disponible globalmente:
// {$link->getProductLink($product.id_product)}
// {$link->getModuleLink('mymodule','page')}
// {$link->getPageLink('cart')}
Descargar en Markdown Pensado para pegar en ChatGPT, Claude u otra IA. Incluye solo el contenido de esta pagina.