Class Dispatcher

Dispatcher converts Requests into controller actions. It uses the dispatched Request to locate and load the correct controller. If found, the requested action is called on the controller.

Dispatcher implements CakeEventListener

Direct Subclasses

Package: Cake\Routing
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Routing/Dispatcher.php

Properties summary

Method Summary

  • __construct() public
    Constructor.
  • _attachFilters() protected

    Attaches all event listeners for this dispatcher instance. Loads the dispatcher filters from the configured locations.

  • _getController() protected
    Get controller to use, either plugin controller or application controller
  • _invoke() protected

    Initializes the components and models a controller will be using. Triggers the controller action, and invokes the rendering if Controller::$autoRender is true and echo's the output. Otherwise the return value of the controller action are returned.

  • _loadController() protected
    Load controller and return controller class name
  • dispatch() public

    Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set to autoRender, via Controller::$autoRender, then Dispatcher will render the view.

  • Returns the CakeEventManager instance or creates one if none was created. Attaches the default listeners and filters

  • Returns the list of events this object listens to.
  • parseParams() public

    Applies Routing and additionalParameters to the request to be dispatched. If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run.

Method Detail

__construct()source public

__construct( string $base false )

Constructor.

Parameters

string $base optional false
The base directory for the application. Writes App.base to Configure.

_attachFilters()source protected

_attachFilters( CakeEventManager $manager )

Attaches all event listeners for this dispatcher instance. Loads the dispatcher filters from the configured locations.

Parameters

CakeEventManager $manager
Event manager instance.

Throws

MissingDispatcherFilterException

_getController()source protected

_getController( CakeRequest $request , CakeResponse $response )

Get controller to use, either plugin controller or application controller

Parameters

CakeRequest $request
Request object
CakeResponse $response
Response for the controller.

Returns

mixed
name of controller if not loaded, or object if loaded

_invoke()source protected

_invoke( Controller $controller , CakeRequest $request )

Initializes the components and models a controller will be using. Triggers the controller action, and invokes the rendering if Controller::$autoRender is true and echo's the output. Otherwise the return value of the controller action are returned.

Parameters

Controller $controller
Controller to invoke
CakeRequest $request
The request object to invoke the controller for.

Returns

CakeResponse
the resulting response object

_loadController()source protected

_loadController( CakeRequest $request )

Load controller and return controller class name

Parameters

CakeRequest $request
Request instance.

Returns

string|boolean
Name of controller class name

dispatch()source public

dispatch( CakeRequest $request , CakeResponse $response , array $additionalParams array() )

Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set to autoRender, via Controller::$autoRender, then Dispatcher will render the view.

Actions in CakePHP can be any public method on a controller, that is not declared in Controller. If you want controller methods to be public and in-accessible by URL, then prefix them with a _. For example public function _loadPosts() { } would not be accessible via URL. Private and protected methods are also not accessible via URL.

If no controller of given name can be found, invoke() will throw an exception. If the controller is found, and the action is not found an exception will be thrown.

Parameters

CakeRequest $request
Request object to dispatch.
CakeResponse $response
Response object to put the results of the dispatch into.
array $additionalParams optional array()
Settings array ("bare", "return") which is melded with the GET and POST params

Returns

string|null
if $request['return'] is set then it returns response body, null otherwise

Throws

MissingControllerException
When the controller is missing.

Triggers

Dispatcher.beforeDispatch $this, compact('request', 'response', 'additionalParams')
Dispatcher.afterDispatch $this, compact('request', 'response')

getEventManager()source public

getEventManager( )

Returns the CakeEventManager instance or creates one if none was created. Attaches the default listeners and filters

Returns

CakeEventManager

implementedEvents()source public

implementedEvents( )

Returns the list of events this object listens to.

Returns

array

Implementation of

CakeEventListener::implementedEvents()

parseParams()source public

parseParams( CakeEvent $event )

Applies Routing and additionalParameters to the request to be dispatched. If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run.

Parameters

CakeEvent $event
containing the request, response and additional params

Properties detail

$_eventManagersource

protected CakeEventManager

Event manager, used to handle dispatcher filters

© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/2.10/class-Dispatcher.html