How to define route group name in laravel

后端 未结 4 621
予麋鹿
予麋鹿 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:16

    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
    
    0 讨论(0)
  • 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];
    }
    
    0 讨论(0)
  • 2020-12-15 17:33

    Try this

    Route::group(['prefix'=>'accounts','as'=>'account.'], function(){
    
    Route::get('connect', [
    'as' => 'connect', 'uses' => 'AccountController@connect'
    ]);
    
    });
    
    0 讨论(0)
  • 2020-12-15 17:36
    // 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 讨论(0)
提交回复
热议问题