我正在尝试使用会话实现Flash消息,但我无法这样做.
在我的控制器中,我有:
public function store(Request $request) { session()->flash('donald','duck'); session()->put('mickey','mouse'); return redirect()->action('CustomerController@index')->with('bugs','bunny'); }
但是当我在视图中检查会话变量时,我只能看到session() – > put(‘mickey’,’mouse’)中的值.
会议:
{"_token":"F6DoffOFb17B36eEJQruxvPe0ra1CbyJiaooDn3F","_prevIoUs":{"url":"http:\/\/localhost\/customers\/create"},"flash":{"old":[],"new":[]},"mickey":"mouse"}
很多人通过在Web中间件中没有相关路由来遇到这个问题.我也确保这样做但它仍然不起作用.
在routes.PHP中:
Route::group(['middleware' => ['web']],function () { Route::get('/',function () { return view('welcome'); }); Route::get('/customers','CustomerController@index'); Route::get('/customers/create','CustomerController@create'); Route::post('/customers','CustomerController@store'); });
在Kernel.PHP中:
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',];
任何人都可以让我知道我在这里做错了什么吗?谢谢!
通过替换修复了问题
原文链接:https://www.f2er.com/laravel/133856.htmlRoute::group(['middleware' => ['web']],function () { ... });
同
Route::group(['middlewareGroups' => ['web']],function () { ... });
当所有文档都表明我们使用[‘middleware’=>时,不知道为什么会这样有效. [ ‘卷筒纸’]]