0

How do we include_once an external (outside of Magento 2 code but on the same web server) PHP file?

I added the events.xml and adminuserlogin.php files below, into an already activated and fucntioning admin module, but my attempts to have the Observer call/include the PHP file does nothing. Do you see anything wrong with the code?

The module folder structures and the code are as follows:

/home/abc/public_html/app/code/abcCo/Backend/etc/adminhtml/events.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="backend_auth_user_login_success">
            <observer name="backend_activity" instance="abcCo\Backend\Observer\adminuserlogin" />
        </event>
    </config>

/home/abc/public_html/app/code/abcCo/Backend/Observer/adminuserlogin.php:

<?php

namespace abcCo\Backend\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;

class adminuserlogin implements ObserverInterface
{
   public function __construct() 
{ 
    //Observer initialization code...
    //You can use dependency injection to get any class this observer may need.
    }

public function execute(\Magento\Framework\Event\Observer $observer)
    {
       // execute an external php script when I log in.

include_once '/home/abc/public_html/app/code/abcCo/testModule/Controller/Index/login_test.php';

    } 
} 

?>

Thank you.

3 Answers 3

1

Assuming you have your file on the ro

Add ./ on your path.

Example:

include_once("./app/code/abcCo/testModule/Controller/Index/login_test.php");
0

This is not tested

<?php

namespace abcCo\Backend\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
//location is determined whether you use <working_root>/index.php or <working_root>/pub/index.php if pub/index.php you'll want to use  ../app/code/... in require_once funct.
require_once(__DIR__ .'app/code/abcCo/testModule/Controller/Index/login_test.php');
class adminuserlogin implements ObserverInterface
{
    public function __construct() 
    { 
     //Observer initialization code...
     //You can use dependency injection to get any class this observer may need.
    }

     public function execute(\Magento\Framework\Event\Observer $observer)
    {
     // use login_test.php functions here. 
    } 
} 

?>

Now that being said, you probably should just create a helper class that provides the functionality in the included file.

Here's something for reference: Custom helper in custom module in Magento 2 about helper classes.

2
  • Thanks for replying. The added code did not work, even with some variations that I made to it. I'll give the custom helper a look as you suggested.
    – Krang
    Jul 16, 2018 at 16:53
  • Yeah thats a better direction, I hope it works out for you. Jul 16, 2018 at 17:51
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.