0

I'm making my software compatible with Magento 2. I need to update a PHP file (bundled with the software) that is required to communicate with Magento.

I've read that the root folder can be either the Magento installation root folder or the pub folder.

If it's in the root folder, we must write:

require __DIR__ . '/app/bootstrap.php';

If it's in the pub folder, we must write:

require realpath(__DIR__) . '/../app/bootstrap.php';

Is there a reliable and elegant way to include bootstrap.php if the script file is in the root folder or the pub folder ?

The code should work in both cases.

3
  • 3
    Can you not integrate it into Magento as an actual proper Magento module, with a real controller and request path? That way you don't even need to worry about the root folder.
    – Ryan Hoerr
    Commented Apr 19, 2017 at 20:24
  • I'm also going to make a Magento module. It will be better and I'll be able to have a configuration panel in the Magento backoffice for the software. Commented Apr 20, 2017 at 9:09
  • But I'll keep the bundled PHP file as an option. It simplifies the installation for people who just want to try the software. Most of my customers are shop owners. Installing a module may be too difficult and technical and may discourage them from trying the software with their shop. Here's the current installation process: benabee.com/en/downloads . It's easy but sometimes people upload the file in the wrong folder or have permission issues. Commented Apr 20, 2017 at 9:25

1 Answer 1

0

You can use absolute path instead of relative path as below.

$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require "$root/app/bootstrap.php";
1
  • I tested the code. It works fine in both case. Thanks Commented Apr 20, 2017 at 9:06

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.