How to determine the name of a route group in laravel

Is it possible to determine the name of a route group in laravel?

What I'm trying to do is to know that the current request belongs to which group, so I can activate the main menu and submenu for 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'); }); 

HTML code for navigation

 <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 I want to call a function or something that will give me the current name of the route group.

Examples:

  • If I am in the index or create the quotes page getCurrentRouteGroup() , return quote
  • If I'm on the indexing or connecting pages page, getCurrentRouteGroup() should return account
+20
source share
4 answers

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 ']); }); 

Find an explanation and white papers here (under Route groups and named routes).

Update

 {{ $routeName = \Request::route()->getName() }} @if(strpos($routeName, 'account.') === 0) // do something @endif 

Alternative from Rohit Khatri

 function getCurrentRouteGroup() { $routeName = Illuminate\Support\Facades\Route::current()->getName(); return explode('.',$routeName)[0]; } 
+21
source

try it

 Route::group(['prefix'=>'accounts','as'=>'account.'], function(){ Route::get('connect', [ 'as' => 'connect', 'uses' => ' AccountController@connect ' ]); }); 
0
source
 // 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'); }); }); 
0
source

Gotta 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 
0
source

Source: https://habr.com/ru/post/1247859/


All Articles