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
Afficher le fichier Open project: cakephp/cakephp Class Usage Examples

Méthodes publiques

Свойство 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

Свойство 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.

Méthodes publiques

Méthode 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.

Méthodes protégées

Méthode 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 méthode

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 méthode

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

__set() public méthode

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

_loadComponents() protected méthode

Loads the defined components using the Component factory.
protected _loadComponents ( ) : void
Résultat void

_mergeControllerVars() protected méthode

Merge components, helpers vars from parent classes.
protected _mergeControllerVars ( ) : void
Résultat void

_viewPath() protected méthode

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

afterFilter() public méthode

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

beforeFilter() public méthode

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
Résultat Cake\Network\Response | null

beforeRedirect() public méthode

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.
Résultat Cake\Network\Response | null

beforeRender() public méthode

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
Résultat Cake\Network\Response | null

components() public méthode

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.
Résultat Cake\Controller\ComponentRegistry

implementedEvents() public méthode

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

initialize() public méthode

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

invokeAction() public méthode

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

isAction() public méthode

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.
Résultat boolean Whether or not the method is accessible from a URL.

loadComponent() public méthode

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.
Résultat Component

paginate() public méthode

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.
Résultat Cake\ORM\ResultSet Query results

redirect() public méthode

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)
Résultat Cake\Network\Response | null

referer() public méthode

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
Résultat string Referring URL

render() public méthode

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
Résultat Cake\Network\Response A response object containing the rendered view.

setAction() public méthode

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.
Résultat mixed Returns the return value of the called action

setRequest() public méthode

- $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.
Résultat void

shutdownProcess() public méthode

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
Résultat Cake\Network\Response | null

startupProcess() public méthode

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
Résultat Cake\Network\Response | null

Property Details

$View public_oe 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
Résultat Cake\View\View

$_components protected_oe property

Instance of ComponentRegistry used to create Components
protected ComponentRegistry,Cake\Controller $_components
Résultat Cake\Controller\ComponentRegistry

$_responseClass protected_oe property

The class name to use for creating the response object.
protected string $_responseClass
Résultat string

$_validViewOptions protected_oe property

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

$autoRender public_oe property

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

$components public_oe property

Example: public $components = ['RequestHandler', 'Acl'];
public array $components
Résultat array

$helpers public_oe property

Example: public $helpers = ['Form', 'Html', 'Time'];
public array $helpers
Résultat array

$name public_oe property

Set automatically using conventions in Controller::__construct().
public string $name
Résultat string

$paginate public_oe 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
Résultat array

$passedArgs public_oe property

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

$plugin public_oe property

Automatically set to the name of a plugin.
public string $plugin
Résultat string

$request public_oe 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
Résultat Cake\Network\Request

$response public_oe property

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