Reputation: 806
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.
app/config/routing.yml contains several bundles routes, for example :
_project_custom1:
resource: "@ProjectCustom1Bundle/Resources/config/routing.yml"
prefix: /custom1
_project_custom2:
resource: "@ProjectCustom2Bundle/Resources/config/routing.yml"
prefix: /custom2
src/ directory has two bundles and next structure, e.g.
Common/AdminBundle/Resources/config/routing.yml
Common/AdminBundle/Resources/config/routing_dashboard.yml
routing.yml
contains next lines:
admin_dashboard_content:
resource: "@CommonAdminBundle/Resources/config/routing_dashboard.yml"
prefix: /dashboard
routing_dashboard.yml
contains:
admin_dashboard_license_content:
path: /license-content
defaults: { _controller: CommonAdminBundle:Dashboard:licenseContent }
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
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