PHP Class 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.
See also: lithium\net\http\Media
See also: lithium\action\Dispatcher
See also: lithium\action\Controller::$_render
Inheritance: extends lithium\core\Object
Show file Open project: unionofrad/lithium Class Usage Examples

Public Properties

Property Type Description
$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 Properties

Property Type Description
$_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.

Public Methods

Method Description
__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

Protected Methods

Method Description
_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.

Method Details

__construct() public method

Constructor.
See also: lithium\action\Controller::$request
See also: lithium\action\Controller::$response
See also: lithium\action\Controller::$_render
See also: 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_
return void

__invoke() public method

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.
return object Returns the response object associated with this controller.

_init() protected method

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
return void

redirect() public method

Creates a redirect response by calling render() and providing a 'location' parameter.
See also: lithium\net\http\Router::match()
See also: 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.
return object Returns the instance of the `Response` object associated with this controller.

render() public method

Uses results (typically coming from a controller action) to generate content and headers for a Response object.
See also: 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.
return object Returns the `Response` object associated with this `Controller` instance.

set() public method

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.
return void

Property Details

$_autoConfig protected property

Auto configuration properties.
protected array $_autoConfig
return array

$_classes protected property

Lists Controller's class dependencies. For details on extending or replacing a class, please refer to that class's API.
protected array $_classes
return array

$_render protected property

- 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.
See also: lithium\action\Controller::render()
See also: lithium\net\http\Media::type()
See also: lithium\net\http\Media::render()
protected array $_render
return array

$request public property

$this->request->controller or $this->request->action.
See also: lithium\action\Request
public object $request
return object

$response public property

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.
See also: lithium\action\Response
public object $response
return object