Magento2 我正在我的块中使用\Magento\Customer\Model\SessionFactory$customerSession,但如果没有可缓存的false,它将无法工作

Magento2 我正在我的块中使用\Magento\Customer\Model\SessionFactory$customerSession,但如果没有可缓存的false,它将无法工作,magento2,Magento2,如果需要在块中获取当前客户会话,则必须在布局xml中将cacheable设置为false。我正在我的块中使用\Magento\Customer\Model\SessionFactory$customerSession,但如果没有可缓存的false,它将无法工作。有人能告诉我解决这个问题的推荐方法吗。默认情况下,会话缓存在块中。但是,您可以使用cacheable=false来使用块中的会话,但不建议这样做 我建议使用注册表以块的形式存储/获取信息。如果您需要来自会话的特定信息,您可以从块的控制器在

如果需要在块中获取当前客户会话,则必须在
布局xml
中将cacheable设置为false。我正在我的块中使用
\Magento\Customer\Model\SessionFactory$customerSession
,但如果没有可缓存的false,它将无法工作。有人能告诉我解决这个问题的推荐方法吗。

默认情况下,会话缓存在块中。但是,您可以使用cacheable=false来使用块中的会话,但不建议这样做

我建议使用注册表以块的形式存储/获取信息。如果您需要来自会话的特定信息,您可以从块的控制器在注册表中设置此信息

以下是一些信息:


祝你好运

你能检查一下吗?这可能对你有帮助

namespace Vendor\Module\Block;


class Form extends \Magento\Framework\View\Element\Template
{
    protected $customerSession;

    /**
     * Construct
     *
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ) {
        parent::__construct($context, $data);

        $this->customerSession = $customerSession;
    }

    public function _prepareLayout()
    {

        var_dump($this->customerSession->getCustomer()->getId());
        exit();
        return parent::_prepareLayout();
    }
}