Reputation: 7739
In Symfony2, do you know how to find a route from a url in controller? I have this example:
$params = $router->match('/blog/my-blog-post');
// array('slug' => 'my-blog-post', '_controller' => 'AcmeBlogBundle:Blog:show')
$uri = $router->generate('blog_show', array('slug' => 'my-blog-post'));
// /blog/my-blog-post
I would like find blog_show
when i have /blog/my-blog-post
Thank you
Upvotes: 10
Views: 7602
Reputation: 123
you can get the same error if using absolute paths this is what I did when needed to match the referrer
$ref = str_replace("app_dev.php/", "", parse_url($request->headers->get('referer'),PHP_URL_PATH ));
$route = $this->container->get('router')->match($ref)['_route'];
Upvotes: 1
Reputation: 3313
I recently discovered that the match() method uses the HTTP METHOD of the current request in order to match the request. So if you are doing a PUT request for example, it will try to match the URL you have given with a PUT method, resulting in a MethodNotAllowedException exception (for example, getting the referer).
See more in https://stackoverflow.com/a/16506062/100675
Upvotes: 1
Reputation: 42036
I don't know what you have in that $router
, but with the router service, I get this here:
$this->get('router')->match('/')
array
'_controller' => string 'Namespace\Foo\MyController::indexAction'
'_route' => string 'home'
If you want the route name of the current page by the way you can just read it from the request object: $request->attributes->get('_route')
.
Upvotes: 16