单击后Laravel会话数据丢失 [英] Laravel session data is lost after click
问题描述
class FileController extends Controller
{
public function login()
{
/*
* TODO: Handle via CAS
* Hardcoded for demo purposes
*/
Session::put('isLogged', true);
Session::put('index', "123456");
return View::make('login');
}
public function user()
{
if(Session::get('isLogged') == true )
return View::make('user');
}
}
我有以下代码.登录时有一个指向FileControllers @ user的链接.在第二页上,我的会话数据丢失(Session :: all()为空).是什么引起了这个问题?
I have the following code. There is a link on login that goes to the FileControllers@user . On the second page my session data is lost (Session::all() is empty). What could be causing this issue?
推荐答案
尝试将路由(在 app/Http/routes.php
内部)包装在 Route :: group()
与 web
中间件:
Try wrapping your routes (inside app/Http/routes.php
) in a Route::group()
with the web
middleware:
Route::group(['middleware' => ['web']], function () {
// My Routes
});
一种简单的测试方法:
Route::group(['middleware' => 'web'], function () {
Route::get('', function () {
Session::set('test', 'testing');
});
Route::get('other', function () {
dd(Session::get('test'));
});
});
如果删除Web中间件,则将收到 null
,因为 web
中间件负责启动会话.
If you remove the web middleware, you'll receive null
since the web
middleware is responsible for starting the session.
确保您的 app/Http/Kernel.php
中具有 web
中间件组:
Ensure you have the web
middleware group inside your app/Http/Kernel.php
:
protected $middlewareGroups = [
'web' => [
Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
Middleware\VerifyCsrfToken::class,
],
];
这篇关于单击后Laravel会话数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!