0

I'm a beginner at PHP and Magento have developed a Magento2 extension and used this line code in it:

require_once '/../knownuserv3/KnownUser.php';

But when I tried to put my extension in Magento Store, I got a series of test failure from Code Sniffer, which I solved all but for this one couldn't find any solution until now. knownuser is a class in another library includes a static method which I need to call it. this is the error text from Code Sniffer: enter image description here

4
  • 4
    Possible duplicate of How to use require_once in magento 2
    – Mohit Rane
    Sep 5, 2019 at 13:02
  • No, it didn't work, I tried to use object manager and dependency injection but still I needed to use require_once to load the related class. I don't understand why shouldn't we use require_once and what is the replacement for it?!
    – S.Cheginy
    Sep 9, 2019 at 9:41
  • have you tried using use vendor\module\classname; ?
    – Mohit Rane
    Sep 9, 2019 at 9:57
  • Yes, I tried this as well. but the problem is that the class that I'm using it is a third party library so it seems it doesn't load before using require!
    – S.Cheginy
    Sep 9, 2019 at 10:06

1 Answer 1

0

Try This Code

<?php

namespace VendoreName\ModuleName\Controller\Index;

use Magento\Framework\App\Action\Action;
use Zend_Loader;

class GetPdfData extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var \Magento\Framework\Filesystem\DirectoryList
     */
    protected $dir;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\Filesystem\DirectoryList $dir
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Filesystem\DirectoryList $dir,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->dir = $dir;
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
        $this->initializePDF();
    }

    /**
     * Construct initialization of mpdf library
     */
    protected function initializePDF()
    {
        error_reporting(0);

        $path = $this->dir->getRoot() . "/vendor/mpdf/mpdf/src/";

        $this->pdf = $path . 'Mpdf.php';

        //include_once $this->pdf;
        Zend_Loader::loadFile($this->pdf, null, true);

        $this->configvariables = $path . 'Config/ConfigVariables.php';

        // require_once $this->configvariables;
        Zend_Loader::loadFile($this->configvariables, null, true);
    }

    /**
     * @inheritdoc
     */
    public function execute()
    {
        echo "Your Code";
        exit();
    }
}

Note: Here we need to add absolute path of files which we want to include_once or require_once. MPdf is installing via composer require mpdf/mpdf command and this module placed into the vendor directory.

Your Answer

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

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