php 如何在laravel中定义路由组名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36838177/
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
How to define route group name in laravel
提问by Alexey Mezenin
Is there any way to define the name of route group in laravel?
有没有办法在laravel中定义路由组的名称?
What I'm trying to accomplish by this is to know that the current request belongs to which group so I can make active the main menu and sub menu by the current route action:
我试图通过此完成的是知道当前请求属于哪个组,以便我可以通过当前路由操作激活主菜单和子菜单:
Code:
代码:
Route::group(['prefix'=>'accounts','as'=>'account.'], function(){
Route::get('/', 'AccountController@index')->name('index');
Route::get('connect', 'AccountController@connect')->name('connect');
});
Route::group(['prefix'=>'quotes','as'=>'quote.'], function(){
Route::get('/', 'QuoteController@index')->name('index');
Route::get('connect', 'QuoteController@create')->name('create');
});
Navigation HTML Code
导航 HTML 代码
<ul>
<li> // Add class 'active' when any route is open from account route group
<a href="{{route('account.index')}}">Accounts</a>
<ul>
<li> // Add class 'active' when connect sub menu is clicked
<a href="{{route('account.connect')}}">Connect Account</a>
</li>
</ul>
</li>
<li> // Add class 'active' when any route is open from quote route group
<a href="{{route('quote.index')}}">Quotes</a>
<ul>
<li> // Add class 'active' when create sub menu is clicked
<a href="{{route('quote.create')}}">Create Quote</a>
</li>
</ul>
</li>
</ul>
Now what I want is to call a function or something which will give me the current route's group name.
现在我想要的是调用一个函数或其他东西,它会给我当前路由的组名。
Examples:
例子:
- If I'm on index or create page of quotes
getCurrentRouteGroup()
should returnquote
- If I'm on index or connect page of accounts
getCurrentRouteGroup()
should returnaccount
- 如果我在索引或创建报价页
getCurrentRouteGroup()
应该返回quote
- 如果我在索引或连接帐户页面
getCurrentRouteGroup()
应该返回account
回答by Alexey Mezenin
This should work:
这应该有效:
Route::group(['prefix'=>'accounts','as'=>'account.'], function(){
Route::get('/', ['as' => 'index', 'uses' => 'AccountController@index']);
Route::get('connect', ['as' => 'connect', 'uses' = > 'AccountController@connect']);
});
Look herefor an explanation and in the official documentation(under Route Groups & Named Routes).
在此处查看解释和官方文档(在Route Groups & Named Routes 下)。
Update
更新
{{ $routeName = \Request::route()->getName() }}
@if(strpos($routeName, 'account.') === 0)
// do something
@endif
Alternative from Rohit Khatri
来自 Rohit Khatri 的替代品
function getCurrentRouteGroup() {
$routeName = Illuminate\Support\Facades\Route::current()->getName();
return explode('.',$routeName)[0];
}
回答by Sunil
Try this
尝试这个
Route::group(['prefix'=>'accounts','as'=>'account.'], function(){
Route::get('connect', [
'as' => 'connect', 'uses' => 'AccountController@connect'
]);
});
回答by Sambit Mohapatra
// both the format of defining the prefix are working,tested on laravel 5.6
Route::group(['prefix'=>'accounts','as'=>'account.'], function() {
Route::get('/', 'SomeController@index')->name('test');
Route::get('/new', function(){
return redirect()->route('account.test');
});
});
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
Route::get('/', [
'as' => 'custom',
'uses' => 'SomeController@index'
]);
Route::get('/custom', function(){
return route('admin.custom');
});
});
回答by Arun Joshi
It should work-
它应该工作-
inside blade-
内刀片-
{{ $yourRouteName = \Request::route()->getName() }}
// Find the first occurrence of account in URL-
@if(strpos($routeName, 'account.') === 0)
console the message or your code
@endif