How to define route group name in laravel

后端 未结 4 627
予麋鹿
予麋鹿 2020-12-15 17:05

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

4条回答
  •  星月不相逢
    2020-12-15 17:24

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

提交回复
热议问题