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

时间:2017-08-05 08:06:30

标签: php laravel-5 routing

我需要使用相同前缀 manager 下的所有路由,其中​​一个中间件用于访客 manager_guest ,另一个用于登录用户 manager_auth 。 下面的代码是我的路由web.php文件。

还有其他办法吗?

我的路线:

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

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

3 个答案:

答案 0 :(得分:11)

试试这个

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

    });

答案 1 :(得分:6)

你可以"分解"你的代码是这样的:

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

我注意到所有控制器都存在于子命名空间管理器中。您可以链接方法并使您的路线文件更清晰。例如:

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

答案 2 :(得分:0)

由于我有很多更改路线,并且不想更改名称空间,因此其他答案对我都无效。进行这项工作的关键是“按原样”。缺点是使用“ route()”时会更改路径,但是您在此处的每条路径上使用名称都将覆盖该路径。

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

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