BaseApplication
in package
implements
ConsoleApplicationInterface, ContainerApplicationInterface, HttpApplicationInterface, PluginApplicationInterface, RoutingApplicationInterface
uses
EventDispatcherTrait
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.
Table of Contents
Interfaces
- ConsoleApplicationInterface
- An interface defining the methods that the console runner depend on.
- ContainerApplicationInterface
- Interface for applications that configure and use a dependency injection container.
- HttpApplicationInterface
- An interface defining the methods that the http server depend on.
- PluginApplicationInterface
- Interface for Applications that leverage plugins & events.
- RoutingApplicationInterface
- Interface for applications that use routing.
Properties
- $_eventClass : string
- Default class name for new event objects.
- $_eventManager : EventManagerInterface|null
- Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
- $configDir : string
- $container : ContainerInterface|null
- Container
- $controllerFactory : ControllerFactoryInterface|null
- Controller factory
- $plugins : PluginCollection
- Plugin Collection
Methods
- __construct() : mixed
- Constructor
- addOptionalPlugin() : $this
- Add an optional plugin
- addPlugin() : $this
- Add a plugin to the loaded plugin set.
- bootstrap() : void
- Load all the application configuration and bootstrap logic.
- console() : CommandCollection
- Define the console commands for an application.
- dispatchEvent() : EventInterface
- Wrapper for creating and dispatching events.
- getContainer() : ContainerInterface
- Get the dependency injection container for the application.
- getEventManager() : EventManagerInterface
- Returns the Cake\Event\EventManager manager instance for this object.
- getPlugins() : PluginCollection
- Get the plugin collection in use.
- handle() : ResponseInterface
- Invoke the application.
- middleware() : MiddlewareQueue
- Define the HTTP middleware layers for an application.
- pluginBootstrap() : void
- Run bootstrap logic for loaded plugins.
- pluginConsole() : CommandCollection
- Run console hooks for plugins
- pluginMiddleware() : MiddlewareQueue
- Run middleware hooks for plugins
- pluginRoutes() : RouteBuilder
- Run routes hooks for loaded plugins
- routes() : void
- Define the routes for an application.
- services() : void
- Register application container services.
- setEventManager() : $this
- Returns the Cake\Event\EventManagerInterface instance for this object.
- buildContainer() : ContainerInterface
- Build the service container
Properties
$_eventClass
Default class name for new event objects.
protected
string
$_eventClass
= \Cake\Event\Event::class
$_eventManager
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
protected
EventManagerInterface|null
$_eventManager
$configDir
protected
string
$configDir
Contains the path of the config directory
$container
Container
protected
ContainerInterface|null
$container
$controllerFactory
Controller factory
protected
ControllerFactoryInterface|null
$controllerFactory
$plugins
Plugin Collection
protected
PluginCollection
$plugins
Methods
__construct()
Constructor
public
__construct(string $configDir[, EventManagerInterface|null $eventManager = null ][, ControllerFactoryInterface|null $controllerFactory = null ]) : mixed
Parameters
- $configDir : string
-
The directory the bootstrap configuration is held in.
- $eventManager : EventManagerInterface|null = null
-
Application event manager instance.
- $controllerFactory : ControllerFactoryInterface|null = null
-
Controller factory.
addOptionalPlugin()
Add an optional plugin
public
addOptionalPlugin(PluginInterface|string $name[, array<string, mixed> $config = [] ]) : $this
If it isn't available, ignore it.
Parameters
- $name : PluginInterface|string
-
The plugin name or plugin object.
- $config : array<string, mixed> = []
-
The configuration data for the plugin if using a string for $name
Return values
$thisaddPlugin()
Add a plugin to the loaded plugin set.
public
addPlugin(mixed $name[, array<string|int, mixed> $config = [] ]) : $this
Parameters
- $name : mixed
-
The plugin name or plugin object.
- $config : array<string|int, mixed> = []
-
The configuration data for the plugin if using a string for $name
Tags
Return values
$thisbootstrap()
Load all the application configuration and bootstrap logic.
public
bootstrap() : void
Tags
console()
Define the console commands for an application.
public
console(CommandCollection $commands) : CommandCollection
By default, all commands in CakePHP, plugins and the application will be loaded using conventions based names.
Parameters
- $commands : CommandCollection
-
The CommandCollection to add commands into.
Return values
CommandCollection —The updated collection.
dispatchEvent()
Wrapper for creating and dispatching events.
public
dispatchEvent(string $name[, array<string|int, mixed>|null $data = null ][, object|null $subject = null ]) : EventInterface
Returns a dispatched event.
Parameters
- $name : string
-
Name of the event.
- $data : array<string|int, mixed>|null = null
-
Any value you wish to be transported with this event to it can be read by listeners.
- $subject : object|null = null
-
The object that this event applies to ($this by default).
Return values
EventInterfacegetContainer()
Get the dependency injection container for the application.
public
getContainer() : ContainerInterface
The first time the container is fetched it will be constructed and stored for future calls.
Return values
ContainerInterfacegetEventManager()
Returns the Cake\Event\EventManager manager instance for this object.
public
getEventManager() : EventManagerInterface
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.
Return values
EventManagerInterfacegetPlugins()
Get the plugin collection in use.
public
getPlugins() : PluginCollection
Return values
PluginCollectionhandle()
Invoke the application.
public
handle(ServerRequestInterface $request) : ResponseInterface
- Add the request to the container, enabling its injection into other services.
- Create the controller that will handle this request.
- Invoke the controller.
Parameters
- $request : ServerRequestInterface
-
The request
Return values
ResponseInterfacemiddleware()
Define the HTTP middleware layers for an application.
public
abstract middleware(MiddlewareQueue $middlewareQueue) : MiddlewareQueue
Parameters
- $middlewareQueue : MiddlewareQueue
-
The middleware queue to set in your App Class
Return values
MiddlewareQueuepluginBootstrap()
Run bootstrap logic for loaded plugins.
public
pluginBootstrap() : void
Tags
pluginConsole()
Run console hooks for plugins
public
pluginConsole(CommandCollection $commands) : CommandCollection
Parameters
- $commands : CommandCollection
-
The CommandCollection to use.
Tags
Return values
CommandCollectionpluginMiddleware()
Run middleware hooks for plugins
public
pluginMiddleware(MiddlewareQueue $middleware) : MiddlewareQueue
Parameters
- $middleware : MiddlewareQueue
-
The MiddlewareQueue to use.
Tags
Return values
MiddlewareQueuepluginRoutes()
Run routes hooks for loaded plugins
public
pluginRoutes(RouteBuilder $routes) : RouteBuilder
Parameters
- $routes : RouteBuilder
-
The route builder to use.
Tags
Return values
RouteBuilderroutes()
Define the routes for an application.
public
routes(RouteBuilder $routes) : void
By default, this will load config/routes.php
for ease of use and backwards compatibility.
Parameters
- $routes : RouteBuilder
-
A route builder to add routes into.
services()
Register application container services.
public
services(ContainerInterface $container) : void
Parameters
- $container : ContainerInterface
-
The Container to update.
setEventManager()
Returns the Cake\Event\EventManagerInterface instance for this object.
public
setEventManager(EventManagerInterface $eventManager) : $this
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
- $eventManager : EventManagerInterface
-
the eventManager to set
Return values
$thisbuildContainer()
Build the service container
protected
buildContainer() : ContainerInterface
Override this method if you need to use a custom container or want to change how the container is built.