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.

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

1 Answer 1

7

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();
    }
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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