0

I created an observer to catch the event when a new url is requested with

<event name="controller_front_send_response_before">
    <observer name="brands_index_events_observer" instance="WiserobotT\ManufacturerinfoT\Observer\BrandObserver" />
</event>

I check the Url if it matches abc/xyz.html, then set routeName, ControllerName and ActionName:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        //get request object
$requestOb = $objectManager->get('Magento\Framework\App\Request\Http');
$requestOb->setRouteName('catalogsearch')
    ->setControllerName('advanced')
    ->setActionName('result');

I use var_dump($requestOb->getFullActionName()); and a get

string(29) `catalogsearch_advanced_result`

Good! But the page still shows: The page you requested was not found...

I want the page to have content like the catalogsearch/advanced/result page but still keep the old url.

0

Try following way


<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="controller_front_send_response_before">
        <observer name="custom_controller_front_send_response_before" instance="[Vendor]\[Module]\Observer\ResponseObserver"/>
    </event>
</config>


namespace [Vendor]\[Module]\Observer;

use Magento\Framework\Event\ObserverInterface;

class ResponseObserver implements ObserverInterface
{
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlBuilder;
    /**
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param \Magento\Framework\UrlInterface $urlManager
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\Framework\UrlInterface $urlBuilder
    ) {
        $this->objectManager = $objectManager;
        $this->urlBuilder = $urlBuilder;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $objectManager = $this->objectManager;
        $requestObj = $objectManager->get('Magento\Framework\App\Request\Http');
        $responseObj = $objectManager->get('Magento\Framework\App\Response\Http');
        // check your condition here
        if($requestObj->getFullActionName() == 'cms_index_index') {
            $url = $this->urlBuilder->getUrl('catalogsearch/advanced/index');
            $responseObj->setRedirect($url)
                ->sendResponse();
        }
    }
}

| improve this answer | |
  • thanks!! but the url also change to new url, but a need to keep my own url – Hoang Huynh Nhat Apr 8 '16 at 2:53
  • when catch enable than this not work in Magento2.2.2. – Dhaduk Mitesh Feb 22 '18 at 13:55
0

You are using the wrong event. When controller_front_send_response_before is triggered, the request object already has been processed and the complete response HTML (with "The page you requested was not found") generated.

You could use the controller_action_predispatch event instead. This also happens when the responsible controller already has been determined, but it's not executed yet and you can trigger the route matching again with:

$request->setDispatched(false);
| improve this answer | |
  • Thank you so much with that event. It works but the page is mess up with 2 content, one from catalog and one from 404, the title is 404 page. When I put $request->setDispatched(false); I got error Error 503 Service Unavailable Service Unavailable Guru Meditation: XID: 66280584 Varnish cache server – Hoang Huynh Nhat Apr 8 '16 at 4:35
  • I see, the problem is that the "noroute" action (404) behaves differently than others. The correct approach for your problem would be to create a custom router instead of using an observer. It is explained in detail here: inchoo.net/magento-2/routing-in-magento-2 – Fabian Schmengler Apr 8 '16 at 7:03
  • thank you, I think You just show me the right way, but I got error Front controller reached 100 router match iterations > when I try to catch in foreach loop: if(strpos($identifier, $brandprefix) !== false) { foreach ($brandCollection as $key => $_manu) { if(strpos($identifier, $_manu->getData('url_key')) !== false ) { /* * We must set module, controller path and action name for our controller class(Controller/Test/Test.php) */ $request->setModuleName('catalogsearch')->setControllerName('advanced')->setActionName('index'); } } } – Hoang Huynh Nhat Apr 8 '16 at 10:49
0

Finally to solved my problem. Thanks God. hahaha after use this to set controllerName,RouteName, actionName, and params

$request->setModuleName('catalogsearch')->setControllerName('advanced')->setActionName('result')->setParam('manufacturer', $_manu->getData('option_id'));

a found an error with this code in controller Magento\CatalogSearch\Controller\Advanced\Result

$this->_catalogSearchAdvanced->addFilters($this->getRequest()->getQueryValue()); I rewrite this controller and fix this code to $this->_catalogSearchAdvanced->addFilters($this->getRequest()->getParams()); DONE. It's work perfect.

| improve this answer | |

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.