46

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:

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

<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:

  1. If I'm on index or create page of quotes getCurrentRouteGroup() should return quote
  2. If I'm on index or connect page of accounts getCurrentRouteGroup() should return account

7 Answers7

64

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 here for an explanation and in the official documentation (under Route Groups & 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];
}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I want to get the current route group name on the view. –  Apr 25 '16 at 10:54
  • Just use same `{{ route('account.index') }}` – Alexey Mezenin Apr 25 '16 at 10:56
  • 1
    It gives me the route url, I don't want url. I want to call a function `getCurrentRouteGroup()` and it will give me the name of route group. Like If I'm on connect or index page of accounts, then `getCurrentRouteGroup()` will return `account`. –  Apr 25 '16 at 10:58
  • You didn't ask about it in your original question, but please look **Update** section of my answer. – Alexey Mezenin Apr 25 '16 at 11:10
  • It may achieve the goal, but it is not exactly "getting the name of the group". It is more "extract the *route* name and see if it matches the prefix defined in the group". In other word, getting the information from an other operation using the same input as the one we need. – challet May 09 '17 at 11:05
  • 1
    Thanks for the answer. Just a little fix - it is better to use `@php $routeName = \Request::route()->getName() @endphp`, because `{{}}` will output the variable. – gdfgdfg Nov 26 '19 at 00:55
  • Using as => 'account' then using . in the name of sub routes makes more sense. – Steve Moretz May 18 '21 at 11:59
13

You can use Route::name()->group(...) to prefix all names for a group of routes

Route::name('foo.')->prefix('xyz')->group(function() {

    Route::get('path', 'SomeController@method')->name('bar');

});

Here route('foo.bar') resolves to url /xyz/path

See related Laravel Docs

Don't forget to append dot in the prefix name :-)

igaster
  • 12,983
  • 6
  • 26
  • 27
5
// 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');
    });
}); 
Alexander Malakhov
  • 3,383
  • 2
  • 33
  • 58
  • 1
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – rollstuhlfahrer Mar 24 '18 at 10:43
2

Laravel 9 documentation says:

The name method may be used to prefix each route name in the group with a given string. For example, you may want to prefix all of the grouped route's names with admin. The given string is prefixed to the route name exactly as it is specified, so we will be sure to provide the trailing . character in the prefix:

Route::name('admin.')->group(function () {
    Route::get('users', function () {
        // Route assigned name "admin.users"...
    })->name('users');
});
miken32
  • 42,008
  • 16
  • 111
  • 154
Ali
  • 47
  • 5
  • This was [already suggested](https://stackoverflow.com/a/69026951/1255289) well before your answer was posted. – miken32 Jul 13 '23 at 19:45
  • It was linked to the older version of Larvel and iI thought n the new version its more clear... – Ali Aug 01 '23 at 22:30
-1

Try this

Route::group(['prefix'=>'accounts','as'=>'account.'], function(){

Route::get('connect', [
'as' => 'connect', 'uses' => 'AccountController@connect'
]);

});
Sunil
  • 1,106
  • 1
  • 7
  • 17
  • What I have works perfect, but I want to know on the view that what group the request belongs to. –  Apr 25 '16 at 10:53
-1

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
NickCoder
  • 1,504
  • 2
  • 23
  • 35
-1

In Laravel 9 you can now do this:

Route::controller(AccountController::class)->group(function () {
    Route::get('/', 'index')->name('index');
    Route::get('/connect', 'connect')->name('connect');
});
Eden Moshe
  • 1,097
  • 1
  • 6
  • 16