0

how to get the module file system path in magento2 for the below?

 require_once(Mage::getModuleDir('controllers','Mage_CatalogSearch').DS.'ResultController.php');
 abstract class Result extends Mage_CatalogSearch_ResultController
 {
    ...
 }

How do I write the same in magento2 ?

What is the equivalent class path in magento 2

1

To answer you question....
You need to inject in your class and instance of \Magento\Framework\Module\Dir\Reader like this:

protected $moduleReader;
public function __construct(
    ...
    \Magento\Framework\Module\Dir\Reader $moduleReader,
    ....
) {
    ....
    $this->moduleReader = $moduleReader;
    ....
}

Then you can use this :

 $this->moduleReader->getModuleDir('Controller', 'Magento_CatalogSearch');  

Now to probably will solve your problem.
In Magento 2 you don't need to include the file of the original controller in order to extend it.
The controllers follow the same autoloader rules.
You can simply extend the controller you need and the autoloader will find the class.

0

You can simply call use keyword with full namespace.

use Magento\Framework\View\Result\PageFactory;

And use of that class function using __construct method,

class Test{
    protected $resultPageFactory;
    public function __construct(
            PageFactory $resultPageFactory
        ) {
            $this->resultForwardFactory = $resultForwardFactory;
        }
    public function getanotherClass(){
         $this->resultForwardFactory->getFunctionfromFile();
    }    

}

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.