问题:
问题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传递给组内的路由?
答案 0 :(得分:1)
找到答案
https://stackoverflow.com/a/36298013/4805056
原帖说要替换
Route::group(['middleware' => ['web']], function () {
...
});
与
Route::group(['middlewareGroups' => ['web']], function () {
...
});