我有一个有线问题。我在 session 中放入了一些字符串/对象,但当我想要获取它时却不存在。
这是代码:
class CartController extends \BaseController
{
public function index()
{
return Session::all(); // items is not there
}
public function store()
{
Session::put('items', Input::get('items'));
}
}
Angular :
this.saveItemsToSession = function() {
$http.post('cart', {
items: 'test even string'
});
};
什么会导致这个问题?
它似乎 Session 不工作。 这种方式有效:
session_start();
class CartController extends \BaseController
{
public function index()
{
return $_SESSION['items'];
}
public function store()
{
$_SESSION['items'] = Input::get('items');
}
}
最佳答案
来自 laravel 文档:https://laravel.com/docs/master/routing
Any routes not placed within the web middleware group will not have access to sessions and CSRF protection, so make sure any routes that need these features are placed within the group. Typically, you will place most of your routes within this group:
Route::group(['middleware' => ['web']], function () {
//all routes
});
将所有路由放在中间件中,您的问题就会解决。
关于php - 放入 session 后,Laravel session key 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27504907/