简体   繁体   English

Laravel 5.4路由相同的前缀组但不同的中间件给出错误

[英]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 . 我需要在同一前缀管理器下的所有路由,其中​​一个中间件用于guest manager_guest ,另一个用于登录用户manager_auth This code bellow is my route web.php file. 下面的代码是我的路由web.php文件。

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

Try this instead 试试这个

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

    });

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

None of the other answers worked for me as I had a lot of routes to change, and didn't want to change namespaces. 没有其他答案对我有用,因为我有很多路径要改变,并且不想更改名称空间。 The key to making this work is "as". 做这项工作的关键是“作为”。 The downside of this being that it changes the path when using "route()", but your use of name on each route here would override that anyway. 这样做的缺点是它在使用“route()”时改变了路径,但是你在这里使用每条路线上的名字无论如何都会覆盖它。

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

Route::group(['prefix' => 'manager', 'middleware' => ['manager_auth'], 'as' => 'manager_auth'], function() {
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM