PHP Класс lithium\action\Controller

Each controller has a series of 'actions' which are defined as class methods of the Controller classes. Each action has a specific responsibility, such as listing a set of objects, updating an object, or deleting an object. A controller object is instantiated by the Dispatcher (lithium\action\Dispatcher), and is given an instance of the lithium\action\Request class, which contains all necessary request state, including routing information, GET & POST data, and server variables. The controller is then invoked (using PHP's magic __invoke() syntax), and the proper action is called, according to the routing information stored in the Request object. A controller then returns a response (i.e. using redirect() or render()) which includes HTTP headers, and/or a serialized data response (JSON or XML, etc.) or HTML webpage. For more information on returning serialized data responses for web services, or manipulating template rendering from within your controllers, see the settings in $_render and the lithium\net\http\Media class.
См. также: lithium\net\http\Media
См. также: lithium\action\Dispatcher
См. также: lithium\action\Controller::$_render
Наследование: extends lithium\core\Object
Показать файл Открыть проект Примеры использования класса

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

Свойство Тип Описание
$request object $this->request->controller or $this->request->action.
$response object Contains an instance of the Response object which aggregates the headers and body content to be written back to the client (browser) when the result of the request is rendered.

Защищенные свойства (Protected)

Свойство Тип Описание
$_autoConfig array Auto configuration properties.
$_classes array Lists Controller's class dependencies. For details on extending or replacing a class, please refer to that class's API.
$_render array - The 'type' key is the content type that will be rendered by default, unless another is explicitly specified (defaults to 'html'). - The 'data' key contains an associative array of variables to be sent to the view, including any variables created in set(), or if an action returns any variables (as an associative array). - When an action is invoked, it will by default attempt to render a response, set the 'auto' key to false to prevent this behavior. - If you manually call render() within an action, the 'hasRendered' key stores this state, so that responses are not rendered multiple times, either manually or automatically. - The 'layout' key specifies the name of the layout to be used (defaults to 'default'). Typically, layout files are looked up as /views/layouts/..php. Based on the default settings, the actual path would be path-to-app/views/layouts/default.html.php. - Though typically introspected from the action that is executed, the 'template' key can be manually specified. This sets the template to be rendered, and is looked up (by default) as /views//..php, i.e.: path-to-app/views/posts/index.html.php. - To enable automatic content-type negotiation (i.e. determining the content type of the response based on the value of the HTTP Accept header), set the 'negotiate' flag to true. Otherwise, the response will only be based on the type parameter of the request object (defaulting to 'html' if no type is present in the Request parameters). Keep in mind that most of these settings may be passed to Controller::render() as well. To change how these settings operate (i.e. template paths, default render settings for individual media types), see the Media class.

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

Метод Описание
__construct ( array $config = [] ) : void Constructor.
__invoke ( object $request, array $dispatchParams, array $options = [] ) : object Called by the Dispatcher class to invoke an action.
redirect ( mixed $url, array $options = [] ) : object Creates a redirect response by calling render() and providing a 'location' parameter.
render ( array $options = [] ) : object Uses results (typically coming from a controller action) to generate content and headers for a Response object.
set ( array $data = [] ) : void This method is used to pass along any data from the controller to the view and layout

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

Метод Описание
_init ( ) : void Populates the $response property with a new instance of the Response class passing it configuration, and sets some rendering options, depending on the incoming request.

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

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

Constructor.
См. также: lithium\action\Controller::$request
См. также: lithium\action\Controller::$response
См. также: lithium\action\Controller::$_render
См. также: lithium\action\Controller::$_classes
public __construct ( array $config = [] ) : void
$config array Available configuration options are: - `'request'` _object|null_: Either a request object or `null`. - `'response'` _array_: Options for constructing the response object. - `'render'` _array_: Rendering control options. - `'classes'` _array_
Результат void

__invoke() публичный Метод

Called by the Dispatcher class to invoke an action.
public __invoke ( object $request, array $dispatchParams, array $options = [] ) : object
$request object The request object with URL and HTTP info for dispatching this action.
$dispatchParams array The array of parameters that will be passed to the action.
$options array The dispatch options for this action.
Результат object Returns the response object associated with this controller.

_init() защищенный Метод

Populates the $response property with a new instance of the Response class passing it configuration, and sets some rendering options, depending on the incoming request.
protected _init ( ) : void
Результат void

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

Creates a redirect response by calling render() and providing a 'location' parameter.
См. также: lithium\net\http\Router::match()
См. также: lithium\action\Controller::$response
public redirect ( mixed $url, array $options = [] ) : object
$url mixed The location to redirect to, provided as a string relative to the root of the application, a fully-qualified URL, or an array of routing parameters to be resolved to a URL. Post-processed by `Router::match()`.
$options array Options when performing the redirect. Available options include: - `'status'` _integer_: The HTTP status code associated with the redirect. Defaults to `302`. - `'head'` _boolean_: Determines whether only headers are returned with the response. Defaults to `true`, in which case only headers and no body are returned. Set to `false` to render a body as well. - `'exit'` _boolean_: Exit immediately after rendering. Defaults to `false`. Because `redirect()` does not exit by default, you should always prefix calls with a `return` statement, so that the action is always immediately exited.
Результат object Returns the instance of the `Response` object associated with this controller.

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

Uses results (typically coming from a controller action) to generate content and headers for a Response object.
См. также: lithium\action\Controller::$_render
public render ( array $options = [] ) : object
$options array An array of options, as follows: - `'data'`: An associative array of variables to be assigned to the template. These are merged on top of any variables set in `Controller::set()`. - `'head'`: If true, only renders the headers of the response, not the body. Defaults to `false`. - `'template'`: The name of a template, which usually matches the name of the action. By default, this template is looked for in the views directory of the current controller, i.e. given a `PostsController` object, if template is set to `'view'`, the template path would be `views/posts/view.html.php`. Defaults to the name of the action being rendered. The options specified here are merged with the values in the `Controller::$_render` property. You may refer to it for other options accepted by this method.
Результат object Returns the `Response` object associated with this `Controller` instance.

set() публичный Метод

This method is used to pass along any data from the controller to the view and layout
public set ( array $data = [] ) : void
$data array sets of ` => ` to pass to view layer.
Результат void

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

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

Auto configuration properties.
protected array $_autoConfig
Результат array

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

Lists Controller's class dependencies. For details on extending or replacing a class, please refer to that class's API.
protected array $_classes
Результат array

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

- The 'type' key is the content type that will be rendered by default, unless another is explicitly specified (defaults to 'html'). - The 'data' key contains an associative array of variables to be sent to the view, including any variables created in set(), or if an action returns any variables (as an associative array). - When an action is invoked, it will by default attempt to render a response, set the 'auto' key to false to prevent this behavior. - If you manually call render() within an action, the 'hasRendered' key stores this state, so that responses are not rendered multiple times, either manually or automatically. - The 'layout' key specifies the name of the layout to be used (defaults to 'default'). Typically, layout files are looked up as /views/layouts/..php. Based on the default settings, the actual path would be path-to-app/views/layouts/default.html.php. - Though typically introspected from the action that is executed, the 'template' key can be manually specified. This sets the template to be rendered, and is looked up (by default) as /views//..php, i.e.: path-to-app/views/posts/index.html.php. - To enable automatic content-type negotiation (i.e. determining the content type of the response based on the value of the HTTP Accept header), set the 'negotiate' flag to true. Otherwise, the response will only be based on the type parameter of the request object (defaulting to 'html' if no type is present in the Request parameters). Keep in mind that most of these settings may be passed to Controller::render() as well. To change how these settings operate (i.e. template paths, default render settings for individual media types), see the Media class.
См. также: lithium\action\Controller::render()
См. также: lithium\net\http\Media::type()
См. также: lithium\net\http\Media::render()
protected array $_render
Результат array

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

$this->request->controller or $this->request->action.
См. также: lithium\action\Request
public object $request
Результат object

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

Contains an instance of the Response object which aggregates the headers and body content to be written back to the client (browser) when the result of the request is rendered.
См. также: lithium\action\Response
public object $response
Результат object