PHP Class Cake\Controller\Controller

Provides basic functionality, such as rendering views inside layouts, automatic model availability, redirection, callbacks, and more. Controllers should provide a number of 'action' methods. These are public methods on a controller that are not inherited from Controller. Each action serves as an endpoint for performing a specific action on a resource or collection of resources. For example adding or editing a new object, or listing a set of objects. You can access request parameters, using $this->request. The request object contains all the POST, GET and FILES that were part of the request. After performing the required action, controllers are responsible for creating a response. This usually takes the form of a generated View, or possibly a redirection to another URL. In either case $this->response allows you to manipulate all aspects of the response. Controllers are created by Dispatcher based on request parameters and routing. By default controllers and actions use conventional names. For example /posts/index maps to PostsController::index(). You can re-map URLs using Router::connect() or RouterBuilder::connect(). ### Life cycle callbacks CakePHP fires a number of life cycle callbacks during each request. By implementing a method you can receive the related events. The available callbacks are: - beforeFilter(Event $event) Called before each action. This is a good place to do general logic that applies to all actions. - beforeRender(Event $event) Called before the view is rendered. - beforeRedirect(Event $event, $url, Response $response) Called before a redirect is done. - afterFilter(Event $event) Called after each action is complete and after the view is rendered.
Inheritance: implements Cake\Event\EventListenerInterface, implements Cake\Event\EventDispatcherInterface, use trait Cake\Event\EventDispatcherTrait, use trait Cake\ORM\Locator\LocatorAwareTrait, use trait Cake\Log\LogTrait, use trait Cake\Utility\MergeVariablesTrait, use trait Cake\Datasource\ModelAwareTrait, use trait Cake\Routing\RequestActionTrait, use trait Cake\View\ViewVarsTrait
Show file Open project: cakephp/cakephp Class Usage Examples

Public Properties

Property Type Description
$View Cake\View\View Instance of the View created during rendering. Won't be set until after Controller::render() is called.
$autoRender boolean Set to true to automatically render the view after action logic.
$components array Example: public $components = ['RequestHandler', 'Acl'];
$helpers array Example: public $helpers = ['Form', 'Html', 'Time'];
$name string Set automatically using conventions in Controller::__construct().
$paginate array Used to pre-configure pagination preferences for the various tables your controller will be paginating.
$passedArgs array Holds all passed params.
$plugin string Automatically set to the name of a plugin.
$request Cake\Network\Request This object contains all the information about a request and several methods for reading additional information about the request.
$response Cake\Network\Response An instance of a Response object that contains information about the impending response

Protected Properties

Property Type Description
$_components Cake\Controller\ComponentRegistry Instance of ComponentRegistry used to create Components
$_responseClass string The class name to use for creating the response object.
$_validViewOptions array These Controller properties will be passed from the Controller to the View as options.

Public Methods

Method Description
__construct ( Cake\Network\Request $request = null, Response $response = null, string | null $name = null, Cake\Event\EventManager | null $eventManager = null, Cake\Controller\ComponentRegistry | null $components = null ) Constructor.
__get ( string $name ) : boolean | object Magic accessor for model autoloading.
__set ( string $name, mixed $value ) : void Magic setter for removed properties.
afterFilter ( Cake\Event\Event $event ) : Response | null Called after the controller action is run and rendered.
beforeFilter ( Cake\Event\Event $event ) : Response | null 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 ( Cake\Event\Event $event, string | array $url, Response $response ) : Response | null The beforeRedirect method is invoked when the controller's redirect method is called but before any further action.
beforeRender ( Cake\Event\Event $event ) : Response | null 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 ( Cake\Controller\ComponentRegistry | null $components = null ) : Cake\Controller\ComponentRegistry Get the component registry for this controller.
implementedEvents ( ) : array Returns a list of all events that will fire in the controller during its lifecycle.
initialize ( ) : void Initialization hook method.
invokeAction ( ) : mixed Dispatches the controller action. Checks that the action exists and isn't private.
isAction ( string $action ) : boolean Method to check that an action is accessible from a URL.
loadComponent ( string $name, array $config = [] ) : Component Add a component to the controller's registry.
paginate ( Table | string | Query | null $object = null, array $settings = [] ) : Cake\ORM\ResultSet Handles pagination of records in Table objects.
redirect ( string | array $url, integer $status = 302 ) : Response | null Redirects to given $url, after turning off $this->autoRender.
referer ( string | array | null $default = null, boolean $local = false ) : string Returns the referring URL for this request.
render ( string | null $view = null, string | null $layout = null ) : Response Instantiates the correct view class, hands it its data, and uses it to render the view output.
setAction ( string $action ) : mixed Internally redirects one action to another. Does not perform another HTTP request unlike Controller::redirect()
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:
shutdownProcess ( ) : Response | null Perform the various shutdown processes for this controller.
startupProcess ( ) : Response | null Perform the startup process for this controller.

