简体   繁体   中英

Sonata route parameters renaming

is there a possible way to rename route parameters in sonata-admin bundle? current admin route is like entity/{id}/show , would like to do entity/{name}/show . Thanks.

I think you can override the getIdParameter method in your administration class:

public function getIdParameter()
{
    return 'name';
}

routes are configured from the configureRoutes method of your admin class.

Here is how i added a custom route for the 'send' action of my 'EmailAdmin':

 protected function configureRoutes(RouteCollection $collection)
 { 
    parent::configureRoutes($collection);
    $collection->add('send', $this->getRouterIdParameter().'/send');
 }

The first parameter of add() is the name of the action and the second is the path of the route.

that way you can configure your custom routes and if you clear the collection or don't call parent::configureRoutes you can redefine all the default routes.

More detailed information from the sonata doc: https://sonata-project.org/bundles/admin/master/doc/reference/routing.html

A work around to change the id parameter could be to overwrite getIdParameter() and find the way to customize the action in the CRUDController so that items are queried by name or even creating your own custom action and route so you have full control

At the end of the day entities are always queried by their (unique) id so if you want the name of the entity as the id parameter in the url you either need to make the name property the identifier of your entity (and make it unique of course) or use something like apache mod_rewrite to rewrite your urls

Hope that helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM