Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 routing for same prefix group but different middleware giving error

I need all the route under same prefix manager with one middleware for guest manager_guest and another for logged in user manager_auth. This code bellow is my route web.php file.

Is there any other way ?

My routes:

Route::prefix('manager')->group(['middleware' => 'manager_guest'], function () {
    Route::get('/register', 'Manager\RegisterController@showRegister')->name('manager.register.create');
    Route::post('/register', 'Manager\RegisterController@register')->name('manager.register.store');
    Route::get('/login', 'Manager\LoginController@showLogin')->name('manager.login.create');
    Route::post('/login', 'Manager\LoginController@login')->name('manager.login');
});

Route::prefix('manager')->group(['middleware' => 'manager_auth'], function () {
    Route::post('/logout', 'Manager\LoginController@logout')->name('manager.logout');
    Route::get('/profile', 'Manager\PageController@profile')->name('manager.profile');
});

Error after executing php artisan route:list

PHP Warning: Uncaught ErrorException: Array to string conversion in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php:329
Stack trace:
#0 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Array to string...', 'E:\\laragon\\www\\...', 3
29, Array)
#1 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): require()
#2 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(285): Illuminate\Routing\Router->loadRoutes(Array)
#3 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\RouteRegistrar.php(104): Illuminate\Routing\Router->group(Array, Array)
#4 E:\laragon\www\laraveladmin\routes\web.php(30): Illuminate\Routing\RouteRegistrar->group(Array, Object(Closure))
#5 E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php(329): require('E:\\laragon\\www\\...')
#6 in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate\Routing\Router.php on line 329
PHP Fatal error:  Illuminate\Routing\Router::loadRoutes(): Failed opening required 'Array' (include_path='E:\Developer\Wbserver\php\PEAR') in E:\laragon\www\laraveladmin\vendor\laravel\framework\src\Illuminate
\Routing\Router.php on line 329
 
[Symfony\Component\Debug\Exception\FatalErrorException] Illuminate\Routing\Router::loadRoutes(): Failed opening required 'Array' (include_path='E:\Developer\Wbserver\php\PEAR')
like image 355
Rakesh Roy Avatar asked Aug 05 '17 08:08

Rakesh Roy


2 Answers

Try this instead

Route::group(['prefix' => 'manager',  'middleware' => 'manager_guest'], function() {

    });
like image 182
Clément Baconnier Avatar answered Oct 01 '22 15:10

Clément Baconnier


You could "factorize" your code like this:

Route::prefix('manager')->group(function () {
    Route::middleware(['manager_guest'])->group(function () {
        // These will be prefixed with "manager" and assigned the "manager_guest" middleware
    });

    Route::middleware(['manager_auth'])->group(function () {
        // These will be prefixed with "manager" and assigned the "manager_auth" middleware
    });

    // These will just be prefixed with "manager"
});

I noticed all your controllers live in the sub-namespace Manager. You can chain the methods and make your routes file even cleaner. For instance:

Route::prefix('manager')->namespace('Manager')->group(function () {
    Route::middleware(['manager_guest'])->group(function () {
        Route::get('register', 'RegisterController@showRegister')->name('mananger.register.create');
    });

    Route::middleware(['manager_auth'])->group(function () {
        Route::get('profile', 'PageController@profile')->name('mananger.profile');
    });
});
like image 23
louisfischer Avatar answered Oct 01 '22 15:10

louisfischer