简体   繁体   中英

Get route to controller symfony2

I'm trying to figure out whether it's possible to get the route (as defined in the routes.yml file) to a specific controller?

An example may be helpful.

class SomeRandomController extends Controller {
    public function showAction($id) {
        //Get the entity and return with a Response object
    }
}

in routes.yml...

some_random_show:
    pattern: /some/random/thing/{id}/show
    defaults: { _controller: "MyBundle:SomeRandom:show", id: 0 }

So what I would like is get the route name (as in some_random_show ) if I have the class name and its namespace (as in MyBundle\\Controller\\SomeRandomController ). Any ideas?

EDIT Let's say I know the action, as I specifically want the show action for a controller.

The rationale behind this is a bit of a hack, but it comes down to me saving the class name and the id for a specific entity to the DB, and then I need to generate the show URL based on what entity class I persisted. If that makes any sense.

I don't think this will work, mainly because Actions can have multiple routes.

But if all you want to do is mapping a specific entity class to a specific route name, you should consider writing a service for it that returns a route name based on the entity name.

The route name could be generic (eg convert your entity name to lowercase and replace \\ with _) or you could define which entity maps to which route by hand in your config.yml.

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