Deadpool
Deadpool

Reputation: 806

How to override route in Symfony2

It's not a duplicate of override single route in symfony2. Please be patient and read my question carefully.

My problem is differs from the question above. So, my Symfony app has a standard structure. BUT it's configured in a specific way.

Next bundle is Custom bundle. How can extend DashboardController, override licenseContentAction and override only one (/license-content) route and make Symfony use my Custom bundle configuration?

Copying the same structure is not working. According to my explanation I wrote something like this:

<?php
namespace Custom\Custom1\Controller;
use Common\AdminBundle\Controller\DashboardController as BaseController;
class DashboardController extends BaseController
{
    public function licenseContentAction()
    {
       //return parent::licenseContentAction();
       die("HELLO!");
    }
}

How I can build route file for make extended method working?

Upvotes: 1

Views: 4283

Answers (1)

David Kmenta
David Kmenta

Reputation: 850

All you need to do is add these lines into the app/config/routing.yml file.

admin_dashboard_license_content:
    path: /dashboard/license-content
    defaults: { _controller: CustomBundle:Dashboard:licenseContent }

And create a DashboardController extending the base Symfony controller with a licenseContentAction method into the CustomBundle namespace.

And now the magic...

If you want to preserve the path, you have to add these lines at the beginning of the routing.yml file. The name of the route has to be different (unique) and the path of course has to be same as the overrode one.

The Symfony looks for the path and immediately when it matches the request is redirected to the corresponding controller action.

If you want to change the path add these lines at the end of the routing.yml file. The name of the route has to be same. I'm not sure if the previously defined route (path) still exists, I suppose yes, and if requests, made with this path, are still processed...

Symfony collects the routes into an array where the key is the name of a route. By adding a route with same name at the end of the file a previously defined route is overrode.

Upvotes: 1

Related Questions