Autoload
Using Composer to Load PSR-0 Compliant Files
Webman follows the PSR-4
autoloading standard. If your business requires loading a codebase that complies with the PSR-0
standard, refer to the following steps.
- Create an
extend
directory to store thePSR-0
compliant codebase. - Edit
composer.json
, adding the following content underautoload
.
"psr-0" : {
"": "extend/"
}
The final result will look similar to this:
- Run
composer dumpautoload
. - Execute
php start.php restart
to restart webman (note that it must be restarted for changes to take effect).
Using Composer to Load Certain Files
-
Edit
composer.json
, adding the files you want to load underautoload.files
."files": [ "./support/helpers.php", "./app/helpers.php" ]
-
Run
composer dumpautoload
. -
Execute
php start.php restart
to restart webman (note that it must be restarted for changes to take effect).
Tip
The files configured inautoload.files
in composer.json will be loaded before webman starts. In contrast, files loaded through the framework'sconfig/autoload.php
are loaded after webman starts.
Changes to files loaded viaautoload.files
in composer.json require a restart to take effect; reload will not suffice. However, files loaded through the framework'sconfig/autoload.php
support hot reloading, and changes can take effect with a reload.
Using the Framework to Load Certain Files
Some files may not comply with the SPR standard and may not be autoloaded. We can load these files by configuring config/autoload.php
, for example:
return [
'files' => [
base_path() . '/app/functions.php',
base_path() . '/support/Request.php',
base_path() . '/support/Response.php',
]
];
Tip
We see thatautoload.php
is configured to load thesupport/Request.php
andsupport/Response.php
files. This is because there are also two identical files invendor/workerman/webman-framework/src/support/
. By prioritizing the loading ofsupport/Request.php
andsupport/Response.php
from the project root directory, we allow customization of these two files' content without modifying the files invendor
. If you do not need to customize them, you can ignore these configurations.