Class ErrorController
Error Handling Controller
Controller used by ErrorHandler to render error views.
Property Summary
-
$Auth public @property
Cake\Controller\Component\AuthComponent
-
$Cookie public @property
Cake\Controller\Component\CookieComponent
-
$Csrf public @property
Cake\Controller\Component\CsrfComponent
-
$Flash public @property
Cake\Controller\Component\FlashComponent
-
$Paginator public @property
Cake\Controller\Component\PaginatorComponent
-
$RequestHandler public @property
Cake\Controller\Component\RequestHandlerComponent
-
$Security public @property
Cake\Controller\Component\SecurityComponent
-
$View public
Cake\View\View
Instance of the View created during rendering. Won't be set until after Controller::render() is called.
-
$_components protected
Cake\Controller\ComponentRegistry
Instance of ComponentRegistry used to create Components
-
$_eventClass protected
string
Default class name for new event objects.
-
$_eventManager protected
Cake\Event\EventManager
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
-
$_modelFactories protected
array
A list of model factory functions.
-
$_responseClass protected
string
The class name to use for creating the response object.
-
$_validViewOptions protected
array
These Controller properties will be passed from the Controller to the View as options.
-
$autoRender public
bool
Set to true to automatically render the view after action logic.
-
$components public
array
Array containing the names of components this controller uses. Component names should not contain the "Component" portion of the class name.
-
$helpers public
mixed
An array containing the names of helpers this controller uses. The array elements should not contain the "Helper" part of the class name.
-
$modelClass public
string
This object's primary model class name. Should be a plural form. CakePHP will not inflect the name.
-
$name public
string
The name of this controller. Controller names are plural, named after the model they manipulate.
-
$paginate public
array
Settings for pagination.
-
$passedArgs public
mixed
Holds all passed params.
-
$plugin public
string
Automatically set to the name of a plugin.
-
$request public
Cake\Network\Request
An instance of a Cake\Network\Request object that contains information about the current request. This object contains all the information about a request and several methods for reading additional information about the request.
-
$response public
Cake\Network\Response
An instance of a Response object that contains information about the impending response
-
$view public
string
The name of the view file to render. The name specified is the filename in /app/Template/
without the .ctp extension. -
$viewClass public
string
The name of the View class this controller sends output to.
-
$viewPath public
string
The path to this controllers view templates. Example
Articles
-
$viewVars public
array
Variables for the view
Method Summary
-
__construct() public
Constructor
-
__get() public
Magic accessor for model autoloading.
-
_loadComponents() protected
Loads the defined components using the Component factory.
-
_mergeControllerVars() protected
Merge components, helpers vars from parent classes.
-
_mergeProperty() protected
Merge a single property with the values declared in all parent classes.
-
_mergePropertyData() protected
Merge each of the keys in a property together.
-
_mergeVars() protected
Merge the list of $properties with all parent classes of the current class.
-
_setModelClass() protected
Set the modelClass and modelKey properties based on conventions.
-
afterFilter() public
Called after the controller action is run and rendered.
-
beforeFilter() public
Called before the controller action. You can use this method to configure and customize components or perform logic that needs to happen before each controller action.
-
beforeRedirect() public
The beforeRedirect method is invoked when the controller's redirect method is called but before any further action.
-
beforeRender() public
Called after the controller action is run, but before the view is rendered. You can use this method to perform logic or set view variables that are required on every request.
-
components() public
Get the component registry for this controller.
-
createView() public
Constructs the view class instance based on object properties.
-
dispatchEvent() public
Wrapper for creating and dispatching events.
-
eventManager() public
Returns the Cake\Event\EventManager manager instance for this object.
-
getView() public
Get view instance
-
implementedEvents() public
Returns a list of all events that will fire in the controller during its lifecycle. You can override this function to add you own listener callbacks
-
initialize() public
Initialization hook method.
-
invokeAction() public
Dispatches the controller action. Checks that the action exists and isn't private.
-
isAction() public
Method to check that an action is accessible from a URL.
-
loadComponent() public
Add a component to the controller's registry.
-
loadModel() public
Loads and constructs repository objects required by this object
-
log() public
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
-
modelFactory() public
Register a callable to generate repositories of a given type.
-
paginate() public
Handles pagination of records in Table objects.
-
redirect() public
Redirects to given $url, after turning off $this->autoRender. Script execution is halted after the redirect.
-
referer() public
Returns the referring URL for this request.
-
render() public
Instantiates the correct view class, hands it its data, and uses it to render the view output.
-
requestAction() public
Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
-
set() public
Saves a variable or an associative array of variables for use inside a template.
-
setAction() public
Internally redirects one action to another. Does not perform another HTTP request unlike Controller::redirect()
-
setRequest() public
Sets the request objects and configures a number of controller properties based on the contents of the request. Controller acts as a proxy for certain View variables which must also be updated here. The properties that get set are:
-
shutdownProcess() public
Perform the various shutdown processes for this controller. Fire the Components and Controller callbacks in the correct order.
-
startupProcess() public
Perform the startup process for this controller. Fire the Components and Controller callbacks in the correct order.
-
viewOptions() public
Get/Set valid view options in the object's _validViewOptions property. The property is created as an empty array if it is not set. If called without any parameters it will return the current list of valid view options. See
createView()
.
Method Detail
__construct() ¶ public
__construct(Cake\Network\Request|null $request = null, Cake\Network\Response|null $response = null)
Constructor
Parameters
-
Cake\Network\Request|null
$request optional Request instance.
-
Cake\Network\Response|null
$response optional Response instance.
__get() ¶ public
__get(string $name): bool|object
Magic accessor for model autoloading.
Parameters
-
string
$name Property name
Returns
bool|object
_loadComponents() ¶ protected
_loadComponents(): void
Loads the defined components using the Component factory.
Returns
void
_mergeControllerVars() ¶ protected
_mergeControllerVars(): void
Merge components, helpers vars from parent classes.
Returns
void
_mergeProperty() ¶ protected
_mergeProperty(string $property, array $parentClasses, array $options): void
Merge a single property with the values declared in all parent classes.
Parameters
-
string
$property The name of the property being merged.
-
array
$parentClasses An array of classes you want to merge with.
-
array
$options Options for merging the property, see _mergeVars()
Returns
void
_mergePropertyData() ¶ protected
_mergePropertyData(array $current, array $parent, bool $isAssoc): mixed
Merge each of the keys in a property together.
Parameters
-
array
$current The current merged value.
-
array
$parent The parent class' value.
-
bool
$isAssoc Whether or not the merging should be done in associative mode.
Returns
mixed
_mergeVars() ¶ protected
_mergeVars(array $properties, array $options = []): void
Merge the list of $properties with all parent classes of the current class.
Options:
associative
- A list of properties that should be treated as associative arrays. Properties in this list will be passed through Hash::normalize() before merging.
Parameters
-
array
$properties An array of properties and the merge strategy for them.
-
array
$options optional The options to use when merging properties.
Returns
void
_setModelClass() ¶ protected
_setModelClass(string $name): void
Set the modelClass and modelKey properties based on conventions.
If the properties are already set they will not be overwritten
Parameters
-
string
$name Class name.
Returns
void
afterFilter() ¶ public
afterFilter(Event $event): void
Called after the controller action is run and rendered.
Parameters
-
Event
$event An Event instance
Returns
void
Links
beforeFilter() ¶ public
beforeFilter(Event $event): void
Called before the controller action. You can use this method to configure and customize components or perform logic that needs to happen before each controller action.
Parameters
-
Event
$event An Event instance
Returns
void
Links
beforeRedirect() ¶ public
beforeRedirect(Event $event, string|array $url, Cake\Network\Response $response): void
The beforeRedirect method is invoked when the controller's redirect method is called but before any further action.
If the event is stopped the controller will not continue on to redirect the request. The $url and $status variables have same meaning as for the controller's method. You can set the event result to response instance or modify the redirect location using controller's response instance.
Parameters
-
Event
$event An Event instance
-
string|array
$url A string or array-based URL pointing to another location within the app, or an absolute URL
-
Cake\Network\Response
$response The response object.
Returns
void
Links
beforeRender() ¶ public
beforeRender(Event $event): void
Called after the controller action is run, but before the view is rendered. You can use this method to perform logic or set view variables that are required on every request.
Parameters
-
Event
$event An Event instance
Returns
void
Links
components() ¶ public
components(): Cake\Controller\ComponentRegistry
Get the component registry for this controller.
Returns
Cake\Controller\ComponentRegistry
createView() ¶ public
createView(string|null $viewClass = null): Cake\View\View
Constructs the view class instance based on object properties.
Parameters
-
string|null
$viewClass optional Optional namespaced class name of the View class to instantiate.
Returns
Cake\View\View
Throws
Cake\View\Exception\MissingViewException
If view class was not found.
dispatchEvent() ¶ public
dispatchEvent(string $name, array|null $data = null, object|null $subject = null): Cake\Event\Event
Wrapper for creating and dispatching events.
Returns a dispatched event.
Parameters
-
string
$name Name of the event.
-
array|null
$data optional Any value you wish to be transported with this event to it can be read by listeners.
-
object|null
$subject optional The object that this event applies to ($this by default).
Returns
Cake\Event\Event
eventManager() ¶ public
eventManager(Cake\Event\EventManager|null $eventManager = null): Cake\Event\EventManager
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.
Parameters
-
Cake\Event\EventManager|null
$eventManager optional the eventManager to set
Returns
Cake\Event\EventManager
getView() ¶ public
getView(string|null $viewClass = null): Cake\View\View
Get view instance
Parameters
-
string|null
$viewClass optional View class name or null to use $viewClass
Returns
Cake\View\View
Throws
Cake\View\Exception\MissingViewException
If view class was not found.
implementedEvents() ¶ public
implementedEvents(): array
Returns a list of all events that will fire in the controller during its lifecycle. You can override this function to add you own listener callbacks
Example:
public function implementedEvents()
{
return [
'Order.complete' => 'sendEmail',
'Article.afterBuy' => 'decrementInventory',
'User.onRegister' => ['callable' => 'logRegistration', 'priority' => 20, 'passParams' => true]
];
}
Returns
array
initialize() ¶ public
initialize(): void
Initialization hook method.
Implement this method to avoid having to overwrite the constructor and call parent.
Returns
void
invokeAction() ¶ public
invokeAction(): mixed
Dispatches the controller action. Checks that the action exists and isn't private.
Returns
mixed
Throws
LogicException
When request is not set.
Cake\Controller\Exception\MissingActionException
When actions are not defined or inaccessible.
isAction() ¶ public
isAction(string $action): bool
Method to check that an action is accessible from a URL.
Override this method to change which controller methods can be reached. The default implementation disallows access to all methods defined on Cake\Controller\Controller, and allows all public methods on all subclasses of this class.
Parameters
-
string
$action The action to check.
Returns
bool
loadComponent() ¶ public
loadComponent(string $name, array $config = []): Cake\Controller\Component
Add a component to the controller's registry.
This method will also set the component to a property. For example:
$this->loadComponent('Acl.Acl');
Will result in a Toolbar
property being set.
Parameters
-
string
$name The name of the component to load.
-
array
$config optional The config for the component.
Returns
Cake\Controller\Component
loadModel() ¶ public
loadModel(string|null $modelClass = null, string $type = 'Table'): object
Loads and constructs repository objects required by this object
Typically used to load ORM Table objects as required. Can also be used to load other types of repository objects your application uses.
If a repository provider does not return an object a MissingModelException will be thrown.
Parameters
-
string|null
$modelClass optional Name of model class to load. Defaults to $this->modelClass
-
string
$type optional The type of repository to load. Defaults to 'Table' which delegates to Cake\ORM\TableRegistry.
Returns
object
Throws
Cake\Datasource\Exception\MissingModelException
If the model class cannot be found.
InvalidArgumentException
When using a type that has not been registered.
log() ¶ public
log(mixed $msg, int|string $level = LogLevel::ERROR, string|array $context = []): bool
Convenience method to write a message to Log. See Log::write() for more information on writing to logs.
Parameters
-
mixed
$msg Log message.
-
int|string
$level optional Error level.
-
string|array
$context optional Additional log data relevant to this message.
Returns
bool
modelFactory() ¶ public
modelFactory(string $type, callable $factory): void
Register a callable to generate repositories of a given type.
Parameters
-
string
$type The name of the repository type the factory function is for.
-
callable
$factory The factory function used to create instances.
Returns
void
paginate() ¶ public
paginate(Cake\ORM\Table|stringCake\ORM\Query|null $object = null): Cake\ORM\ResultSet
Handles pagination of records in Table objects.
Will load the referenced Table object, and have the PaginatorComponent
paginate the query using the request date and settings defined in $this->paginate
.
This method will also make the PaginatorHelper available in the view.
Parameters
-
Cake\ORM\Table|stringCake\ORM\Query|null
$object optional Table to paginate (e.g: Table instance, 'TableName' or a Query object)
Returns
Cake\ORM\ResultSet
Throws
RuntimeException
When no compatible table object can be found.
Links
redirect() ¶ public
redirect(string|array $url, int $status = 302): voidCake\Network\Response
Redirects to given $url, after turning off $this->autoRender. Script execution is halted after the redirect.
Parameters
-
string|array
$url A string or array-based URL pointing to another location within the app, or an absolute URL
-
int
$status optional HTTP status code (eg: 301)
Returns
voidCake\Network\Response
Links
referer() ¶ public
referer(string|null $default = null, bool $local = false): string
Returns the referring URL for this request.
Parameters
-
string|null
$default optional Default URL to use if HTTP_REFERER cannot be read from headers
-
bool
$local optional If true, restrict referring URLs to local server
Returns
string
render() ¶ public
render(string $view = null, string $layout = null): Cake\Network\Response
Instantiates the correct view class, hands it its data, and uses it to render the view output.
Parameters
-
string
$view optional View to use for rendering
-
string
$layout optional Layout to use
Returns
Cake\Network\Response
Links
requestAction() ¶ public
requestAction(string|array $url, array $extra = []): mixed
Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
Under the hood this method uses Router::reverse() to convert the $url parameter into a string URL. You should use URL formats that are compatible with Router::reverse()
Examples
A basic example getting the return value of the controller action:
$variables = $this->requestAction('/articles/popular');
A basic example of request action to fetch a rendered page without the layout.
$viewHtml = $this->requestAction('/articles/popular', ['return']);
You can also pass the URL as an array:
$vars = $this->requestAction(['controller' => 'articles', 'action' => 'popular']);
Passing other request data
You can pass POST, GET, COOKIE and other data into the request using the appropriate keys.
Cookies can be passed using the cookies
key. Get parameters can be set with query
and post
data can be sent using the post
key.
$vars = $this->requestAction('/articles/popular', [
'query' => ['page' => 1],
'cookies' => ['remember_me' => 1],
]);
Sending environment or header values
By default actions dispatched with this method will use the global $_SERVER and $_ENV values. If you want to override those values for a request action, you can specify the values:
$vars = $this->requestAction('/articles/popular', [
'environment' => ['CONTENT_TYPE' => 'application/json']
]);
Transmitting the session
By default actions dispatched with this method will use the standard session object. If you want a particular session instance to be used, you need to specify it.
$vars = $this->requestAction('/articles/popular', [
'session' => new Session($someSessionConfig)
]);
Parameters
-
string|array
$url String or array-based url. Unlike other url arrays in CakePHP, this url will not automatically handle passed arguments in the $url parameter.
-
array
$extra optional if array includes the key "return" it sets the autoRender to true. Can also be used to submit GET/POST data, and passed arguments.
Returns
mixed
set() ¶ public
set(string|array $name, string|array|null $value = null): $this
Saves a variable or an associative array of variables for use inside a template.
Parameters
-
string|array
$name A string or an array of data.
-
string|array|null
$value optional Value in case $name is a string (which then works as the key). Unused if $name is an associative array, otherwise serves as the values to $name's keys.
Returns
$this
setAction() ¶ public
setAction(string $action): mixed
Internally redirects one action to another. Does not perform another HTTP request unlike Controller::redirect()
Examples:
setAction('another_action');
setAction('action_with_parameters', $parameter1);
Parameters
-
string
$action The new action to be 'redirected' to. Any other parameters passed to this method will be passed as parameters to the new action.
Returns
mixed
setRequest() ¶ public
setRequest(Cake\Network\Request $request): void
Sets the request objects and configures a number of controller properties based on the contents of the request. Controller acts as a proxy for certain View variables which must also be updated here. The properties that get set are:
- $this->request - To the $request parameter
- $this->plugin - To the $request->params['plugin']
- $this->autoRender - To false if $request->params['return'] == 1
- $this->passedArgs - The combined results of params['named'] and params['pass]
- View::$passedArgs - $this->passedArgs
- View::$plugin - $this->plugin
- View::$view - To the $request->params['action']
- View::$autoLayout - To the false if $request->params['bare']; is set.
Parameters
-
Cake\Network\Request
$request Request instance.
Returns
void
shutdownProcess() ¶ public
shutdownProcess(): voidCake\Network\Response
Perform the various shutdown processes for this controller. Fire the Components and Controller callbacks in the correct order.
- triggers the component
shutdown
callback. - calls the Controller's
afterFilter
method.
Returns
voidCake\Network\Response
startupProcess() ¶ public
startupProcess(): voidCake\Network\Response
Perform the startup process for this controller. Fire the Components and Controller callbacks in the correct order.
- Initializes components, which fires their
initialize
callback - Calls the controller
beforeFilter
. - triggers Component
startup
methods.
Returns
voidCake\Network\Response
viewOptions() ¶ public
viewOptions(string|array|null $options = null, bool $merge = true): array
Get/Set valid view options in the object's _validViewOptions property. The property is
created as an empty array if it is not set. If called without any parameters it will
return the current list of valid view options. See createView()
.
Parameters
-
string|array|null
$options optional string or array of string to be appended to _validViewOptions.
-
bool
$merge optional Whether to merge with or override existing valid View options. Defaults to
true
.
Returns
array
Property Detail
$View ¶ public
Instance of the View created during rendering. Won't be set until after Controller::render() is called.
Type
Cake\View\View
$_components ¶ protected
Instance of ComponentRegistry used to create Components
Type
Cake\Controller\ComponentRegistry
$_eventManager ¶ protected
Instance of the Cake\Event\EventManager this object is using to dispatch inner events.
Type
Cake\Event\EventManager
$_validViewOptions ¶ protected
These Controller properties will be passed from the Controller to the View as options.
Type
array
$components ¶ public
Array containing the names of components this controller uses. Component names should not contain the "Component" portion of the class name.
Example:
public $components = ['RequestHandler', 'Acl'];
Type
array
$helpers ¶ public
An array containing the names of helpers this controller uses. The array elements should not contain the "Helper" part of the class name.
Example:
public $helpers = ['Form', 'Html', 'Time'];
Type
mixed
$modelClass ¶ public
This object's primary model class name. Should be a plural form. CakePHP will not inflect the name.
Example: For an object named 'Comments', the modelClass would be 'Comments'.
Plugin classes should use Plugin.Comments
style names to correctly load
models from the correct plugin.
Type
string
$name ¶ public
The name of this controller. Controller names are plural, named after the model they manipulate.
Set automatically using conventions in Controller::__construct().
Type
string
$paginate ¶ public
Settings for pagination.
Used to pre-configure pagination preferences for the various tables your controller will be paginating.
Type
array
$request ¶ public
An instance of a Cake\Network\Request object that contains information about the current request. This object contains all the information about a request and several methods for reading additional information about the request.
Type
Cake\Network\Request
$response ¶ public
An instance of a Response object that contains information about the impending response
Type
Cake\Network\Response
$view ¶ public
The name of the view file to render. The name specified
is the filename in /app/Template/
Type
string
$viewPath ¶ public
The path to this controllers view templates.
Example Articles
Set automatically using conventions in Controller::__construct().
Type
string