lithium\action\Controller::__invoke()

public method

Called by the Dispatcher class to invoke an action.

Parameters

  • object $request

    The request object with URL and HTTP info for dispatching this action.

  • array $dispatchParams

    The array of parameters that will be passed to the action.

  • array $options

    The dispatch options for this action.

Returns

object

Returns the response object associated with this controller.

Filter

Filter to execute logic before an action is invoked (i.e. custom access control) or after it has been called and has returned its response (i.e. for caching it).

Source

	public function __invoke($request, $dispatchParams, array $options = []) {
		$params = compact('request', 'dispatchParams', 'options');

		return Filters::run($this, __FUNCTION__, $params, function($params) {
			$dispatchParams = $params['dispatchParams'];

			$action = isset($dispatchParams['action']) ? $dispatchParams['action'] : 'index';
			$args = isset($dispatchParams['args']) ? $dispatchParams['args'] : [];

			if (substr($action, 0, 1) === '_' || method_exists(__CLASS__, $action)) {
				throw new DispatchException('Attempted to invoke a private method.');
			}
			if (!method_exists($this, $action)) {
				throw new DispatchException("Action `{$action}` not found.");
			}
			$this->_render['template'] = $this->_render['template'] ?: $action;

			if ($result = $this->invokeMethod($action, $args)) {
				if (is_string($result)) {
					$this->render(['text' => $result]);
					return $this->response;
				}
				if (is_array($result)) {
					$this->set($result);
				}
			}

			if (!$this->_render['hasRendered'] && $this->_render['auto']) {
				$this->render();
			}
			return $this->response;
		});
	}