php – Laravel 5.2嵌套路由组与中间件

问题:

> Session :: flash无效
>奇怪的路线参数

问题1

我遇到了一个错误,Session :: flash无法正常工作.
我也发现了一种解决方法,但它很奇怪.

我认为嵌套组和中间件有问题.

码:

Route::group(['middleware' => 'web'], function () {
    Route::group([
            'prefix' => '{locale}',
            'middleware' => ['localized']
        ], function($locale)
    {
        Route::resource('/', 'ProductController', ['only' => ['index']]);
        Route::get('/cart', 'CartController@show')->name('show-shopping-cart');
    });

    Route::patch('/cart', 'CartController@update')->name('patch-cart');
});

现在..
如果我以这种方式修改kernel.php,这将有效:

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class,
];

protected $middlewareGroups = [
    'web' => [
    ],

    'api' => [
        'throttle:60,1',
    ],
];

你怎么看待这件事?

问题2

在上面的路由中,ProductController @ show获得了2个参数:locale和id.

例如,如果我导航到/ en / 4

function show($id){
    dd($id);
}

$id =“en”

它会以这种方式工作:

function show($locale, $id){
    dd($id);
}

这是设计的吗?
如果有,是否有办法避免将$locale传递给组内的路由?

最佳答案 找到了答案

https://stackoverflow.com/a/36298013/4805056

原帖说要替换

Route::group(['middleware' => ['web']], function () {
   ...
});

Route::group(['middlewareGroups' => ['web']], function () {
   ...
});
点赞