Protected Methods

Method Description
_loadComponents ( ) : void Loads the defined components using the Component factory.
_mergeControllerVars ( ) : void Merge components, helpers vars from parent classes.
_viewPath ( ) : string Get the viewPath based on controller name and request prefix.

Method Details

__construct() public method

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.
public __construct ( Cake\Network\Request $request = null, Response $response = null, string | null $name = null, Cake\Event\EventManager | null $eventManager = null, Cake\Controller\ComponentRegistry | null $components = null )
$request Cake\Network\Request Request object for this controller. Can be null for testing, but expect that features that use the request parameters will not work.
$response Cake\Network\Response Response object for this controller.
$name string | null Override the name useful in testing when using mocks.
$eventManager Cake\Event\EventManager | null The event manager. Defaults to a new instance.
$components Cake\Controller\ComponentRegistry | null The component registry. Defaults to a new instance.

__get() public method

Magic accessor for model autoloading.
public __get ( string $name ) : boolean | object
$name string Property name
return boolean | object The model instance or false

__set() public method

Magic setter for removed properties.
public __set ( string $name, mixed $value ) : void
$name string Property name.
$value mixed Value to set.
return void

_loadComponents() protected method

Loads the defined components using the Component factory.
protected _loadComponents ( ) : void
return void

_mergeControllerVars() protected method

Merge components, helpers vars from parent classes.
protected _mergeControllerVars ( ) : void
return void

_viewPath() protected method

Get the viewPath based on controller name and request prefix.
protected _viewPath ( ) : string
return string

afterFilter() public method

Called after the controller action is run and rendered.
public afterFilter ( Cake\Event\Event $event ) : Response | null
$event Cake\Event\Event An Event instance
return Cake\Network\Response | null

beforeFilter() public method

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.
public beforeFilter ( Cake\Event\Event $event ) : Response | null
$event Cake\Event\Event An Event instance
return Cake\Network\Response | null

beforeRedirect() public method

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.
public beforeRedirect ( Cake\Event\Event $event, string | array $url, Response $response ) : Response | null
$event Cake\Event\Event An Event instance
$url string | array A string or array-based URL pointing to another location within the app, or an absolute URL
$response Cake\Network\Response The response object.
return Cake\Network\Response | null

beforeRender() public method

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.
public beforeRender ( Cake\Event\Event $event ) : Response | null
$event Cake\Event\Event An Event instance
return Cake\Network\Response | null

components() public method

If called with the first parameter, it will be set as the controller $this->_components property
public components ( Cake\Controller\ComponentRegistry | null $components = null ) : Cake\Controller\ComponentRegistry
$components Cake\Controller\ComponentRegistry | null Component registry.
return Cake\Controller\ComponentRegistry

implementedEvents() public method

You can override this function to add your own listener callbacks
public implementedEvents ( ) : array
return array

initialize() public method

Implement this method to avoid having to overwrite the constructor and call parent.
public initialize ( ) : void
return void

invokeAction() public method

Dispatches the controller action. Checks that the action exists and isn't private.
public invokeAction ( ) : mixed
return mixed The resulting response.

isAction() public method

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.
public isAction ( string $action ) : boolean
$action string The action to check.
return boolean Whether or not the method is accessible from a URL.

