Laravel:在视图中获取当前路由命名空间(form.blade.php)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43558228/
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 current route namespace in view (form.blade.php)
提问by Rahul Sharma
i am using laravel 5.4 for my project and i just want to get the route namespace or prefix in my blade file. So is it possible to get the route group namespace?
我正在为我的项目使用 laravel 5.4,我只想在我的刀片文件中获取路由命名空间或前缀。那么是否有可能获得路由组命名空间?
My Route:
我的路线:
Route::group(['prefix'=>'organization','namespace'=>'Organization'], function(){
Route::get('team/{id}',['as'=>'info.team' , 'uses'=>'ManageTeamController@info']);
});
Blade File:
刀片文件:
{{dd(\Request::url())}}
i am getting this:
我得到这个:
"http://ocrm.dev/organization/team/8"
so i just need:
所以我只需要:
http://ocrm.dev/organization
is it possible?
是否可以?
回答by Alexey Mezenin
You can use starts_with()
and currentRouteName()
to check if route name starts from specified namespace string:
您可以使用starts_with()
和currentRouteName()
检查路由名称是否从指定的命名空间字符串开始:
if (starts_with(Route::currentRouteName(), 'Organization.'))
If you want to check URL, use is()
method:
如果要检查 URL,请使用is()
方法:
if (request()->is('organization*'))
If you want to get route prefix which is organization
, here's a bit modified solution by @sandeep, but globally available (so you could use in views, controllers, custom classes etc.):
如果你想获得路由前缀 which is organization
,这是@sandeep 稍微修改的解决方案,但全局可用(因此你可以在视图、控制器、自定义类等中使用):
request()->route()->getPrefix()
So, to get this URL:
因此,要获取此 URL:
http://ocrm.dev/organization
You could use url()
helper. This solution is also globally available:
你可以使用url()
助手。此解决方案也是全球可用的:
url(request()->route()->getPrefix())
回答by sandeep
you can use the prefix instead of namespace , because your namespace and prefix seems to same.
您可以使用 prefix 而不是 namespace ,因为您的命名空间和前缀似乎相同。
$request->route()->getPrefix()