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 the PSR-0 compliant codebase.
  • Edit composer.json, adding the following content under autoload.
"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 under autoload.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 in autoload.files in composer.json will be loaded before webman starts. In contrast, files loaded through the framework's config/autoload.php are loaded after webman starts.
Changes to files loaded via autoload.files in composer.json require a restart to take effect; reload will not suffice. However, files loaded through the framework's config/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 that autoload.php is configured to load the support/Request.php and support/Response.php files. This is because there are also two identical files in vendor/workerman/webman-framework/src/support/. By prioritizing the loading of support/Request.php and support/Response.php from the project root directory, we allow customization of these two files' content without modifying the files in vendor. If you do not need to customize them, you can ignore these configurations.