1

When i edit in constructor file of my custom module it give me this error " Type Error occurred when creating object " i don't know what i am doing wrong i use same code for creating a Block and its working.

My Controller code is :

<?php
namespace Dealers\Validation\Controller\Valuation;

use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{
protected $_customerSession;
     
public function __construct(
    \Magento\Customer\Model\SessionFactory $customerSession,
    array $userdata = []
) {
    $this->_customerSession = $customerSession->create();   
    parent::__construct($context, $userdata);
}

public function execute()
{
    /*if(!$this->_customerSession->isLoggedIn()) {
        $this->redirect->redirect($controller->getResponse(), 'customer/account/login');
    }*/
    $this->_view->loadLayout(); 
    $this->_view->renderLayout(); 
}
}

I want to redirect it to login page if user is not logged in.

enter image description here

0

2 Answers 2

2

This is my working code, you can follow this

protected $customerSession;
protected $urlInterface;

public function __construct(
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Framework\UrlInterface $urlInterface,
    \Magento\Framework\App\Action\Context $context
) {
    $this->urlInterface = $urlInterface;
    $this->customerSession = $customerSession; 
    parent::__construct($context);
}

public function execute()
{
    //Redirect to login page if customer is not logged in
    if(!$this->customerSession->isLoggedIn()) {
        $this->messageManager->addErrorMessage("You must be logged in to add/update product");
        $this->customerSession->setAfterAuthUrl($this->urlInterface->getCurrentUrl());
        $this->customerSession->authenticate();
        return;
    }
    $this->_view->loadLayout(); 
    $this->_view->renderLayout(); 
}

This will check if customer is logged in, it will redirect to login otherwise. It also sets return URL so that after login customer will redirect back to current URL

Edit: Context error fixed

11
  • Thanks for the help but still i have found the same error " Type Error occurred when creating object "
    – Prits
    Commented Jul 8, 2020 at 6:10
  • If you have used parent::__construct($context, $userdata); then just remove $userdata from your constructor and just use parent::__construct($context); Commented Jul 8, 2020 at 6:34
  • Will you share your full controler file @Shoaib
    – Prits
    Commented Jul 8, 2020 at 6:47
  • It give me this error " Uncaught Error: Call to undefined method Magento\Customer\Model\SessionFactory::isLoggedIn() "
    – Prits
    Commented Jul 8, 2020 at 7:02
  • 1
    Done Accepted your answer I have asked seprate question stackoverflow.com/questions/62810173/… Please look into it @Shoaib
    – Prits
    Commented Jul 9, 2020 at 8:05
2

Try to update below content in your file

<?php
namespace Dealers\Validation\Controller\Valuation;

use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_customerSession;
        
    public function __construct(
        Context $context,
        \Magento\Customer\Model\SessionFactory $customerSession
    ) {
        $this->_customerSession = $customerSession->create();   
        parent::__construct($context);
    }

    public function execute()
    {
        if(!$this->_customerSession->isLoggedIn()) {
            return $this->resultRedirectFactory->create()->setPath('customer/account/login', ['_current' => true]);
        }
        $this->_view->loadLayout(); 
        $this->_view->renderLayout(); 
    }
}

You missed \Magento\Framework\App\Action\Context $context this in your construct.

After changing above content try to run below commands once.

php bin/magento setup:upgrade
php bin/magento setup:di:compile

We don't need to pass userData array to parent construct, because if you'll open below file.

vendor/magento/framework/App/Action/Action.php

There you can see we need to pass only single arg to our parent construct and it's context. So just remove second arg and try to compile again.

Hope this will help you!

10
  • @Thanks for the help Kishan added screenshot after di:compile command gave me that error
    – Prits
    Commented Jul 8, 2020 at 6:16
  • Answer updated. Please check @Prits Commented Jul 8, 2020 at 6:21
  • Thanks it worked @Kishan If i want to redirect it to login page if not logged in then it would done before execute right!
    – Prits
    Commented Jul 8, 2020 at 6:45
  • It give me this error " Uncaught Error: Call to undefined method Magento\Customer\Model\SessionFactory::isLoggedIn() "
    – Prits
    Commented Jul 8, 2020 at 7:18
  • 1
    @Prits, I have updated my answer, please update code and let me know if it helps. Thanks. Commented Jul 8, 2020 at 8:18

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.