Application
The Application
is the heart of your application. It controlshow your application is configured, and what plugins, middleware, consolecommands and routes are included.
You can find your Application
class at src/Application.php. By defaultit will be pretty slim and only define a few defaultMiddleware. Applications can define the following hookmethods:
bootstrap
Used to load configuration files, define constants and other global functions.By default this will include config/bootstrap.php. This is the ideal placeto load Plugins and global event listeners.routes
Used to load routes. By default thiswill include config/routes.php.middleware
Used to add middleware to your application.console
Used to add console commands to yourapplication. By default this will automatically discover shells & commands inyour application and all plugins.
Bootstrapping your Application
If you have any additional configuration needs, you should add them to yourapplication’s config/bootstrap.php file. This file is included before eachrequest, and CLI command.
This file is ideal for a number of common bootstrapping tasks:
- Defining convenience functions.
- Declaring constants.
- Defining cache configuration.
- Defining logging configuration.
- Loading custom inflections.
- Loading configuration files.
It might be tempting to place formatting functions there in order to use them inyour controllers. As you’ll see in the Controllers and Viewssections there are better ways you add custom logic to your application.
Application::bootstrap()
In addition to the config/bootstrap.php file which should be used toconfigure low-level concerns of your application, you can also use theApplication::bootstrap()
hook method to load/initialize plugins, and attachglobal event listeners:
- // in src/Application.php
- namespace App;
- use Cake\Core\Plugin;
- use Cake\Http\BaseApplication;
- class Application extends BaseApplication
- {
- public function bootstrap()
- {
- // Call the parent to `require_once` config/bootstrap.php
- parent::bootstrap();
- // Load MyPlugin
- $this->addPlugin('MyPlugin');
- }
- }
Loading plugins and events in Application::bootstrap()
makesController Integration Testing easier as events and routes will be re-processed oneach test method.