2

I've got a couple controller that I'm trying to use to save and retrieve a custom piece of session data. It works on my local development environment but doesn't work on my staging environment. Here's the specs for my local environment:

  • Apache 2.4
  • PHP 7.0.16
  • Magento 2.1.5
  • Mysql 5.7

Here's the spec's for my staging environment

  • Apache 2.2
  • PHP 7.0.15
  • Mysql 5.6
  • Magento 2.1.5
  • Redis
  • Varnish

Here's my get controller:

<?php

namespace Foo\StoreLocator\Controller\Store;


class Get extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * Save constructor.
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession
    ){
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function execute()
    {
        $selectedStore = $this->customerSession->getSelectedStore();

        return $this->resultFactory
            ->create(\Magento\Framework\Controller\ResultFactory::TYPE_JSON)
            ->setData($selectedStore);
    }
}

Here's my set controller:

<?php

namespace Foo\StoreLocator\Controller\Store;

class Save extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $session;

    /**
     * Save constructor.
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession
    ){
        $this->session = $customerSession;
        parent::__construct($context);
    }

    /**
     * @return void
     */
    public function execute()
    {
        $data = $this->getRequest()->getPostValue();

        $this->session->setSelectedStore($data);

        return;
    }
}

I'm calling the controllers via AJAX call on product details pages. On my staging environment, the getter always returns null. I thought maybe it was an issue with full page cache being enabled on my staging environment because I don't use any caching on my local environment, but I disabled full page cache on staging and the issue persists.

Any ideas what's going on and/or how to fix this issue? I've reworked things to use a cookie but I'd really like to use the customer session.

2

It's easier to use registry as your controller name is not changing, you can add the following code in your 'set' controller action class file:

use Magento\Framework\Registry;

protected $registry;

And inject this in your class construct method.

public function __construct(Registry $registry)
{
  $this->registry = $registry;
}

You should then be able to register your data like below:

$this->registry->register('your_data', $data);

And in your 'get' controller action class file:

use Magento\Framework\Registry;

protected $registry;

And inject this in your class construct method.

public function __construct(Registry $registry)
{
  $this->registry = $registry;
}

And to retrieve data from registry do:

$this->registry->getData('your_data');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.