php - 放入 session 后,Laravel session key 为空

标签 php angularjs session laravel

我有一个有线问题。我在 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/

相关文章:

angularjs - Angular ui 路由器中的 404 页面

mysql - Joomla session 表中的多次插入/更新

session - Web 应用程序的基于 token 的身份验证 : how to cache token?

PHP XDebug 禁止中断每个请求

php - 如何为上传的照片生成短文件名?

javascript - 在 Angular 中向 json 对象添加空数组

session - 如何防止 ashx 重置 session 超时?

javascript - 进度条 AJAX 和 PHP

php - 从数据库中检索数据到下拉菜单,但它没有显示在那里

css - :hover over <div> does not apply to all its child elements