Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get route from url

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

like image 561
bux Avatar asked Nov 22 '11 15:11

bux


People also ask

How do I find the URL of a route?

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.

How do I access route parameter?

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 ActivatedRoute in Angular?

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.

What is navigateByUrl in Angular?

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.


3 Answers

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').

like image 188
Seldaek Avatar answered Oct 19 '22 01:10

Seldaek


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

like image 39
fesja Avatar answered Oct 19 '22 00:10

fesja


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'];
like image 36
Marciano Avatar answered Oct 19 '22 02:10

Marciano