Laravel get route name from given URL
Asked Answered
F

5

24

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.

Frenchman answered 5/7, 2014 at 4:19 Comment(5)
Can you explain what you are trying to do?Bergeman
@Bergeman I am trying to get the route name from a given URL, not current URL.Frenchman
No, I totally get it, but I don't understand why you need to do it. Essentially you are looking to reverse engineer the routes.php file.Bergeman
That's not a good patternStocks
If you could explain why, we can find another wayStocks
G
38

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.

Grant answered 7/4, 2016 at 12:29 Comment(0)
A
12

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

Assorted answered 3/8, 2019 at 16:1 Comment(1)
Thanks, this is the only solution that work without breaking some routes, especially route with parametersOlli
P
5

I don't think this can be done with out-of-the-box Laravel. Also remember that not all routes in Laravel are named, so you probably want to retrieve the route object, not the route name.

One possible solution would be to extend the default \Iluminate\Routing\Router class and add a public method to your custom class that uses the protected Router::findRoute(Request $request) method.

A simplified example:

class MyRouter extends \Illuminate\Routing\Router {

    public function resolveRouteFromUrl($url) {
        return $this->findRoute(\Illuminate\Http\Request::create($url));
    }
}

This should return the route that matches the URL you specified, but I haven't actually tested this.

Note that if you want this new custom router to replace the built-in one, you will likely have to also create a new ServiceProvider to register your new class into the IoC container instead of the default one.

You could adapt the ServiceProvider in the code below to your needs:

https://github.com/jasonlewis/enhanced-router

Otherwise if you just want to manually instantiate your custom router in your code as needed, you'd have to do something like:

$myRouter = new MyRouter(new \Illuminate\Events\Dispatcher());
$route = $myRouter->resolveRouteFromUrl('/your/url/here');
Penury answered 7/8, 2014 at 12:12 Comment(0)
A
5

It can be done without extending the default \Iluminate\Routing\Router class.

    Route::dispatchToRoute(Request::create('/your/url/here'));
    $route = Route::currentRouteName();

If you call Route::currentRouteName() after dispatchToRoute call, it will return current route name of dispatched request.

Aconite answered 5/4, 2016 at 8:32 Comment(0)
D
0

Laravel 10

<?php
\Route::getRoutes()->match(Request::create($url));
Defy answered 6/12, 2023 at 5:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.