loadComponent() public method

This method will also set the component to a property. For example: $this->loadComponent('Acl.Acl'); Will result in a Toolbar property being set.
public loadComponent ( string $name, array $config = [] ) : Component
$name string The name of the component to load.
$config array The config for the component.
return Component

paginate() public method

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.
public paginate ( Table | string | Query | null $object = null, array $settings = [] ) : Cake\ORM\ResultSet
$object Cake\ORM\Table | string | Cake\ORM\Query | null Table to paginate (e.g: Table instance, 'TableName' or a Query object)
$settings array The settings/configuration used for pagination.
return Cake\ORM\ResultSet Query results

redirect() public method

Redirects to given $url, after turning off $this->autoRender.
public redirect ( string | array $url, integer $status = 302 ) : Response | null
$url string | array A string or array-based URL pointing to another location within the app, or an absolute URL
$status integer HTTP status code (eg: 301)
return Cake\Network\Response | null

referer() public method

Returns the referring URL for this request.
public referer ( string | array | null $default = null, boolean $local = false ) : string
$default string | array | null Default URL to use if HTTP_REFERER cannot be read from headers
$local boolean If true, restrict referring URLs to local server
return string Referring URL

render() public method

Instantiates the correct view class, hands it its data, and uses it to render the view output.
public render ( string | null $view = null, string | null $layout = null ) : Response
$view string | null View to use for rendering
$layout string | null Layout to use
return Cake\Network\Response A response object containing the rendered view.

setAction() public method

Examples: setAction('another_action'); setAction('action_with_parameters', $parameter1);
public setAction ( string $action ) : mixed
$action string The new action to be 'redirected' to. Any other parameters passed to this method will be passed as parameters to the new action.
return mixed Returns the return value of the called action

setRequest() public method

- $this->request - To the $request parameter - $this->plugin - To the $request->params['plugin'] - $this->passedArgs - Same as $request->params['pass] - View::$plugin - $this->plugin
public setRequest ( Cake\Network\Request $request ) : void
$request Cake\Network\Request Request instance.
return void

shutdownProcess() public method

Fire the Components and Controller callbacks in the correct order. - triggers the component shutdown callback. - calls the Controller's afterFilter method.
public shutdownProcess ( ) : Response | null
return Cake\Network\Response | null

startupProcess() public method

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.
public startupProcess ( ) : Response | null
return Cake\Network\Response | null

Property Details

$View public property

Instance of the View created during rendering. Won't be set until after Controller::render() is called.
Deprecation: 3.1.0 Use viewBuilder() instead.
public View,Cake\View $View
return Cake\View\View

$_components protected property

Instance of ComponentRegistry used to create Components
protected ComponentRegistry,Cake\Controller $_components
return Cake\Controller\ComponentRegistry

$_responseClass protected property

The class name to use for creating the response object.
protected string $_responseClass
return string

$_validViewOptions protected property

These Controller properties will be passed from the Controller to the View as options.
See also: Cake\View\View
protected array $_validViewOptions
return array

$autoRender public property

Set to true to automatically render the view after action logic.
public bool $autoRender
return boolean

$components public property

Example: public $components = ['RequestHandler', 'Acl'];
public array $components
return array

$helpers public property

Example: public $helpers = ['Form', 'Html', 'Time'];
public array $helpers
return array

$name public property

Set automatically using conventions in Controller::__construct().
public string $name
return string

$paginate public property

Used to pre-configure pagination preferences for the various tables your controller will be paginating.
See also: Cake\Controller\Component\PaginatorComponent
public array $paginate
return array

$passedArgs public property

Holds all passed params.
Deprecation: 3.1.0 Use `$this->request->params['pass']` instead.
public array $passedArgs
return array

$plugin public property

Automatically set to the name of a plugin.
public string $plugin
return string

$request public property

This object contains all the information about a request and several methods for reading additional information about the request.
public Request,Cake\Network $request
return Cake\Network\Request

$response public property

An instance of a Response object that contains information about the impending response
public Response,Cake\Network $response
return Cake\Network\Response