gpt4 book ai didi

Laravel:在 View 中获取当前路由命名空间(form.blade.php)

转载 作者:行者123 更新时间:2023-12-01 04:58:30 25 4
gpt4 key购买 nike

我正在为我的项目使用 laravel 5.4,我只想在我的 Blade 文件中获取路由命名空间或前缀。那么是否有可能获得路由组命名空间?

我的路线:

Route::group(['prefix'=>'organization','namespace'=>'Organization'], function(){
Route::get('team/{id}',['as'=>'info.team' , 'uses'=>'ManageTeamController@info']);
});

Blade 文件:
{{dd(\Request::url())}}

我得到这个:
"http://ocrm.dev/organization/team/8"

所以我只需要:
http://ocrm.dev/organization

是否可以?

最佳答案

您可以使用 starts_with() currentRouteName() 检查路由名称是否从指定的命名空间字符串开始:

if (starts_with(Route::currentRouteName(), 'Organization.'))

如果要检查 URL,请使用 is()方法:
if (request()->is('organization*'))

如果你想获得路由前缀是 organization ,这是@sandeep 稍微修改的解决方案,但全局可用(因此您可以在 View 、 Controller 、自定义类等中使用):
request()->route()->getPrefix()

因此,要获取此 URL:
http://ocrm.dev/organization

您可以使用 url() helper 。此解决方案也是全局可用的:
url(request()->route()->getPrefix())

关于Laravel:在 View 中获取当前路由命名空间(form.blade.php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43558228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com