2

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"}
1
  • Check this line ´$resultPage->getConfig()->addBodyClass($additional_class);´.
    – kunj
    Jun 14, 2018 at 4:15

4 Answers 4

5

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.

1
  • Thank you. You are a life saver!
    – Thowzif
    May 1, 2020 at 13:17
1

Add this line in your method . And make bin/magento c:c

...

public function execute()
{
    $resultPage = $this->resultFactory->create();
    //Code ...
}
0

You need to add this code :

$resultPage = $this->resultFactory->create();

Before this code:

$resultPage->getConfig()->addBodyClass($additional_class);
0

This problem seems to be within the Porto Theme. I've installed the latest version.

I did what Kunj had propossed and added $resultPage = $this->resultFactory->create(); before the line $resultPage->getConfig()->addBodyClass($additional_class);

So we get this:

$resultPage = $this->resultFactory->create(); 
$resultPage->getConfig()->addBodyClass($additional_class);

But I'm getting this error now:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Magento\Framework\Controller\ResultFactory::create(), 
0 passed in /data/web/public/app/code/Smartwave/Porto/Controller/CatalogSearch/Result/Index.php on line 100 and at least 1 expected 

in /data/web/public/vendor/magento/framework/Controller/ResultFactory.php:81 Stack trace: #0 /data/web/public/app/code/Smartwave/Porto/Controller/CatalogSearch/Result/Index.php(100): Magento\Framework\Controller\ResultFactory->create() #1 /data/web/public/generated/code/Smartwave/Porto/Controller/CatalogSearch/Result/Index/Interceptor.php(24): Smartwave\Porto\Controller\CatalogSearch\Result\Index->execute() #2 /data/web/public/vendor/magento/framework/App/Action/Action.php(107): Smartwave\Porto\Controller\CatalogSearch\Result\Index\Interceptor->execute() #3 /data/web/public/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Framework\App\Action\Action->dispatch(Object(Magento\Framework\App\Request\Http))#4 /data/web/public/vendor/magento/framework/Inte in /data/web/public/vendor/magento/framework/Controller/ResultFactory.php on line 81

What am I doing wrong?

Br,

Steve

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.