Class BaseApplication
Base class for full-stack applications
This class serves as a base class for applications that are using CakePHP as a full stack framework. If you are only using the Http or Console libraries you should implement the relevant interfaces directly.
The application class is responsible for bootstrapping the application, and ensuring that middleware is attached. It is also invoked as the last piece of middleware, and delegates request/response handling to the correct controller.
Property Summary
-
$configDir protected
string -
$container protected
Cake\Container\ContainerInterface|nullContainer
-
$eventClass protected
stringDefault class name for new event objects.
-
$eventManager protected
Cake\Event\EventManagerInterfaceInstance of the Cake\Event\EventManager this object is using to dispatch inner events.
-
$plugins protected
Cake\Core\PluginCollectionPlugin Collection
Method Summary
-
__construct() public
Constructor
-
addOptionalPlugin() public
Add an optional plugin
If it isn't available, ignore it.
-
addPlugin() public
Add a plugin to the loaded plugin set.
If the named plugin does not exist, or does not define a Plugin class, an instance of
Cake\Core\BasePluginwill be used. This generated class will have all plugin hooks enabled. -
bootstrap() public
Load all the application configuration and bootstrap logic.
Override this method to add additional bootstrap logic for your application.
-
buildContainer() protected
Build the service container
Override this method if you need to change how the container is built.
The container class is determined by
Configure::read('App.container'), which defaults toCake\Container\Container. Set it to the name of a class implementingCake\Container\ContainerInterfaceto use a custom container implementation. -
console() public
Define the console commands for an application.
By default, all commands in CakePHP, plugins and the application will be loaded using conventions based names.
-
dispatchEvent() public
Wrapper for creating and dispatching events.
Returns a dispatched event.
-
eventListeners() public
Define global event listeners for the application.
Listener classes are resolved through the application container and can declare constructor dependencies.
-
events() public
Register application events.
-
getContainer() public
Get the dependency injection container for the application.
The first time the container is fetched it will be constructed and stored for future calls.
-
getEventManager() public
Returns the Cake\Event\EventManager manager instance for this object.
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
-
getPlugins() public
Get the plugin collection in use.
-
handle() public
Invoke the application.
- Add the request to the container, enabling its injection into other services.
- Create the controller that will handle this request.
- Invoke the controller.
-
middleware() abstract public
Define the HTTP middleware layers for an application.
-
pluginBootstrap() public
Run bootstrap logic for loaded plugins.
-
pluginConsole() public
Run console hooks for plugins
-
pluginMiddleware() public
Run middleware hooks for plugins
-
pluginRoutes() public
Run routes hooks for loaded plugins
-
registerEventListeners() protected
Validate, resolve, and register the given event listeners.
-
registerEvents() protected
Register application events.
-
routes() public
{@inheritDoc}
By default, this will load
config/routes.phpfor ease of use and backwards compatibility. -
services() public
Register application container services.
-
setEventManager() public
Returns the Cake\Event\EventManagerInterface instance for this object.
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Method Detail
__construct() ¶ public
__construct(string $configDir, Cake\Event\EventManagerInterface|null $eventManager = null, Cake\Http\ControllerFactoryInterface<Cake\Controller\Controller>|null $controllerFactory = null)
Constructor
Parameters
-
string$configDir The directory the bootstrap configuration is held in.
-
Cake\Event\EventManagerInterface|null$eventManager optional Application event manager instance.
-
Cake\Http\ControllerFactoryInterface<Cake\Controller\Controller>|null$controllerFactory optional Controller factory.
addOptionalPlugin() ¶ public
addOptionalPlugin(Cake\Core\PluginInterface|string $name, array<string, mixed> $config = []): $this
Add an optional plugin
If it isn't available, ignore it.
Parameters
-
Cake\Core\PluginInterface|string$name The plugin name or plugin object.
-
array<string, mixed>$config optional The configuration data for the plugin if using a string for $name
Returns
$thisaddPlugin() ¶ public
addPlugin(Cake\Core\PluginInterface|string $name, array<string, mixed> $config = []): $this
Add a plugin to the loaded plugin set.
If the named plugin does not exist, or does not define a Plugin class, an
instance of Cake\Core\BasePlugin will be used. This generated class will have
all plugin hooks enabled.
Parameters
-
Cake\Core\PluginInterface|string$name -
array<string, mixed>$config optional
Returns
$thisbootstrap() ¶ public
bootstrap(): void
Load all the application configuration and bootstrap logic.
Override this method to add additional bootstrap logic for your application.
Returns
voidbuildContainer() ¶ protected
buildContainer(): Cake\Container\ContainerInterface
Build the service container
Override this method if you need to change how the container is built.
The container class is determined by Configure::read('App.container'),
which defaults to Cake\Container\Container. Set it to the name of a
class implementing Cake\Container\ContainerInterface to use a custom
container implementation.
Returns
Cake\Container\ContainerInterfaceconsole() ¶ public
console(Cake\Console\CommandCollection $commands): Cake\Console\CommandCollection
Define the console commands for an application.
By default, all commands in CakePHP, plugins and the application will be loaded using conventions based names.
Parameters
-
Cake\Console\CommandCollection$commands The CommandCollection to add commands into.
Returns
Cake\Console\CommandCollectionThe updated collection.
dispatchEvent() ¶ public
dispatchEvent(string $name, array $data = [], TSubject|null $subject = null): Cake\Event\EventInterface<TSubject>
Wrapper for creating and dispatching events.
Returns a dispatched event.
Templates
TSubject
of object = $this Parameters
-
string$name Name of the event.
-
array$data optional Any value you wish to be transported with this event to it can be read by listeners.
-
TSubject|null$subject optional The object that this event applies to ($this by default).
Returns
Cake\Event\EventInterface<TSubject>eventListeners() ¶ public
eventListeners(): list<class-string<Cake\Event\EventListenerInterface>>
Define global event listeners for the application.
Listener classes are resolved through the application container and can declare constructor dependencies.
Returns
list<class-string<Cake\Event\EventListenerInterface>>events() ¶ public
events(Cake\Event\EventManagerInterface $eventManager): Cake\Event\EventManagerInterface
Register application events.
Parameters
-
Cake\Event\EventManagerInterface$eventManager The global event manager to register listeners on
Returns
Cake\Event\EventManagerInterfacegetContainer() ¶ public
getContainer(): Cake\Container\ContainerInterface
Get the dependency injection container for the application.
The first time the container is fetched it will be constructed and stored for future calls.
Returns
Cake\Container\ContainerInterfacegetEventManager() ¶ public
getEventManager(): Cake\Event\EventManagerInterface
Returns the Cake\Event\EventManager manager instance for this object.
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Returns
Cake\Event\EventManagerInterfacegetPlugins() ¶ public
getPlugins(): Cake\Core\PluginCollection
Get the plugin collection in use.
Returns
Cake\Core\PluginCollectionhandle() ¶ public
handle(ServerRequestInterface $request): Psr\Http\Message\ResponseInterface
Invoke the application.
- Add the request to the container, enabling its injection into other services.
- Create the controller that will handle this request.
- Invoke the controller.
Parameters
-
ServerRequestInterface$request The request
Returns
Psr\Http\Message\ResponseInterfacemiddleware() ¶ abstract public
middleware(Cake\Http\MiddlewareQueue $middlewareQueue): Cake\Http\MiddlewareQueue
Define the HTTP middleware layers for an application.
Parameters
-
Cake\Http\MiddlewareQueue$middlewareQueue The middleware queue to set in your App Class
Returns
Cake\Http\MiddlewareQueuepluginBootstrap() ¶ public
pluginBootstrap(): void
Run bootstrap logic for loaded plugins.
Returns
voidpluginConsole() ¶ public
pluginConsole(Cake\Console\CommandCollection $commands): Cake\Console\CommandCollection
Run console hooks for plugins
Parameters
-
Cake\Console\CommandCollection$commands
Returns
Cake\Console\CommandCollectionpluginMiddleware() ¶ public
pluginMiddleware(Cake\Http\MiddlewareQueue $middleware): Cake\Http\MiddlewareQueue
Run middleware hooks for plugins
Parameters
-
Cake\Http\MiddlewareQueue$middleware
Returns
Cake\Http\MiddlewareQueuepluginRoutes() ¶ public
pluginRoutes(Cake\Routing\RouteBuilder $routes): Cake\Routing\RouteBuilder
Run routes hooks for loaded plugins
Parameters
-
Cake\Routing\RouteBuilder$routes
Returns
Cake\Routing\RouteBuilderregisterEventListeners() ¶ protected
registerEventListeners(list<class-string<Cake\Event\EventListenerInterface>> $listeners, Cake\Event\EventManagerInterface $eventManager, Cake\Container\ContainerInterface $container): void
Validate, resolve, and register the given event listeners.
Parameters
-
list<class-string<Cake\Event\EventListenerInterface>>$listeners FQCNs to register.
-
Cake\Event\EventManagerInterface$eventManager Manager the listeners are attached to.
-
Cake\Container\ContainerInterface$container Container used to resolve each listener.
Returns
voidThrows
InvalidArgumentExceptionWhen an entry is not a class string implementing `EventListenerInterface`.
routes() ¶ public
routes(Cake\Routing\RouteBuilder $routes): void
{@inheritDoc}
By default, this will load config/routes.php for ease of use and backwards compatibility.
Parameters
-
Cake\Routing\RouteBuilder$routes A route builder to add routes into.
Returns
voidservices() ¶ public
services(Cake\Container\ContainerInterface $container): void
Register application container services.
Parameters
-
Cake\Container\ContainerInterface$container The Container to update.
Returns
voidsetEventManager() ¶ public
setEventManager(Cake\Event\EventManagerInterface $eventManager): $this
Returns the Cake\Event\EventManagerInterface instance for this object.
You can use this instance to register any new listeners or callbacks to the object events, or create your own events and trigger them at will.
Parameters
-
Cake\Event\EventManagerInterface$eventManager the eventManager to set
Returns
$thisProperty Detail
$eventManager ¶ protected
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
Type
Cake\Event\EventManagerInterface