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.

Krang
  • 167
  • 2
  • 12

3 Answers3

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");
Joao71
  • 1,197
  • 6
  • 16
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.

Chris Anderson
  • 334
  • 1
  • 7
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-&gt;dir = $dir;
    $this-&gt;resultPageFactory = $resultPageFactory;
    parent::__construct($context);
    $this-&gt;initializePDF();
}

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

    $path = $this-&gt;dir-&gt;getRoot() . &quot;/vendor/mpdf/mpdf/src/&quot;;

    $this-&gt;pdf = $path . 'Mpdf.php';

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

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

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

/**
 * @inheritdoc
 */
public function execute()
{
    echo &quot;Your Code&quot;;
    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.

Msquare
  • 9,063
  • 7
  • 25
  • 63