2

I have create a module and its working fine when compilation is disabled. Its getting below error when after compilation run.

PHP Fatal error: Uncaught TypeError: Argument 1 passed to Magento\Customer\Model\Customer::__construct() must be an instance of Magento\Framework\Model\Context, instance of Magento\Framework\ObjectManager\ObjectManager given,

<?php

namespace Supravat\Customer\Helper;

class Customer extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $customerRepositoryInterface;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
    ) 
    {
        ...........
        parent::__construct($context);
        $this->customerRepositoryInterface = $customerRepositoryInterface;
    }

    public function getCustomerById() {
        $customer_id  = 1; // 1 is the magento customer id
        $customer = $this->customerRepositoryInterface->getById($customer_id);
        var_dump($customer->getData());
    }
}

Note: Compilation is generates code and dependency injection configuration successfully.

  • If it works fine, why do we need to compile it ? – Toan Tam Jul 25 '17 at 16:18
  • after compile its not working. but compilation was successful. – Supravat M Jul 25 '17 at 16:19
  • Yeah, i know. I have the same situation like you before. And after researching, the solution is let it be, don't run di:setup:compile. – Toan Tam Jul 25 '17 at 16:22
  • Its should be work with compilation. – Supravat M Jul 25 '17 at 16:24
  • If you cannot fix that bugs, remove the var/di folder and that will be good to go. – Toan Tam Jul 25 '17 at 16:29
2

Tested on 2.1.6 and 2.1.8 works just fine. I injected the helper into a footer copyright plugin for proof of concept.

File Structure

app/code/Supravat/
└── Customer
    ├── Helper
    │   └── Customer.php
    ├── Plugin
    │   └── Magento
    │       └── Theme
    │           └── Block
    │               └── Html
    │                   └── Footer.php
    ├── etc
    │   ├── di.xml
    │   └── module.xml
    └── registration.php

registration.php

<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Supravat_Customer',
        __DIR__
    );

module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Supravat_Customer" setup_version="0.0.1"/>
</config>

di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Theme\Block\Html\Footer">
        <plugin name="supravat_customer_magento_theme_block_html_footer" type="Supravat\Customer\Plugin\Magento\Theme\Block\Html\Footer"/>
    </type>
</config>

Customer.php

<?php

namespace Supravat\Customer\Helper;

class Customer extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $customerRepositoryInterface;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
    ) {
        parent::__construct($context);
        $this->customerRepositoryInterface = $customerRepositoryInterface;
    }

    public function getCustomerById($customerId)
    {
        $customer = $this->customerRepositoryInterface->getById($customerId);
        return var_export($customer->__toArray(), true);
    }
}

Footer.php

<?php

namespace Supravat\Customer\Plugin\Magento\Theme\Block\Html;

use Supravat\Customer\Helper\Customer as Helper;

class Footer
{
    protected $helper;
    public function __construct(Helper $helper)
    {
        $this->helper = $helper;
    }

    public function afterGetCopyright(
        \Magento\Theme\Block\Html\Footer $subject,
        $result
    ) {
        return $this->helper->getCustomerById(1);
    }
}

Once all files are in place:

./bin/magento --clear-static-content module:enable Supravat_Customer 
./bin/magento setup:upgrade
./bin/magento setup:di:compile

Refresh the home page and you should see the customer's information in the footer.

This is obviously just a proof of concept, there isn't any good reason why you'd want the customer's information dumped in the footer copyright, but it shows you that it works out of the box.

Footer Copyright plugin

3

The \Magento\Customer\Api\CustomerRepositoryInterface not injected properly. It should be as shown below:

Injection patter is wrong.You should inject class as parameter of __construct().


<?php

namespace Supravat\Customer\Helper;

class Customer extends \Magento\Framework\App\Helper\AbstractHelper
{
  protected $customerRepositoryInterface;

  public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
) 
{
    ...........
    parent::__construct($context);
    $this->customerRepositoryInterface=$customerRepositoryInterface;

}

public function getCustomerById() {
    $customer_id  = 1; // 1 is the magento customer id
    $customer = $this->customerRepositoryInterface->getById($customer_id);
    var_dump($customer->getData());
}
}
  • why CustomerRepositoryInterface not inject properly? Look at Best way to load Customer by Id in Magento 2.1 – Supravat M Jul 26 '17 at 5:19
  • That was ok @supravat. Please compare the code given in the link and your helper class. You will find the answer of why – Prasanta Hatui Jul 26 '17 at 9:25
  • Did you compile & run this code? – Supravat M Jul 26 '17 at 9:36
  • Did you tried my solution? – Prasanta Hatui Jul 26 '17 at 9:38
  • Apology for misplaced of my code. I have update my question. Please check once. – Supravat M Jul 26 '17 at 9:42

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.