Search code examples
laravellaravel-5laravel-5.3

Is it possible use controller with different namespace in route group ?


Is it possible use controller with different namespace in route group ?

Route::group(['prefix' => 'dashboard', 'namespace' => 'admin'], function () {
    Route::get('/', ['namespace'=>'Controllers','uses'=>'SiteController@dashobard']);
    Route::get('posts', 'PostsController@index');
});

Solution

  • As @TimLewis has mentioned in the comments it is possible.

    (Assuming the full namespace for SiteController is App\Http\Controllers) The following should work:

    Route::group(['prefix' => 'dashboard', 'namespace' => 'admin'], function () {
        Route::get('/', '\App\Http\Controllers\SiteController@dashboard');
        Route::get('posts', 'PostsController@index');
    });
    

    However, it would make more sense to split the routes up:

    Route::group(['prefix' => 'dashboard'], function () {
    
        Route::get('/', 'SiteController@dashboard');
    
        Route::group(['namespace' => 'admin'], function () {
            Route::get('posts', 'PostsController@index');
        });
    });
    

    Hope this helps!