Laravel 5.2嵌套路由组与中间件

时间:2016-05-02 20:23:18

标签: php laravel-5.2

问题:

  1. Session :: flash无法正常工作
  2. 奇怪的路线参数
  3. 问题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传递给组内的路由?

1 个答案:

答案 0 :(得分:1)

找到答案

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

原帖说要替换

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

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