Reading list Switch to dark mode

    Generate Symfony route in PrestaShop module

    Updated 27 June 2023

    In this blog, we are going to learn how to generate Symfony route in PrestaShop module from the legacy controller.

    When your application receives a request, it calls a controller action to generate the response. The routing configuration defines which action to run for each incoming URL. It also provides other useful features, like generating SEO-friendly URLs.

    When the module is using legacy controllers, the legacy controllers do not have access to the Symfony container or router. So in PrestaShop Link object has helpers to help generate urls related to Symfony controllers and routes.

    1. Using router via Link::getUrlSmarty (Available from PrestaShop 1.7.0)

    <?php
    
    use Link; // at the top of PHP file
    
    // Generate url with Symfony route
    $symfonyUrl = Link::getUrlSmarty([
        'entity' => 'sf',
        'route' => 'admin_product_catalog',
    ]);
    // This will open admin product listing
    
    
    // Generate url with Symfony route and arguments
    $symfonyUrl = Link::getUrlSmarty([
        'entity' => 'sf',
        'route' => 'admin_customers_edit',
        'sf-params' => [
            'customerId' => 4,
        ],
    ]);
    // This will open customerId 4 for editing

    2. Using router via $link->getAdminLink (Available from PrestaShop 1.7.5)

    <?php
    
    use Context;
    
    $link = Context::getContext()->link;
    
    // Generate url with Symfony route
    $symfonyUrl = $link->getAdminLink(
         'AdminOrders',
         true,
         ['route' => 'admin_orders_index']
    );
    
    // Generate url with Symfony route and arguments
    $symfonyUrl = $link->getAdminLink(
        'AdminProducts', 
        true, 
        [
            'route' => 'admin_product_unit_action',
            'action' => 'delete',
            'id' => 42,
        ]
    );

    3. Javascript routes

    To generate a Symfony route in javascript, you can use the router component.

    The below snippets show an example :

    Searching for an experienced
    Prestashop Company ?
    Find out More
    import Router from '@components/router';
    
    this.router = new Router();
    const route = this.router.generate('my_route', {parameters});

    It however uses a computed file that you might need to recompute if you modified some route settings.

    You can recompute it using the below command :

    php bin/console fos:js-routing:dump --format=json

    And put it in

    admin-dev/themes/new-theme/js/fos_js_routes.json


    This is how we can generate Symfony route in PrestaShop module.

    That’s all.

    If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected].

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home