48

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

70

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];
}
Sign up to request clarification or add additional context in comments.

7 Comments

I want to get the current route group name on the view.
Just use same {{ route('account.index') }}
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.
You didn't ask about it in your original question, but please look Update section of my answer.
Thanks for the answer. Just a little fix - it is better to use @php $routeName = \Request::route()->getName() @endphp, because {{}} will output the variable.
|
19

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

1 Comment

This is the way to go since probably Laravel 8.x
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');
    });
}); 

1 Comment

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.
3

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

2 Comments

This was already suggested well before your answer was posted.
It was linked to the older version of Larvel and iI thought n the new version its more clear...
0

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

1 Comment

This is the same as old version provide. please share some new solution.
-1

Try this

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

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

});

1 Comment

What I have works perfect, but I want to know on the view that what group the request belongs to.
-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

Comments