Magento – Magento 2.x CatalogSearch Undefined Variable($resultPage)

catalogcatalogsearchmagento2undefined variable

Here is my code. My finding is-
This is the URL for Parent Class(
class Index extends \Magento\CatalogSearch\Controller\Result\Index)
but unfortunately there is no $resultPage defined in this class.
so we must make this $resultPage as per direction of this URL. May be we can not send PageFactory as a Parameter to the Constructor and therefore this is the ideal place to look into this URL.

<?php
/**
 *
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Smartwave\Porto\Controller\CatalogSearch\Result;

use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResourceConnection;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Search\Model\QueryFactory;
use Magento\Framework\Controller\ResultFactory;

class Index extends \Magento\CatalogSearch\Controller\Result\Index
{
    /**
     * Catalog session
     *
     * @var Session
     */
    protected $_catalogSession;

    /**
     * @var StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * @var QueryFactory
     */
    private $_queryFactory;

    /**
     * Catalog Layer Resolver
     *
     * @var Resolver
     */
    private $layerResolver;

    protected $_scopeConfig;

    /**
     * @param Context $context
     * @param Session $catalogSession
     * @param StoreManagerInterface $storeManager
     * @param QueryFactory $queryFactory
     * @param Resolver $layerResolver
     */
    public function __construct(
        Context $context,
        Session $catalogSession,
        StoreManagerInterface $storeManager,
        QueryFactory $queryFactory,
        Resolver $layerResolver,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->_catalogSession = $catalogSession;
        $this->_storeManager = $storeManager;
        $this->_queryFactory = $queryFactory;
        $this->layerResolver = $layerResolver;
        parent::__construct($context, $catalogSession, $storeManager, $queryFactory, $layerResolver);
        $this->resultFactory = $context->getResultFactory();
        $this->_scopeConfig = $scopeConfig;
    }

    /**
     * Display search result
     *
     * @return void
     */
    public function execute()
    {
        $this->layerResolver->create(Resolver::CATALOG_LAYER_SEARCH);
        /* @var $query \Magento\Search\Model\Query */
        $query = $this->_queryFactory->get();

        $query->setStoreId($this->_storeManager->getStore()->getId());

        if ($query->getQueryText() != '') {
            if ($this->_objectManager->get('Magento\CatalogSearch\Helper\Data')->isMinQueryLength()) {
                $query->setId(0)->setIsActive(1)->setIsProcessed(1);
            } else {
                $query->saveIncrementalPopularity();

                if ($query->getRedirect()) {
                    $this->getResponse()->setRedirect($query->getRedirect());
                    return;
                }
            }

            $this->_objectManager->get('Magento\CatalogSearch\Helper\Data')->checkNotes();

            $full_width = $this->_scopeConfig->getValue('porto_settings/general/layout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->_storeManager->getStore()->getId());
            $additional_class = '';
            if(isset($full_width) && $full_width == 'full_width')
                $additional_class = 'layout-fullwidth';
            $resultPage->getConfig()->addBodyClass($additional_class);
            $panelLayout = $this->_scopeConfig->getValue('porto_settings/category/page_layout', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $this->_storeManager->getStore()->getId());
            if($panelLayout!=''){
                $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
                $resultPage->getConfig()->setPageLayout($panelLayout);
                return $resultPage;
            }else{
                $this->_view->loadLayout();
                $this->_view->renderLayout();
            }

        } else {
            $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl());
        }
    }
}

And the error is-

    {"0":"Notice: Undefined variable: resultPage in \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/app\/code\/Smartwave\/Porto\/Controller\/CatalogSearch\/Result\/Index.php on line 100","1":"#0 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/app\/code\/Smartwave\/Porto\/Controller\/CatalogSearch\/Result\/Index.php(100): Magento\\Framework\\App\\ErrorHandler->handler(8, 'Undefined varia...', '\/Applications\/M...', 100, Array)\n#1 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/framework\/App\/Action\/Action.php(107): Smartwave\\Porto\\Controller\\CatalogSearch\\Result\\Index->execute()\n#2 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/framework\/App\/FrontController.php(55): Magento\\Framework\\App\\Action\\Action->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#3 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/framework\/Interception\/Interceptor.php(58): Magento\\Framework\\App\\FrontController->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#4 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/framework\/Interception\/Interceptor.php(138): Magento\\Framework\\App\\FrontController\\Interceptor->___callParent('dispatch', Array)\n#5 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/module-store\/App\/FrontController\/Plugin\/RequestPreprocessor.php(94): Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#6 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/framework\/Interception\/Interceptor.php(135): Magento\\Store\\App\\FrontController\\Plugin\\RequestPreprocessor->aroundDispatch(Object(Magento\\Framework\\App\\FrontController\\Interceptor), Object(Closure), Object(Magento\\Framework\\App\\Request\\Http))\n#7 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/module-page-cache\/Model\/App\/FrontController\/BuiltinPlugin.php(73): Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#8 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/framework\/Interception\/Interceptor.php(135): Magento\\PageCache\\Model\\App\\FrontController\\BuiltinPlugin->aroundDispatch(Object(Magento\\Framework\\App\\FrontController\\Interceptor), Object(Closure), Object(Magento\\Framework\\App\\Request\\Http))\n#9 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/framework\/Interception\/Interceptor.php(153): Magento\\Framework\\App\\FrontController\\Interceptor->Magento\\Framework\\Interception\\{closure}(Object(Magento\\Framework\\App\\Request\\Http))\n#10 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/generated\/code\/Magento\/Framework\/App\/FrontController\/Interceptor.php(26): Magento\\Framework\\App\\FrontController\\Interceptor->___callPlugins('dispatch', Array, NULL)\n#11 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/framework\/App\/Http.php(135): Magento\\Framework\\App\\FrontController\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#12 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/vendor\/magento\/framework\/App\/Bootstrap.php(256): Magento\\Framework\\App\\Http->launch()\n#13 \/Applications\/MAMP\/htdocs\/Magento_2.2.4\/index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http\\Interceptor))\n#14 {main}","url":"\/Magento_2.2.4\/catalogsearch\/result\/?q=porsche+911","script_name":"\/Magento_2.2.4\/index.php"}

Best Answer

Try adding this to the beggining of your execute method

$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);

I would also set the visibility of your variable in the class but that is up to you.

Related Topic