If we want to use composer packages, we have to include
'vendor/autoload.php', and if we don't have a front controller design
pattern, we must include 'vendor/autoload.php' on every/most files. Why not
to auto include 'vendor/autoload.php' file (if vendor folder exists)?
- PHP will use include_once to include file
Example Directory Structure:
-
Index.php (file)
-
Test.php (file)
-
Classes (folder)
-- Classes/test2.php (file) -
Vendor (folder)
-
On Index.php, php will automatically include 'vendor/autoload.php'
because the vendor directory exists. -
On Test.php, php will automatically include 'vendor/autoload.php'
because the vendor directory exists. -
On Classes/test2.php, since file is in sub directory and here vendor
directory doesn't exist, so php will not include 'vendor/autoload.php'
If we want to use composer packages, we have to include
'vendor/autoload.php', and if we don't have a front controller design
pattern, we must include 'vendor/autoload.php' on every/most files. Why not
to auto include 'vendor/autoload.php' file (if vendor folder exists)?
- PHP will use include_once to include file
Example Directory Structure:
Index.php (file)
Test.php (file)
Classes (folder)
-- Classes/test2.php (file)Vendor (folder)
On Index.php, php will automatically include 'vendor/autoload.php'
because the vendor directory exists.On Test.php, php will automatically include 'vendor/autoload.php'
because the vendor directory exists.On Classes/test2.php, since file is in sub directory and here vendor
directory doesn't exist, so php will not include 'vendor/autoload.php'
vendor/autoload.php is a Composer-specific convention. It's not something imposed by PHP itself.
It's also quite simple to auto-include a file on every request, using the auto_append_file ini directive:
https://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
If you don't have one or a small number of front controllers, that's a good setting to look into for exactly this purpose.
--Larry Garfield
Larry already mentioned auto_append_file
that I also think is the
way to go, if it fits.
The example directory structure from your email is also considered
insecure, because without proper web-server protection, you are
essentially exposing all vendor
files, including the ones that
potentially execute code.
On Sat, Mar 27, 2021 at 10:23 PM Abdul Haq Sheikh
khaweronline@gmail.com wrote:
If we want to use composer packages, we have to include
'vendor/autoload.php', and if we don't have a front controller design
pattern, we must include 'vendor/autoload.php' on every/most files. Why not
to auto include 'vendor/autoload.php' file (if vendor folder exists)?
- PHP will use include_once to include file
Example Directory Structure:
Index.php (file)
Test.php (file)
Classes (folder)
-- Classes/test2.php (file)Vendor (folder)
On Index.php, php will automatically include 'vendor/autoload.php'
because the vendor directory exists.On Test.php, php will automatically include 'vendor/autoload.php'
because the vendor directory exists.On Classes/test2.php, since file is in sub directory and here vendor
directory doesn't exist, so php will not include 'vendor/autoload.php'
- On Index.php, php will automatically include 'vendor/autoload.php'
because the vendor directory exists.- On Test.php, php will automatically include 'vendor/autoload.php'
because the vendor directory exists.- On Classes/test2.php, since file is in sub directory and here vendor
directory doesn't exist, so php will not include 'vendor/autoload.php'
Just an observation, but if you're doing vendor/autoload.php from your
index.php page, and are not already doing custom include paths, then
chances are you have your vendor directory publicly accessible and that
is considered unwise.