Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid to require vendor/autoload.php in every file

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.

like image 573
Augusto Nuñez Avatar asked Sep 13 '26 00:09

Augusto Nuñez


1 Answers

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"
like image 144
Nico Haase Avatar answered Sep 14 '26 14:09

Nico Haase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!