Autoloading
Loading files based on the PSR-0 specification using Composer
webman follows the PSR-4
autoloading specification. If your project needs to load libraries based on the PSR-0
specification, follow these steps:
- Create a
extend
directory to store the libraries that follow thePSR-0
specification. - Edit the
composer.json
file and add the following code underautoload
:
"psr-0" : {
"": "extend/"
}
The final result should look like this:
- Run
composer dumpautoload
. - Restart webman by running
php start.php restart
(Note: you must restart for the changes to take effect).
Loading specific files using Composer
-
Edit the
composer.json
file and add the files you want to load underautoload.files
:"files": [ "./support/helpers.php", "./app/helpers.php" ]
-
Run
composer dumpautoload
. -
Restart webman by running
php start.php restart
(Note: you must restart for the changes to take effect).
Note
Files specified in theautoload.files
configuration in composer.json will be loaded before webman starts. On the other hand, files loaded using theconfig/autoload.php
provided by the framework are loaded after webman starts.
Changes made to the files specified inautoload.files
in composer.json will require a restart to take effect. Reloading will not work. However, files loaded using theconfig/autoload.php
provided by the framework support hot reloading, and changes will take effect upon reloading.
Loading specific files using the framework
Sometimes, certain files may not conform to the PSR specification and thus cannot be loaded automatically. In such cases, we can use the config/autoload.php
file to load these files. For example:
return [
'files' => [
base_path() . '/app/functions.php',
base_path() . '/support/Request.php',
base_path() . '/support/Response.php',
]
];
Note
In theautoload.php
file, we see thatsupport/Request.php
andsupport/Response.php
are loaded. This is because there are also two files with the same names invendor/workerman/webman-framework/src/support/
. By loading the files specified inautoload.php
, we give priority to the files in the project's root directory, allowing us to customize the content of these two files without modifying the files invendor
. If you don't need to customize them, you can ignore these two configurations.