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
There are many ways by which you can get a current Route or URL in Angular. You can use the router service, location service or window object to get the path. You can also listen to changes to URL using the router event or URL change event of the location service.
The first way is through the route snapshot. The route snapshot provides the initial value of the route parameter map (called the paramMap ). You can access the parameters directly without subscribing or adding observable operators. The paramMap provides methods to handle parameter access like get , getAll , and has .
What is an activated route? The Angular Docs define the ActivatedRoute as. A service that is provided to each route component that contains route specific information such as route parameters, static data, resolve data, global query params and the global fragment.
navigateByUrl is similar to Router. navigate , except that a string is passed in instead of URL segments. The navigation should be absolute and start with a / . Here's a basic example using the Router.navigateByUrl method: goPlaces() { this.
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')
.
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
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'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With