0

I implemented redirection for users when some condition become true. My code

<?php

namespace send\sms\Observer;

use Magento\Framework\Registry;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Message\ManagerInterface;


class check implements ObserverInterface
{

    protected $redirect;
    protected $_customerSession;
    protected $_objectManager;
    protected $_urlInterface;


    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Framework\UrlInterface $urlInterface
    ) {

        $this->_customerSession = $customerSession;
        $this->redirect = $redirect;
        $this->_objectManager = $objectManager;
        $this->_urlInterface = $urlInterface;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $actionName = $observer->getEvent()->getRequest()->getFullActionName();
        $controller = $observer->getControllerAction();

        $obm = \Magento\Framework\App\ObjectManager::getInstance();
    /** @var \Magento\Framework\App\Http\Context $context */
    $context = $obm->get('Magento\Framework\App\Http\Context');
    /** @var bool $isLoggedIn */
    $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
    if($isLoggedIn){ 
      $om = \Magento\Framework\App\ObjectManager::getInstance();
           $customerSession = $om->create('Magento\Customer\Model\Session');
           $suser_id = $customerSession->getCustomer()->getId();
    $this->_resources = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection');   
    $connection= $this->_resources->getConnection();
    $tableName = $this->_resources->getTableName('otpp');
    $sql = "SELECT verified FROM otpp WHERE `entity_id`='$suser_id'";
    $result = $connection->fetchall($sql);
    @$otparray = $result['0']; 
    $verified = $otparray['verified'];



        if($verified == 0) {
            $url = $this->_urlInterface->getUrl('otp/index/index'); // give here your controller/action
        // below code redirects to cart controller
        $observer->getControllerAction()
                    ->getResponse()
                    ->setRedirect($url);
        }

    }
    }

}
  • add exist into your code – Sunil Patel Nov 24 '17 at 12:09
  • @Ramesh KR you should accept the answer which helped you if your problem is solved – Vivek Kumar Nov 25 '17 at 6:29
1

Try this

  <?php

    namespace send\sms\Observer;

    use Magento\Customer\Model\Session;
    use Magento\Framework\App\ObjectManager;
    use Magento\Framework\App\Response\RedirectInterface;
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\ObjectManagerInterface;
    use Magento\Framework\UrlInterface;


    class Check implements ObserverInterface
    {

        protected $redirect;
        protected $_customerSession;
        protected $_objectManager;
        protected $_urlInterface;


        public function __construct(
            Session $customerSession,
            RedirectInterface $redirect,
            ObjectManagerInterface $objectManager,
            UrlInterface $urlInterface
        ) {

            $this->_customerSession = $customerSession;
            $this->redirect = $redirect;
            $this->_objectManager = $objectManager;
            $this->_urlInterface = $urlInterface;
        }

        public function execute(Observer $observer)
        {
            if ($this->_customerSession->isLoggedIn()) {
                $suser_id = $this->_customerSession->getCustomer()->getId();
                $resources = ObjectManager::getInstance()->get('Magento\Framework\App\ResourceConnection');
                $connection = $resources->getConnection();
                $tableName = $resources->getTableName('otpp');
                $sql = "SELECT verified FROM $tableName WHERE `entity_id`='$suser_id'";
                $result = $connection->fetchall($sql);
                if(count($result) > 0) {
                    $verified = $result['0']['verified'];
                    if ($verified == 0) {
                        $url = $this->_urlInterface->getUrl('otp/index/index'); // give here your controller/action
                        // below code redirects to cart controller
                        $observer->getControllerAction()
                            ->getResponse()
                            ->setRedirect($url);
                    }
                }
            }
        }

    }
1

Kindly use following ;

<?php
namespace [Vendor]\[modulename]\Observer;
use \Magento\Framework\Event\Observer;
use \Magento\Framework\Event\ObserverInterface;
class [YourClass] implements ObserverInterface {
    protected $_responseFactory;
    protected $_redirect;
    protected $_url;
    public function __construct(
        ......
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\Http $redirect,
        ......
    ) {
        $this->_responseFactory = $responseFactory;
        $this->_url = $url;
        $this->_redirect = $redirect;

    }
    public function execute(Observer $observer) {

             $CustomRedirectionUrl = $this->_url->getUrl('otp/index/index');
             $this->_redirect->setRedirect($CustomRedirectionUrl);

    }
}
  • Problem in redirecting issue is oocurred – Ramesh KR Nov 24 '17 at 13:47
  • Are you getting any error or its just not redirecting? – Vivek Kumar Nov 24 '17 at 14:36
1

$url = $this->getResponse()->setRedirect('otp/index/index');

or use this also

$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); return $resultRedirect->setUrl('otp/index/index'');

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.