W3cubDocs

/CakePHP 4.4

Class ErrorController

Error Handling Controller

Controller used by ErrorHandler to render error views.

Namespace: Cake\Controller

Property Summary

  • $Auth public @property
    Cake\Controller\Component\AuthComponent
  • $Flash public @property
    Cake\Controller\Component\FlashComponent
  • $FormProtection public @property
    Cake\Controller\Component\FormProtectionComponent
  • $Paginator public @property
    Cake\Controller\Component\PaginatorComponent
  • $RequestHandler public @property
    Cake\Controller\Component\RequestHandlerComponent
  • $Security public @property
    Cake\Controller\Component\SecurityComponent
  • $_components protected
    Cake\Controller\ComponentRegistry|null

    Instance of ComponentRegistry used to create Components

  • $_eventClass protected
    string

    Default class name for new event objects.

  • $_eventManager protected
    Cake\Event\EventManagerInterface|null

    Instance of the Cake\Event\EventManager this object is using to dispatch inner events.

  • $_modelFactories protected
    array<callableCake\Datasource\Locator\LocatorInterface>

    A list of overridden model factory functions.

  • $_modelType protected
    string

    The model type to use.

  • $_tableLocator protected
    Cake\ORM\Locator\LocatorInterface|null

    Table locator instance

  • $_viewBuilder protected
    Cake\View\ViewBuilder|null

    The view builder instance being used.

  • $autoRender protected
    bool

    Set to true to automatically render the view after action logic.

  • $defaultTable protected
    string|null

    This object's default table alias.

  • $middlewares protected
    array

    Middlewares list.

  • $modelClass protected deprecated
    string|null

    This object's primary model class name. Should be a plural form. CakePHP will not inflect the name.

  • $name protected
    string

    The name of this controller. Controller names are plural, named after the model they manipulate.

  • $paginate public
    array

    Settings for pagination.

  • $plugin protected
    string|null

    Automatically set to the name of a plugin.

  • $request protected
    Cake\Http\ServerRequest

    An instance of a \Cake\Http\ServerRequest 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 protected
    Cake\Http\Response

    An instance of a Response object that contains information about the impending response

Method Summary

  • __construct() public

    Constructor.

  • __get() public

    Magic accessor for model autoloading.

  • __set() public

    Magic setter for removed properties.

  • _setModelClass() protected

    Set the modelClass property based on conventions.

  • _templatePath() protected

    Get the templatePath based on controller name and request prefix.

  • 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

    beforeRender callback.

  • chooseViewClass() protected

    Use the view classes defined on this controller to view selection based on content-type negotiation.

  • components() public

    Get the component registry for this controller.

  • createView() public

    Constructs the view class instance based on the current configuration.

  • disableAutoRender() public

    Disable automatic action rendering.

  • dispatchEvent() public

    Wrapper for creating and dispatching events.

  • enableAutoRender() public

    Enable automatic action rendering.

  • fetchTable() public

    Convenience method to get a table instance.

  • getAction() public

    Get the closure for action to be invoked by ControllerFactory.

  • getEventManager() public

    Returns the Cake\Event\EventManager manager instance for this object.

  • getMiddleware() public

    Get middleware to be applied for this controller.

  • getModelType() public

    Get the model type to be used by this class

  • getName() public

    Returns the controller name.

  • getPlugin() public

    Returns the plugin name.

  • getRequest() public

    Gets the request instance.

  • getResponse() public

    Gets the response instance.

  • getTableLocator() public

    Gets the table locator.

  • implementedEvents() public

    Returns a list of all events that will fire in the controller during its lifecycle. You can override this function to add your own listener callbacks

  • initialize() public

    Initialization hook method.

  • invokeAction() public

    Dispatches the controller action.

  • isAction() public

    Method to check that an action is accessible from a URL.

  • isAutoRenderEnabled() public

    Returns true if an action should be rendered automatically.

  • loadComponent() public

    Add a component to the controller's registry.

  • loadModel() public deprecated

    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.

  • middleware() public

    Register middleware for the controller.

  • modelFactory() public

    Override a existing 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.

  • 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.

  • set() public

    Saves a variable or an associative array of variables for use inside a template.

  • setAction() public deprecated

    Internally redirects one action to another. Does not perform another HTTP request unlike Controller::redirect()

  • setEventManager() public

    Returns the Cake\Event\EventManagerInterface instance for this object.

  • setModelType() public

    Set the model type to be used by this class

  • setName() public

    Sets the controller name.

  • setPlugin() public

    Sets the plugin name.

  • 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:

  • setResponse() public

    Sets the response instance.

  • setTableLocator() public

    Sets the table locator.

  • 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.

  • viewBuilder() public

    Get the view builder being used.

  • viewClasses() public

    Get the View classes this controller can perform content negotiation with.

