0

di.xml

<type name="Magento\Framework\AppInterface">
  <plugin sortOrder="1" name="noroute_redirect" type="Namespace\ModuleName\Plugin\AppInterfacePlugin"/>
</type>

AppInterfacePlugin.php

<?php


namespace Namespace\ModuleName\Plugin;

use Magento\Framework\App\State;
use Magento\Framework\AppInterface;
use Magento\Framework\Controller\Result\RedirectFactory;

class AppInterfacePlugin
{

    private $state;
    private $redirectFactory;

    public function __construct(
        State $state,
        RedirectFactory $redirectFactory
    ) {
        $this->state = $state;
        $this->redirectFactory = $redirectFactory;
    }



    public function aroundLaunch(AppInterface $subject, \Closure $proceed)
    {
        if (condition == true) {
            $this->state->setAreaCode('frontend');
            return $this->redirectFactory->create()->setPath('no-route');
            //exit(0);
        }

        return $proceed();
    }
}
  • How do I redirect user to 404 page not found page?
0

Try following code;

<?php


namespace Namespace\ModuleName\Plugin;

use Magento\Framework\App\State;
use Magento\Framework\AppInterface;
use Magento\Framework\Controller\Result\RedirectFactory;

class AppInterfacePlugin
{

    private $state;
    private $redirect;
    private $url; 

    public function __construct(
        State $state,
        \Magento\Framework\UrlInterface $url,
        \Magento\Framework\App\Response\Http $redirect
    ) {
        $this->url = $url;
        $this->state = $state;
        $this->redirect = $redirect;
    }



    public function aroundLaunch(AppInterface $subject, \Closure $proceed)
    {
        if (condition == true) {
            $this->state->setAreaCode('frontend');
            $redirectUrl = $this->url->getUrl('no-route/index/index');
            $this->redirect->setRedirect($redirectUrl);
            //exit(0);
        }

        return $proceed();
    }
}
  • Exception - Exception #0 (Magento\Framework\Exception\SessionException): Area code not set: Area code must be set before starting a session. Exception #1 (Magento\Framework\Exception\LocalizedException): Area code is not set – Slimshadddyyy Jan 10 '19 at 18:08

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.