1

I have added a front phtml file followed by a Block, I need to redirect to that block from a controller.

this is the Block path, it's working fine.

http://mywebsite.co/schools/schoolzone_search/index/display

Here is my controller

<?php
namespace Vendor\Module\Controller\Index;
class Search extends \Magento\Framework\App\Action\Action
{
protected $resultRedirectFactory;
public function __construct(\Magento\Framework\Controller\Result\Redirect $resultRedirectFactory)
{
$this->resultRedirectFactory = $resultRedirectFactory;
}
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$url ="schoolzone_search/index/display";    
$url_2 = "http://mywebsite.co/schools/schoolzone_search/index/display";            
return $resultRedirect->setPath($url);
}
}

in Network it's showing as 200 OK, But it is not redirecting to the Phtml Page. Where am doing wrong? Can I get help?. Thank you in advance

2
    <?php
    namespace Vendor\Module\Controller\Index;
    class Search extends \Magento\Framework\App\Action\Action
    {
    protected $resultRedirectFactory;
    public function __construct(\Magento\Framework\Controller\Result\Redirect $resultRedirectFactory)
    {
    $this->resultRedirectFactory = $resultRedirectFactory;
    }
    public function execute()
    {
    $resultRedirect = $this->resultRedirectFactory->create();
    $url ="schoolzone_search/index/display";    
    $url_2 = "http://mywebsite.co/schools/schoolzone_search/index/display";            
    return $resultRedirect->setPath($url);
   $resultRedirect->setUrl('/frontname/controller/action'); //add your redirect url here or you can simply paste a page url here
    }
    }
| improve this answer | |
  • Thank you @Chikku for answering. I updated code as $resultRedirect->setPath('/schoolzone_search/index/display'); instead of $resultRedirect->setPath($url); but its not working – Praveen Negimani Aug 31 at 8:09
1

Try this code

  <?php
    namespace Vendor\Module\Controller\Index;
    
    use Magento\Framework\Controller\ResultFactory;
    
    class Search extends \Magento\Framework\App\Action\Action
    {
        protected $resultRedirectFactory;
        public $storeManager;
    
        public function __construct(
         \Magento\Store\Model\StoreManagerInterface $storeManager
        )
        {
            $this->resultRedirectFactory = $resultRedirectFactory;
            $this->storeManager=$storeManager;
        }
        public function execute()
        {
            //$resultRedirect = $this->resultRedirectFactory->create();
             $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            //$url ="schoolzone_search/index/display";    
            //$url_2 = "http://mywebsite.co/schools/schoolzone_search/index/display";
            $url = $this->storeManager->getStore()->getUrl('schools/schoolzone_search/index/display', ['_current' => true]);
            return $resultRedirect->setPath($url);
        }
    }
| 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.