Method Detail

__construct() public

__construct(Cake\Http\ServerRequest|null $request = null, Cake\Http\Response|null $response = null, string|null $name = null, Cake\Event\EventManagerInterface|null $eventManager = null, Cake\Controller\ComponentRegistry|null $components = null)

Constructor.

Sets a number of properties based on conventions if they are empty. To override the conventions CakePHP uses you can define properties in your class declaration.

Parameters

Cake\Http\ServerRequest|null $request optional

Request object for this controller. Can be null for testing, but expect that features that use the request parameters will not work.

Cake\Http\Response|null $response optional

Response object for this controller.

string|null $name optional

Override the name useful in testing when using mocks.

Cake\Event\EventManagerInterface|null $eventManager optional

The event manager. Defaults to a new instance.

Cake\Controller\ComponentRegistry|null $components optional

The component registry. Defaults to a new instance.

__get() public

__get(string $name): Cake\Datasource\RepositoryInterface|null

Magic accessor for model autoloading.

Parameters

string $name

Property name

Returns

Cake\Datasource\RepositoryInterface|null

__set() public

__set(string $name, mixed $value): void

Magic setter for removed properties.

Parameters

string $name

Property name.

mixed $value

Value to set.

Returns

void

_setModelClass() protected

_setModelClass(string $name): void

Set the modelClass property based on conventions.

If the property is already set it will not be overwritten

Parameters

string $name

Class name.

Returns

void

_templatePath() protected

_templatePath(): string

Get the templatePath based on controller name and request prefix.

Returns

string

afterFilter() public

afterFilter(Cake\Event\EventInterface $event): Cake\Http\Response|null|void

Called after the controller action is run and rendered.

Parameters

Cake\Event\EventInterface $event

An Event instance

Returns

Cake\Http\Response|null|void

Links

https://book.cakephp.org/4/en/controllers.html#request-life-cycle-callbacks

beforeFilter() public

beforeFilter(Cake\Event\EventInterface $event): Cake\Http\Response|null|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

Cake\Event\EventInterface $event

An Event instance

Returns

Cake\Http\Response|null|void

Links

https://book.cakephp.org/4/en/controllers.html#request-life-cycle-callbacks

beforeRedirect() public

beforeRedirect(Cake\Event\EventInterface $event, array|string $url, Cake\Http\Response $response): Cake\Http\Response|null|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

Cake\Event\EventInterface $event

An Event instance

array|string $url

A string or array-based URL pointing to another location within the app, or an absolute URL

Cake\Http\Response $response

The response object.

Returns

Cake\Http\Response|null|void

Links

https://book.cakephp.org/4/en/controllers.html#request-life-cycle-callbacks

beforeRender() public

beforeRender(Cake\Event\EventInterface $event): Cake\Http\Response|null|void

beforeRender callback.

Parameters

Cake\Event\EventInterface $event

Event.

Returns

Cake\Http\Response|null|void

chooseViewClass() protected

chooseViewClass(): string|null

Use the view classes defined on this controller to view selection based on content-type negotiation.

Returns

string|null

components() public

components(Cake\Controller\ComponentRegistry|null $components = null): Cake\Controller\ComponentRegistry

Get the component registry for this controller.

If called with the first parameter, it will be set as the controller $this->_components property

Parameters

Cake\Controller\ComponentRegistry|null $components optional

Component registry.

Returns

Cake\Controller\ComponentRegistry

createView() public

createView(string|null $viewClass = null): Cake\View\View

Constructs the view class instance based on the current configuration.

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.

disableAutoRender() public

disableAutoRender(): $this

Disable automatic action rendering.

Returns

$this

dispatchEvent() public

dispatchEvent(string $name, array|null $data = null, object|null $subject = null): Cake\Event\EventInterface

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\EventInterface

enableAutoRender() public

enableAutoRender(): $this

Enable automatic action rendering.

Returns

$this

fetchTable() public

fetchTable(string|null $alias = null, array<string, mixed> $options = []): Cake\ORM\Table

Convenience method to get a table instance.

Parameters

string|null $alias optional

The alias name you want to get. Should be in CamelCase format. If null then the value of $defaultTable property is used.

array<string, mixed> $options optional

The options you want to build the table with. If a table has already been loaded the registry options will be ignored.

Returns

Cake\ORM\Table

Throws

Cake\Core\Exception\CakeException
If `$alias` argument and `$defaultTable` property both are `null`.

See Also

\Cake\ORM\TableLocator::get()

getAction() public

getAction(): Closure

Get the closure for action to be invoked by ControllerFactory.

Returns

Closure

Throws

