Skip to main content

Anything wrong in below code, imI'm trying pass argument from di.xml to class

I'm trying to understand virtual types and how we can use them in practicalpractice. Referred some code from the vendor, still its, it is giving me an error when imI'm trying to access inthe helper class.

namespace Ravi\Learn\Helper;

use Magento\Framework\App\Helper\Context;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;
    public function __construct(Context $context, $customerSession)
    {
        parent::__construct($context);
        $this->customerSession = $customerSession;
    }

    public function getSess(){
       return $this->customerSession->getId();
    }
}





 

Below is the code for di.xml file

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <virtualType name="Ravi\Learn\Helper\Data\Session" type="Ravi\Learn\Helper\Data">
        <arguments>
            <argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session</argument>
        </arguments>
    </virtualType>
</config>

Anything wrong in below code, im trying pass argument from di.xml to class

I'm trying to understand virtual types and how we can use them in practical. Referred some code from vendor, still its giving me an error when im trying to access in helper class.

namespace Ravi\Learn\Helper;

use Magento\Framework\App\Helper\Context;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;
    public function __construct(Context $context, $customerSession)
    {
        parent::__construct($context);
        $this->customerSession = $customerSession;
    }

    public function getSess(){
       return $this->customerSession->getId();
    }
}





 
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <virtualType name="Ravi\Learn\Helper\Data\Session" type="Ravi\Learn\Helper\Data">
        <arguments>
            <argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session</argument>
        </arguments>
    </virtualType>
</config>

Anything wrong in below code, I'm trying pass argument from di.xml to class

I'm trying to understand virtual types and how we can use them in practice. Referred some code from the vendor, still, it is giving me an error when I'm trying to access the helper class.

namespace Ravi\Learn\Helper;

use Magento\Framework\App\Helper\Context;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;
    public function __construct(Context $context, $customerSession)
    {
        parent::__construct($context);
        $this->customerSession = $customerSession;
    }

    public function getSess(){
       return $this->customerSession->getId();
    }
}

Below is the code for di.xml file

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <virtualType name="Ravi\Learn\Helper\Data\Session" type="Ravi\Learn\Helper\Data">
        <arguments>
            <argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session</argument>
        </arguments>
    </virtualType>
</config>
Source Link
ravichandra
  • 384
  • 1
  • 4
  • 13

Anything wrong in below code, im trying pass argument from di.xml to class

I'm trying to understand virtual types and how we can use them in practical. Referred some code from vendor, still its giving me an error when im trying to access in helper class.

namespace Ravi\Learn\Helper;

use Magento\Framework\App\Helper\Context;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;
    public function __construct(Context $context, $customerSession)
    {
        parent::__construct($context);
        $this->customerSession = $customerSession;
    }

    public function getSess(){
       return $this->customerSession->getId();
    }
}






<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <virtualType name="Ravi\Learn\Helper\Data\Session" type="Ravi\Learn\Helper\Data">
        <arguments>
            <argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session</argument>
        </arguments>
    </virtualType>
</config>