如何在我的 Laravel 应用程序中回显 'module' 的名称?
How do I echo the name of a 'module' in my Laravel app?
目前我的 Laravel 应用程序中有不同的模块
App
- Module
- Module 1
- Module 2
- Module 3
并且我已将每个模块的路由分组(为简单起见)。
// Module
Route::group(
[
'namespace' => 'Module',
'prefix' => 'Module',
],
function () {
Route::get('/', 'ModuleController@redirect');
Route::get('home', 'ModuleController@index')->name('Module');
在导航栏中有标题的视图(blade 模板)中,我希望标题是路线组的名称,因此无论我在模块中的哪个页面上,模块名称在导航栏中。
目前我试过
{{ Request::path() }} // which returns Module/PageName
在 blade 模板中。还有这个
{{ Route::getCurrentRoute()->getPrefix() }} // which returns /Module
但是这两个在输出中都有额外的东西。第二个是我得到的最接近的我只需要删除'/'
我不确定这是否是实现此目的的最佳方法,但如果您有 better/more 有效的方法,也请告诉我。
提前致谢。
您可以输入路由名称,然后使用Route::currentRouteName()
获取路由名称。希望对您有所帮助!
我认为解决方案已经在您的代码中。
您可以使用第一种解决方案。
只需替换此:
{{ Request::path() }}
使用此代码:
{{ explode('/', Request::path())[0] }}
方法explode
会在每次找到'/'
字符时将Request::path()
分成Array
。然后你选择数组的第一个元素 [0]
其中包含你需要的 Module
名称。
目前我的 Laravel 应用程序中有不同的模块
App
- Module
- Module 1
- Module 2
- Module 3
并且我已将每个模块的路由分组(为简单起见)。
// Module
Route::group(
[
'namespace' => 'Module',
'prefix' => 'Module',
],
function () {
Route::get('/', 'ModuleController@redirect');
Route::get('home', 'ModuleController@index')->name('Module');
在导航栏中有标题的视图(blade 模板)中,我希望标题是路线组的名称,因此无论我在模块中的哪个页面上,模块名称在导航栏中。
目前我试过
{{ Request::path() }} // which returns Module/PageName
在 blade 模板中。还有这个
{{ Route::getCurrentRoute()->getPrefix() }} // which returns /Module
但是这两个在输出中都有额外的东西。第二个是我得到的最接近的我只需要删除'/'
我不确定这是否是实现此目的的最佳方法,但如果您有 better/more 有效的方法,也请告诉我。
提前致谢。
您可以输入路由名称,然后使用Route::currentRouteName()
获取路由名称。希望对您有所帮助!
我认为解决方案已经在您的代码中。 您可以使用第一种解决方案。 只需替换此:
{{ Request::path() }}
使用此代码:
{{ explode('/', Request::path())[0] }}
方法explode
会在每次找到'/'
字符时将Request::path()
分成Array
。然后你选择数组的第一个元素 [0]
其中包含你需要的 Module
名称。