Cake\Controller\Exception\MissingActionException

getEventManager() 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\EventManagerInterface

getMiddleware() public

getMiddleware(): array

Get middleware to be applied for this controller.

Returns

array

getModelType() public

getModelType(): string

Get the model type to be used by this class

Returns

string

getName() public

getName(): string

Returns the controller name.

Returns

string

getPlugin() public

getPlugin(): string|null

Returns the plugin name.

Returns

string|null

getRequest() public

getRequest(): Cake\Http\ServerRequest

Gets the request instance.

Returns

Cake\Http\ServerRequest

getResponse() public

getResponse(): Cake\Http\Response

Gets the response instance.

Returns

Cake\Http\Response

getTableLocator() public

getTableLocator(): Cake\ORM\Locator\LocatorInterface

Gets the table locator.

Returns

Cake\ORM\Locator\LocatorInterface

implementedEvents() public

implementedEvents(): array<string, mixed>

Returns a list of all events that will fire in the controller during its lifecycle. You can override this function to add your own listener callbacks

Example:

public function implementedEvents()
 {
     return [
         'Order.complete' => 'sendEmail',
         'Article.afterBuy' => 'decrementInventory',
         'User.onRegister' => ['callable' => 'logRegistration', 'priority' => 20, 'passParams' => true]
     ];
 }

Returns

array<string, mixed>

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(Closure $action, array $args): void

Dispatches the controller action.

Parameters

Closure $action

The action closure.

array $args

The arguments to be passed when invoking action.

Returns

void

Throws

UnexpectedValueException
If return value of action is not `null` or `ResponseInterface` instance.

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

Throws

ReflectionException

isAutoRenderEnabled() public

isAutoRenderEnabled(): bool

Returns true if an action should be rendered automatically.

Returns

bool

loadComponent() public

loadComponent(string $name, array<string, mixed> $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('Authentication.Authentication');

Will result in a Authentication property being set.

Parameters

string $name

The name of the component to load.

array<string, mixed> $config optional

The config for the component.

Returns

Cake\Controller\Component

Throws

Exception

loadModel() public

loadModel(string|null $modelClass = null, string|null $modelType = null): Cake\Datasource\RepositoryInterface

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. The name can be an alias like 'Post' or FQCN like App\Model\Table\PostsTable::class.

string|null $modelType optional

The type of repository to load. Defaults to the getModelType() value.

Returns

Cake\Datasource\RepositoryInterface

Throws

Cake\Datasource\Exception\MissingModelException
If the model class cannot be found.
UnexpectedValueException
If $modelClass argument is not provided and ModelAwareTrait::$modelClass property value is empty.

log() public

log(string $message, string|int $level = LogLevel::ERROR, array|string $context = []): bool

Convenience method to write a message to Log. See Log::write() for more information on writing to logs.

Parameters

string $message

Log message.

string|int $level optional

Error level.

array|string $context optional

Additional log data relevant to this message.

Returns

bool

middleware() public

middleware(Psr\Http\Server\MiddlewareInterfaceClosure|string $middleware, array<string, mixed> $options = []): void

Register middleware for the controller.

Parameters

Psr\Http\Server\MiddlewareInterfaceClosure|string $middleware

Middleware.

array<string, mixed> $options optional

Valid options:

  • only: (array|string) Only run the middleware for specified actions.
  • except: (array|string) Run the middleware for all actions except the specified ones.

Returns

void

modelFactory() public

modelFactory(string $type, Cake\Datasource\Locator\LocatorInterface|callable $factory): void

Override a existing callable to generate repositories of a given type.

Parameters

string $type

The name of the repository type the factory function is for.

Cake\Datasource\Locator\LocatorInterface|callable $factory

The factory function used to create instances.

Returns

void

paginate() public

paginate(Cake\ORM\TableCake\ORM\Query|string|null $object = null, array<string, mixed> $settings = []): Cake\ORM\ResultSetCake\Datasource\ResultSetInterface

Handles pagination of records in Table objects.

Will load the referenced Table object, and have the paginator 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\TableCake\ORM\Query|string|null $object optional

Table to paginate (e.g: Table instance, 'TableName' or a Query object)

array<string, mixed> $settings optional

The settings/configuration used for pagination.

Returns

Cake\ORM\ResultSetCake\Datasource\ResultSetInterface

Throws

RuntimeException
When no compatible table object can be found.

Links

https://book.cakephp.org/4/en/controllers.html#paginating-a-model

redirect() public

redirect(Psr\Http\Message\UriInterface|array|string $url, int $status = 302): Cake\Http\Response|null

Redirects to given $url, after turning off $this->autoRender.

Parameters

Psr\Http\Message\UriInterface|array|string $url

A string, array-based URL or UriInterface instance.

int $status optional

HTTP status code. Defaults to 302.

Returns

Cake\Http\Response|null

Links

https://book.cakephp.org/4/en/controllers.html#Controller::redirect

referer() public

referer(array|string|null $default = '/', bool $local = true): string

Returns the referring URL for this request.

Parameters

array|string|null $default optional

Default URL to use if HTTP_REFERER cannot be read from headers

bool $local optional

If false, do not restrict referring URLs to local server. Careful with trusting external sources.

Returns

string

render() public

render(string|null $template = null, string|null $layout = null): Cake\Http\Response

Instantiates the correct view class, hands it its data, and uses it to render the view output.

Parameters

string|null $template optional

Template to use for rendering

string|null $layout optional

Layout to use

Returns

Cake\Http\Response

Links

https://book.cakephp.org/4/en/controllers.html#rendering-a-view

set() public

set(array|string $name, mixed $value = null): $this

Saves a variable or an associative array of variables for use inside a template.

Parameters

array|string $name

A string or an array of data.

mixed $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 ...$args): 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.

