3

I'm currently learning about autoload in php using composer and psr-4. i have successfully configured my composer.json and i can access classes using namespaces. Now, the problem is that i have to include vendor/autoload.php in every file that i want to use it in. It looks like this because of the structure i have in my project:

require("../../../vendor/autoload.php");

Is there a cleaner way to do this or avoid requiring it in every file?

I have tried with some things like creating a Global variable with that route, but it is not working. This is what i tried:

At the start of my index.php: $GLOBAL["autoloadPath"]= $_SERVER['DOCUMENT_ROOT'] . '/ipa-crm/vendor/autoload.php';

Then i added this in every file that i needed: require_once $GLOBAL["autoloadPath"];

Thanks in advance.

common sense
  • 3,775
  • 6
  • 22
  • 31
  • I assume you've tried $GLOBALS instead of $GLOBAL? – DigiLive Dec 11 '17 at 20:54
  • 3
    Lesson learned, whatever you do will be a hack.. next time have a single entry point and use namespacing to autoload your logic through a router, instead of many individual files. – Lawrence Cherone Dec 11 '17 at 21:04

3 Answers3

3

Using the configuration auto_prepend_file in .htaccess, you can automatically prepend a PHP file to all requested PHP files. Just put it into your .htaccess like this:

php_value auto_prepend_file "/path/to/file/before.php"
php_value auto_append_file "/path/to/file/after.php"
Nico Haase
  • 11,420
  • 35
  • 43
  • 69
  • Thank you! i tried this and it worked. But instead,i did it in my php.ini file by adding auto_prepend_file and the route to the file. Now, my other question would be: doing this is safe? – Augusto Nuñez Dec 11 '17 at 22:56
  • @AugustoNuñez what do you mean by "safe"? – Nico Haase Dec 12 '17 at 08:39
  • 1
    I feel it is not good to prepend/append files, because that affects every scripts even though they did not require vendor autoload. – Bimal Poudel Sep 10 '19 at 01:22
  • 2
    @BimalPoudel I did not say that this prepend feature is a good one, but the OPs request was clearly to prepend something to **all** files without any exception – Nico Haase Sep 10 '19 at 07:36
1

I would say the most recommended way is having one single file that will load all other files and this files will load all other files depending on current action.

So you have index.php file with content similar to this (this is obviously very simplified):

require __DIR__.'/../vendor/autoload.php';

switch ($_GET['url']) {
    case 'contact':
       require __DIR__.'/../pages/contact.php';
       break;
    case '':
       require __DIR__.'/../pages/main.php';
       break;
   default:
      require __DIR__.'/../pages/404.php';
}

And you also have .htaccess file with mod rewrite enabled with such content:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)(.*)/?$ index.php?url=$1&%{QUERY_STRING}

So now all traffic will go through index.php and you can require Composer autoloader only in this single file.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
0

Couple of different solutions possible. Cleanest way is to use require_once within config file only, where you have your own set of vendor folder for each project. But there is an advanced option on localhost, or the server that allows to reconfigure php.ini:

Modify your php.ini to append a custom path that points to composer's global path. Then write:

<?php
require_once "vendor/autoload.php";

Make sure you do not use local vendor directory, or just ensure that your composer has defined everything locally. I mean, presence of local ./vendor directory is optional. The "./" which is missing in my example will allow PHP to look for global path. Then, always use your composer globally (passing global as additional parameter when you install a composer library.

Your php.ini may look like:

include_path = "C:/Users/USER/AppData/Roaming/Composer"

Thus, vendor/autload.php is always available to any php files, if they call require_once "vendor/autoload.php". Hopefully, you will use it once in your config file for the entire project, and not from individual scripts.

Valor_
  • 3,461
  • 9
  • 60
  • 109
Bimal Poudel
  • 1,214
  • 2
  • 18
  • 41