I want to redirect customer to login page in my controller without using observer. here is my code

 protected $urlInterface;

    public function __construct(

        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\Session $customerSession,
        \Magenticians\Evaluation\Model\EvaluationFactory $EvaluationFactory,
        \Magenticians\Evaluation\Model\Evaluation $eval,
        \Magenticians\Evaluation\Model\ResourceModel\Evaluation\Collection $evaluationCollection,
        \Magento\Framework\Controller\ResultFactory $result)
    {
        parent::__construct(
            $context
        );
        $this->EvaluationFactory = $EvaluationFactory;
        $this->_customerSession = $customerSession;
        $this->_storeManager = $storeManager;
        $this->resultRedirect = $result;
        $this->_resource = $resource;
        $this->_evaluation =$eval;
        $this->_evaluationCollection = $evaluationCollection;
        $this->redirect = $redirect;
        $this->urlInterface = $url;


    }



        // Create login URL
        $login_url = $this->urlInterface
            ->getUrl('customer/account/login');

                $resultRedirect = $this->resultRedirectFactory->create();
                $resultRedirect->setUrl($login_url);
 $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();

but as result i have no redirection response please help me and thanks in advance

up vote 1 down vote accepted

Your code should be looking like something below, your controller is missing the execute() method which is the entry point for the controller action in Magento 2 and return statement which returns the redirection url. Your code should look like something below:

<?php

namespace YourNameSpace;

class YourClassName{

    protected $urlInterface;

    public function __construct(

        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\Session $customerSession,
        \Magenticians\Evaluation\Model\EvaluationFactory $EvaluationFactory,
        \Magenticians\Evaluation\Model\Evaluation $eval,
        \Magenticians\Evaluation\Model\ResourceModel\Evaluation\Collection $evaluationCollection,
        \Magento\Framework\Controller\ResultFactory $result)
    {
        parent::__construct(
            $context
        );
        $this->EvaluationFactory = $EvaluationFactory;
        $this->_customerSession = $customerSession;
        $this->_storeManager = $storeManager;
        $this->resultRedirect = $result;
        $this->_resource = $resource;
        $this->_evaluation =$eval;
        $this->_evaluationCollection = $evaluationCollection;
        $this->redirect = $redirect;
        $this->urlInterface = $url;


    }


    public function execute()
    {
        // Create login URL
        $login_url = $this->urlInterface
            ->getUrl('customer/account/login');
     return $this->resultRedirectFactory->create()->setPath($login_url);

    }
}

?>

Try something like this

return $this->resultRedirectFactory->create()->setPath('customer/account/login');

You need to return the ResultRedirect object inside your execute() method

You have to redirection from execute(), in your code . You did not build Action class properly.

And your code it is missing this function.

public function execute()
{

 return $this->resultRedirectFactory->create()->setPath('customer/account/login');
}

Use below code

<?php
namespace Venor\Module\Controller\Index;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Response\Http;
use Magento\Framework\UrlInterface;

class Index extends \Magento\Framework\App\Action\Action
{
   public function __construct(

    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Response\RedirectInterface $redirect,
    \Magento\Framework\UrlInterface $url,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Customer\Model\Session $customerSession,
    \Magenticians\Evaluation\Model\EvaluationFactory $EvaluationFactory,
    \Magenticians\Evaluation\Model\Evaluation $eval,
    \Magenticians\Evaluation\Model\ResourceModel\Evaluation\Collection $evaluationCollection,
    \Magento\Framework\Controller\ResultFactory $result,
    \Magento\Framework\View\Result\PageFactory $pageFactory,
    \Magento\Framework\App\Response\Http $http

    )
{
    parent::__construct(
        $context
    );
    $this->EvaluationFactory = $EvaluationFactory;
    $this->_customerSession = $customerSession;
    $this->_storeManager = $storeManager;
    $this->resultRedirect = $result;
    $this->_resource = $resource;
    $this->_evaluation =$eval;
    $this->_evaluationCollection = $evaluationCollection;
    $this->redirect = $redirect;
    $this->urlInterface = $url;
    $this->_pageFactory = $pageFactory;
    $this->_http = $http;


}

public function execute(){

     $login_url = $this->urlInterface->getUrl('customer/account/login');
     $this->_http->setRedirect($login_url, 301);
     $pageFactory = $this->_pageFactory->create();
     return $pageFactory;
}

}
  • <b>Fatal error</b>: Uncaught Error: Call to a member function setRedirect() on null in /var/www/html/sajjada/app/code/Magenticians/Evaluation/Controller/Index/Index.php:123 – Developper Magento Sep 14 at 10:38
  • did you do use Magento\Framework\UrlInterface; in top and Http $http, in construnction and $this->_http = $http; – Ansar Husain Sep 14 at 10:42
  • sorry i forget to add $this->_http = $http; but even added this line there is no redirection to login page but error is gone so what s the problem can you please help me – Developper Magento Sep 14 at 10:45
  • Check post now i have updated code using your code in my answer use as it is – Ansar Husain Sep 14 at 10:58

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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