mixed ...$args

Arguments passed to the action

Returns

mixed

setEventManager() 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

$this

setModelType() public

setModelType(string $modelType): $this

Set the model type to be used by this class

Parameters

string $modelType

The model type

Returns

$this

setName() public

setName(string $name): $this

Sets the controller name.

Parameters

string $name

Controller name.

Returns

$this

setPlugin() public

setPlugin(string|null $name): $this

Sets the plugin name.

Parameters

string|null $name

Plugin name.

Returns

$this

setRequest() public

setRequest(Cake\Http\ServerRequest $request): $this

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

Parameters

Cake\Http\ServerRequest $request

Request instance.

Returns

$this

setResponse() public

setResponse(Cake\Http\Response $response): $this

Sets the response instance.

Parameters

Cake\Http\Response $response

Response instance.

Returns

$this

setTableLocator() public

setTableLocator(Cake\ORM\Locator\LocatorInterface $tableLocator): $this

Sets the table locator.

Parameters

Cake\ORM\Locator\LocatorInterface $tableLocator

LocatorInterface instance.

Returns

$this

shutdownProcess() public

shutdownProcess(): Psr\Http\Message\ResponseInterface|null

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

Psr\Http\Message\ResponseInterface|null

startupProcess() public

startupProcess(): Psr\Http\Message\ResponseInterface|null

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

Psr\Http\Message\ResponseInterface|null

viewBuilder() public

viewBuilder(): Cake\View\ViewBuilder

Get the view builder being used.

Returns

Cake\View\ViewBuilder

viewClasses() public

viewClasses(): array<string>

Get the View classes this controller can perform content negotiation with.

Each view class must implement the getContentType() hook method to participate in negotiation.

Returns

array<string>

See Also

Cake\Http\ContentTypeNegotiation

Property Detail

$Auth public @property

Type

Cake\Controller\Component\AuthComponent

$Flash public @property

Type

Cake\Controller\Component\FlashComponent

$FormProtection public @property

Type

Cake\Controller\Component\FormProtectionComponent

$Paginator public @property

Type

Cake\Controller\Component\PaginatorComponent

$RequestHandler public @property

Type

Cake\Controller\Component\RequestHandlerComponent

$Security public @property

Type

Cake\Controller\Component\SecurityComponent

$_components protected

Instance of ComponentRegistry used to create Components

Type

Cake\Controller\ComponentRegistry|null

$_eventClass protected

Default class name for new event objects.

Type

string

$_eventManager protected

Instance of the Cake\Event\EventManager this object is using to dispatch inner events.

Type

Cake\Event\EventManagerInterface|null

$_modelFactories protected

A list of overridden model factory functions.

Type

array<callableCake\Datasource\Locator\LocatorInterface>

$_modelType protected

The model type to use.

Type

string

$_tableLocator protected

Table locator instance

Type

Cake\ORM\Locator\LocatorInterface|null

$_viewBuilder protected

The view builder instance being used.

Type

Cake\View\ViewBuilder|null

$autoRender protected

Set to true to automatically render the view after action logic.

Type

bool

$defaultTable protected

This object's default table alias.

Type

string|null

$middlewares protected

Middlewares list.

Type

array

$modelClass protected deprecated

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.

Use empty string to not use auto-loading on this object. Null auto-detects based on controller name.

Type

string|null

$name protected

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

$plugin protected

Automatically set to the name of a plugin.

Type

string|null

$request protected

An instance of a \Cake\Http\ServerRequest 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\Http\ServerRequest

$response protected

An instance of a Response object that contains information about the impending response

Type

Cake\Http\Response

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.Controller.ErrorController.html