3

I want to know if it's possible to add a global variable all through the site (so that if I need to change it, I would only need to do it once).

It's for an URL.

  • Can you please give more context on what you're trying to do and why? – Ryan Hoerr Jul 2 '18 at 16:52
  • Let me know if you have any issue – Prince Patel Jul 3 '18 at 8:32
4

You can use core session to store the value to access globally

use Magento\Framework\Session\SessionManagerInterface as CoreSession;

class MyClass
{

    protected $_coreSession;

    public function __construct(
        ...
        CoreSession $coreSession
        ...
    ) {
        $this->_coreSession = $coreSession;
    }

    public function setValue(){
        $this->_coreSession->start();
        $this->_coreSession->setMyVariable('My variable value');
    }
}

Now you can get your variable value anywhere by core seesion

    public function getValue(){
        $this->_coreSession->start();
        return $this->_coreSession->getMyVariable();
    }

You can also unset session variable by

    public function unsetValue(){
        $this->_coreSession->start();
        return $this->_coreSession->unsMyVariable();
    }

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.