Laravel 从给定的 URL 获取路由名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24582922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel get route name from given URL
提问by user1995781
In Laravel, we can get route name from current URL via this:
在 Laravel 中,我们可以通过以下方式从当前 URL 获取路由名称:
Route::currentRouteName()
But, how can we get the route name from a specific given URL?
但是,我们如何从特定的给定 URL 获取路由名称?
Thank you.
谢谢你。
回答by ARIF MAHMUD RANA
A very easy way to do it Laravel 5.2
一个非常简单的方法 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
它像这样输出我的路线名称 slug.posts.show
Update: For method like POST, PUTor DELETEyou can do like this
更新:对于像POST、PUT或DELETE这样的方法,你可以这样做
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.phpfor more details.
此外,当你运行app('router')->getRoutes()->match(app('request')->create('/qqq/posts/68/u1', 'POST'))
这将返回Illuminate\Routing\Route
实例在那里你可以调用多个有用的公共方法,如getAction
,getValidators
等检查源https://github.com/illuminate/routing/blob/master/Route.php了解更多详情。
回答by Seb Barre
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.
我不认为这可以用开箱即用的 Laravel 来完成。还要记住,并不是 Laravel 中的所有路由都被命名,所以你可能想要检索路由对象,而不是路由名称。
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.
一种可能的解决方案是扩展默认\Iluminate\Routing\Router
类并向使用受保护Router::findRoute(Request $request)
方法的自定义类添加公共方法。
A simplified example:
一个简化的例子:
class MyRouter extends \Illuminate\Routing\Router {
public function resolveRouteFromUrl($url) {
return $this->findRoute(\Illuminate\Http\Request::create($url));
}
}
This shouldreturn the route that matches the URL you specified, but I haven't actually tested this.
这应该返回与您指定的 URL 匹配的路由,但我还没有实际测试过。
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.
请注意,如果您希望这个新的自定义路由器替换内置路由器,您可能还必须创建一个新的 ServiceProvider 来将您的新类注册到 IoC 容器中,而不是默认的。
You could adapt the ServiceProvider in the code below to your needs:
您可以根据您的需要调整以下代码中的 ServiceProvider:
https://github.com/jasonlewis/enhanced-router
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');
回答by Shahid
It can be done without extending the default \Iluminate\Routing\Router
class.
它可以在不扩展默认\Iluminate\Routing\Router
类的情况下完成。
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.
如果您在调用Route::currentRouteName()
后dispatchToRoute
调用,它将返回已调度请求的当前路由名称。
回答by BassMHL
None of the solutions above worked for me.
上述解决方案都不适合我。
This is the correct way to match a route with the URI:
这是将路由与 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...
其他解决方案执行到容器的绑定,并可能搞砸你的路线......