PHP Класс 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.
Наследование: 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
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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)

Свойство Тип Описание
$_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.

Открытые методы

Метод Описание
__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.

Защищенные методы

Метод Описание
_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.

Описание методов

__construct() публичный метод

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() публичный метод

Magic accessor for model autoloading.
public __get ( string $name ) : boolean | object
$name string Property name
Результат boolean | object The model instance or false

__set() публичный метод

Magic setter for removed properties.
public __set ( string $name, mixed $value ) : void
$name string Property name.
$value mixed Value to set.
Результат void

_loadComponents() защищенный метод

Loads the defined components using the Component factory.
protected _loadComponents ( ) : void
Результат void

_mergeControllerVars() защищенный метод

Merge components, helpers vars from parent classes.
protected _mergeControllerVars ( ) : void
Результат void

_viewPath() защищенный метод

Get the viewPath based on controller name and request prefix.
protected _viewPath ( ) : string
Результат string

afterFilter() публичный метод

Called after the controller action is run and rendered.
public afterFilter ( Cake\Event\Event $event ) : Response | null
$event Cake\Event\Event An Event instance
Результат Cake\Network\Response | null

beforeFilter() публичный метод

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
Результат Cake\Network\Response | null

beforeRedirect() публичный метод

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.
Результат Cake\Network\Response | null

beforeRender() публичный метод

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
Результат Cake\Network\Response | null

components() публичный метод

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.
Результат Cake\Controller\ComponentRegistry

implementedEvents() публичный метод

You can override this function to add your own listener callbacks
public implementedEvents ( ) : array
Результат array

initialize() публичный метод

Implement this method to avoid having to overwrite the constructor and call parent.
public initialize ( ) : void
Результат void

invokeAction() публичный метод

Dispatches the controller action. Checks that the action exists and isn't private.
public invokeAction ( ) : mixed
Результат mixed The resulting response.

isAction() публичный метод

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.
Результат boolean Whether or not the method is accessible from a URL.

loadComponent() публичный метод

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.
Результат Component

paginate() публичный метод

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.
Результат Cake\ORM\ResultSet Query results

redirect() публичный метод

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)
Результат Cake\Network\Response | null

referer() публичный метод

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
Результат string Referring URL

render() публичный метод

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
Результат Cake\Network\Response A response object containing the rendered view.

setAction() публичный метод

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.
Результат mixed Returns the return value of the called action

setRequest() публичный метод

- $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.
Результат void

shutdownProcess() публичный метод

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
Результат Cake\Network\Response | null

startupProcess() публичный метод

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
Результат Cake\Network\Response | null

Описание свойств

$View публичное свойство

Instance of the View created during rendering. Won't be set until after Controller::render() is called.
Устаревший: 3.1.0 Use viewBuilder() instead.
public View,Cake\View $View
Результат Cake\View\View

$_components защищенное свойство

Instance of ComponentRegistry used to create Components
protected ComponentRegistry,Cake\Controller $_components
Результат Cake\Controller\ComponentRegistry

$_responseClass защищенное свойство

The class name to use for creating the response object.
protected string $_responseClass
Результат string

$_validViewOptions защищенное свойство

These Controller properties will be passed from the Controller to the View as options.
См. также: Cake\View\View
protected array $_validViewOptions
Результат array

$autoRender публичное свойство

Set to true to automatically render the view after action logic.
public bool $autoRender
Результат boolean

$components публичное свойство

Example: public $components = ['RequestHandler', 'Acl'];
public array $components
Результат array

$helpers публичное свойство

Example: public $helpers = ['Form', 'Html', 'Time'];
public array $helpers
Результат array

$name публичное свойство

Set automatically using conventions in Controller::__construct().
public string $name
Результат string

$paginate публичное свойство

Used to pre-configure pagination preferences for the various tables your controller will be paginating.
См. также: Cake\Controller\Component\PaginatorComponent
public array $paginate
Результат array

$passedArgs публичное свойство

Holds all passed params.
Устаревший: 3.1.0 Use `$this->request->params['pass']` instead.
public array $passedArgs
Результат array

$plugin публичное свойство

Automatically set to the name of a plugin.
public string $plugin
Результат string

$request публичное свойство

This object contains all the information about a request and several methods for reading additional information about the request.
public Request,Cake\Network $request
Результат Cake\Network\Request

$response публичное свойство

An instance of a Response object that contains information about the impending response
public Response,Cake\Network $response
Результат Cake\Network\Response