How to define route group name in laravel(如何在 laravel 中定义路由组名称)
问题描述
有什么方法可以在 laravel 中定义路由组的名称吗?
Is there any way to define the name of route group in 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:
代码:
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');
});
导航 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.
例子:
- 如果我在索引或创建报价页面
getCurrentRouteGroup()
应该返回quote
- 如果我在帐户的索引或连接页面上,
getCurrentRouteGroup()
应该返回account
- 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
推荐答案
这应该可行:
Route::group(['prefix'=>'accounts','as'=>'account.'], function(){
Route::get('/', ['as' => 'index', 'uses' => 'AccountController@index']);
Route::get('connect', ['as' => 'connect', 'uses' = > 'AccountController@connect']);
});
看这里以获得解释并在 官方文档(在路由组和命名路由下).
Look here for an explanation and in the official documentation (under Route Groups & Named Routes).
更新
{{ $routeName = Request::route()->getName() }}
@if(strpos($routeName, 'account.') === 0)
// do something
@endif
Rohit Khatri 的替代品
function getCurrentRouteGroup() {
$routeName = IlluminateSupportFacadesRoute::current()->getName();
return explode('.',$routeName)[0];
}
这篇关于如何在 laravel 中定义路由组名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 laravel 中定义路由组名称
基础教程推荐
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01