Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel get route name from given URL

Tags:

In Laravel, we can get route name from current URL via this:

Route::currentRouteName()

But, how can we get the route name from a specific given URL?

Thank you.

like image 675
user1995781 Avatar asked Jul 05 '14 04:07

user1995781


People also ask

How do I get the route name in blade?

Example 1: Get current route name in Blade Files $route = Route::current(); dd($route); $name = $route->getName(); dd($name); $actionName = $route->getActionName(); dd($actionName); $name = Route::currentRouteName(); dd($name); $action = Route::currentRouteAction(); dd($action);

What is URI in Laravel?

Laravel Tutorial Index Routing in Laravel allows you to route all your application requests to their appropriate controller. The main and primary routes in Laravel acknowledge and accept a URI (Uniform Resource Identifier) along with a closure, given that it should have to be a simple and expressive way of routing.

Which of the following is correct to get the URL of named route contact in Laravel?

\Request::route()->getName();


2 Answers

A very easy way to do it Laravel 5.2

app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1'))->getName()

It outputs my Route name like this slug.posts.show

Update: For method like POST, PUT or DELETE you can do like this

app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1', 'POST'))->getName()//reference https://github.com/symfony/http-foundation/blob/master/Request.php#L309

Also when you run app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1', 'POST')) this will return Illuminate\Routing\Route instance where you can call multiple useful public methods like getAction, getValidators etc. Check the source https://github.com/illuminate/routing/blob/master/Route.php for more details.

like image 55
ARIF MAHMUD RANA Avatar answered Sep 29 '22 17:09

ARIF MAHMUD RANA


None of the solutions above worked for me.

This is the correct way to match a route with the URI:

$url = 'url-to-match/some-parameter';

$route = collect(\Route::getRoutes())->first(function($route) use($url){
   return $route->matches(request()->create($url));
});

The other solutions perform bindings to the container and can screw up your routes...

like image 36
BassMHL Avatar answered Sep 29 '22 15:09

BassMHL