Magento 2会话自行重置 [英] Magento 2 session unsetting itself

查看:115
本文介绍了Magento 2会话自行重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论是否已登录,我都必须能够存储自定义存在的一些自定义会话变量,但是由于某些原因,我的会话会不断删除自身.

I need to be able to store some custom session variables that exist for the custom, regardless of whether they're signed in or not, but for some reason my sessions keep deleting themselves.

我已使用示例帮我添加会话代码.

I have used this example to help me add my session code in.

这是我的代码

阻止文件

<?php

namespace MyVendor\MyModel\Block;

use Magento\Framework\View\Element\Template;

class ProductSearch extends Template {

    protected $_customSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customSession,
        array $data = []
    ){
        parent::__construct($context, $data);
        $this->_customSession = $customSession;
    }

    //Get the car model from the session
    public function getSessionCarModel(){
        return $this->_customSession->getCarModel();
    }

    //Unset the car model from the session
    public function unsetSessionCarModel(){
        return $this->_customSession->unsCarModel();
    }

}

这是我的模板文件的顶部,它在加载会话时对会话进行排序

and heres the top of my template file that sorts the session when its loaded

productsearchbanner.phtml

<?php

    //If the user has selected a new model, unset our session then start a new one
    if(isset($_POST['modelSelect'])){
        //Unset the other sessions
        $block->unsetSessionCarModel();
        //Set the model session
        $block->setSessionCarModel($_POST['model']);
    }
    echo '<pre>';
    var_dump($_SESSION);
    echo '</pre>';
?>

代码的工作方式是,如果设置了$_POST['modelSelect'],则用户来自模型选择页面,因此我们需要再次启动该过程并重置其会话,但是如果没有,会话应该保持不变.

The way the code is meant to work is, if the $_POST['modelSelect'] is set, the user has come from the model select page so we need to start the process again and reset their session, but if they haven't, the session should remain the same.

我的问题是,当我来自模型选择页面时,我的会话变量在var dump中显示没有问题,如下所示.

My issue is, when I come from the model select page, my session variable shows in the var dump no problem, as shown below.

但是,一旦我转到网站上的任何其他页面(例如,首页)然后返回到产品搜索页面,会话就已经清除了吗?

But then as soon as I go to any another page on my site (for example, the homepage) and then back to the product search page, the session has cleared?

我做错了什么?为什么每次加载页面时都会清除会话?我只需要能够设置$_SESSION['carModel']的等效项,并且对于该用户而言,它是永久的,无论他们是否已登录,或者他们在网站上的位置如何.

What am I doing wrong? Why does my session clear every time I load a page? I just need to be able to set the equivalent of $_SESSION['carModel'] and it be persistent for that user, regardless of if they're logged in or not, or where on the site they go.

有人可以指出我正确的方向吗?

Can someone please point me in the right direction?

推荐答案

在块或模板文件中设置会话是一个问题.这是因为有完整的页面缓存. Magento的执行周期随着FPC的打开而改变.

Setting sessions in Blocks or Template files is a problem. This is because of full page cache. Magento's execution cycle changes with FPC turned on.

控制器或模型是更新会话数据的最佳位置.

Controllers or Models are the best places to update session data.

但是,如果您需要在模板/块中更新会话,则可以通过AJAX调用自定义操作,并让其更新会话信息. 通常,需要在Magento 2中执行以下步骤:

But, if you need to update your session in a template / block, then you can call a custom action via AJAX and have it update the session info. Generally, one would need to take these steps in Magento 2:

  • 在现有或新模块中创建新的控制器/操作对,以更新会话信息.理想情况下,此控制器应仅接受AJAX请求.

  • create a new controller / action pair in an existing or a new module, that would update session info. This controller should ideally accept AJAX requests only.

在容器before_body_end中呈现了一个模板,并在其中放置了一些jQuery代码,这些代码将查询控制器/操作对以更新会话信息.

have a template rendered in container before_body_end and toss some jQuery code in there, that will query the controller / action pair to have the session info updated.

这样,每当页面加载时,它都会通过请求控制器/操作(例如,.

This way, whenever the page will load, it will trigger a session update ( or you can have it trigger on any other event, say when a user clicks something etc. ) by requesting your controller / action, say, /my-module/my-controller/my-session-updater-action.

这篇关于Magento 2会话自行重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