user1995781
user1995781

Reputation: 19453

Laravel get route name from given URL

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.

Upvotes: 24

Views: 30167

Answers (5)

StupidDev
StupidDev

Reputation: 342

Laravel 10

<?php
\Route::getRoutes()->match(Request::create($url));

Upvotes: 0

BassMHL
BassMHL

Reputation: 9047

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

Upvotes: 12

ARIF MAHMUD RANA
ARIF MAHMUD RANA

Reputation: 5166

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.

Upvotes: 38

Shahid
Shahid

Reputation: 357

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.

Upvotes: 5

Seb Barre
Seb Barre

Reputation: 1647

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

Upvotes: 5

Related Questions