CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.4 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.4
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • Dispatcher
  • DispatcherFilter
  • Router
  1: <?php
  2: /**
  3:  * Dispatcher takes the URL information, parses it for parameters and
  4:  * tells the involved controllers what to do.
  5:  *
  6:  * This is the heart of CakePHP's operation.
  7:  *
  8:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  9:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 10:  *
 11:  * Licensed under The MIT License
 12:  * For full copyright and license information, please see the LICENSE.txt
 13:  * Redistributions of files must retain the above copyright notice.
 14:  *
 15:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 16:  * @link          http://cakephp.org CakePHP(tm) Project
 17:  * @package       Cake.Routing
 18:  * @since         CakePHP(tm) v 0.2.9
 19:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 20:  */
 21: 
 22: App::uses('Router', 'Routing');
 23: App::uses('CakeRequest', 'Network');
 24: App::uses('CakeResponse', 'Network');
 25: App::uses('Controller', 'Controller');
 26: App::uses('Scaffold', 'Controller');
 27: App::uses('View', 'View');
 28: App::uses('Debugger', 'Utility');
 29: App::uses('CakeEvent', 'Event');
 30: App::uses('CakeEventManager', 'Event');
 31: App::uses('CakeEventListener', 'Event');
 32: 
 33: /**
 34:  * Dispatcher converts Requests into controller actions. It uses the dispatched Request
 35:  * to locate and load the correct controller. If found, the requested action is called on
 36:  * the controller.
 37:  *
 38:  * @package       Cake.Routing
 39:  */
 40: class Dispatcher implements CakeEventListener {
 41: 
 42: /**
 43:  * Event manager, used to handle dispatcher filters
 44:  *
 45:  * @var CakeEventManager
 46:  */
 47:     protected $_eventManager;
 48: 
 49: /**
 50:  * Constructor.
 51:  *
 52:  * @param string $base The base directory for the application. Writes `App.base` to Configure.
 53:  */
 54:     public function __construct($base = false) {
 55:         if ($base !== false) {
 56:             Configure::write('App.base', $base);
 57:         }
 58:     }
 59: 
 60: /**
 61:  * Returns the CakeEventManager instance or creates one if none was
 62:  * created. Attaches the default listeners and filters
 63:  *
 64:  * @return CakeEventManager
 65:  */
 66:     public function getEventManager() {
 67:         if (!$this->_eventManager) {
 68:             $this->_eventManager = new CakeEventManager();
 69:             $this->_eventManager->attach($this);
 70:             $this->_attachFilters($this->_eventManager);
 71:         }
 72:         return $this->_eventManager;
 73:     }
 74: 
 75: /**
 76:  * Returns the list of events this object listens to.
 77:  *
 78:  * @return array
 79:  */
 80:     public function implementedEvents() {
 81:         return array('Dispatcher.beforeDispatch' => 'parseParams');
 82:     }
 83: 
 84: /**
 85:  * Attaches all event listeners for this dispatcher instance. Loads the
 86:  * dispatcher filters from the configured locations.
 87:  *
 88:  * @param CakeEventManager $manager
 89:  * @return void
 90:  * @throws MissingDispatcherFilterException
 91:  */
 92:     protected function _attachFilters($manager) {
 93:         $filters = Configure::read('Dispatcher.filters');
 94:         if (empty($filters)) {
 95:             return;
 96:         }
 97: 
 98:         foreach ($filters as $filter) {
 99:             if (is_string($filter)) {
100:                 $filter = array('callable' => $filter);
101:             }
102:             if (is_string($filter['callable'])) {
103:                 list($plugin, $callable) = pluginSplit($filter['callable'], true);
104:                 App::uses($callable, $plugin . 'Routing/Filter');
105:                 if (!class_exists($callable)) {
106:                     throw new MissingDispatcherFilterException($callable);
107:                 }
108:                 $manager->attach(new $callable);
109:             } else {
110:                 $on = strtolower($filter['on']);
111:                 $options = array();
112:                 if (isset($filter['priority'])) {
113:                     $options = array('priority' => $filter['priority']);
114:                 }
115:                 $manager->attach($filter['callable'], 'Dispatcher.' . $on . 'Dispatch', $options);
116:             }
117:         }
118:     }
119: 
120: /**
121:  * Dispatches and invokes given Request, handing over control to the involved controller. If the controller is set
122:  * to autoRender, via Controller::$autoRender, then Dispatcher will render the view.
123:  *
124:  * Actions in CakePHP can be any public method on a controller, that is not declared in Controller. If you
125:  * want controller methods to be public and in-accessible by URL, then prefix them with a `_`.
126:  * For example `public function _loadPosts() { }` would not be accessible via URL. Private and protected methods
127:  * are also not accessible via URL.
128:  *
129:  * If no controller of given name can be found, invoke() will throw an exception.
130:  * If the controller is found, and the action is not found an exception will be thrown.
131:  *
132:  * @param CakeRequest $request Request object to dispatch.
133:  * @param CakeResponse $response Response object to put the results of the dispatch into.
134:  * @param array $additionalParams Settings array ("bare", "return") which is melded with the GET and POST params
135:  * @return string|void if `$request['return']` is set then it returns response body, null otherwise
136:  * @throws MissingControllerException When the controller is missing.
137:  */
138:     public function dispatch(CakeRequest $request, CakeResponse $response, $additionalParams = array()) {
139:         $beforeEvent = new CakeEvent('Dispatcher.beforeDispatch', $this, compact('request', 'response', 'additionalParams'));
140:         $this->getEventManager()->dispatch($beforeEvent);
141: 
142:         $request = $beforeEvent->data['request'];
143:         if ($beforeEvent->result instanceof CakeResponse) {
144:             if (isset($request->params['return'])) {
145:                 return $beforeEvent->result->body();
146:             }
147:             $beforeEvent->result->send();
148:             return;
149:         }
150: 
151:         $controller = $this->_getController($request, $response);
152: 
153:         if (!($controller instanceof Controller)) {
154:             throw new MissingControllerException(array(
155:                 'class' => Inflector::camelize($request->params['controller']) . 'Controller',
156:                 'plugin' => empty($request->params['plugin']) ? null : Inflector::camelize($request->params['plugin'])
157:             ));
158:         }
159: 
160:         $response = $this->_invoke($controller, $request, $response);
161:         if (isset($request->params['return'])) {
162:             return $response->body();
163:         }
164: 
165:         $afterEvent = new CakeEvent('Dispatcher.afterDispatch', $this, compact('request', 'response'));
166:         $this->getEventManager()->dispatch($afterEvent);
167:         $afterEvent->data['response']->send();
168:     }
169: 
170: /**
171:  * Initializes the components and models a controller will be using.
172:  * Triggers the controller action, and invokes the rendering if Controller::$autoRender is true and echo's the output.
173:  * Otherwise the return value of the controller action are returned.
174:  *
175:  * @param Controller $controller Controller to invoke
176:  * @param CakeRequest $request The request object to invoke the controller for.
177:  * @param CakeResponse $response The response object to receive the output
178:  * @return CakeResponse the resulting response object
179:  */
180:     protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) {
181:         $controller->constructClasses();
182:         $controller->startupProcess();
183: 
184:         $render = true;
185:         $result = $controller->invokeAction($request);
186:         if ($result instanceof CakeResponse) {
187:             $render = false;
188:             $response = $result;
189:         }
190: 
191:         if ($render && $controller->autoRender) {
192:             $response = $controller->render();
193:         } elseif (!($result instanceof CakeResponse) && $response->body() === null) {
194:             $response->body($result);
195:         }
196:         $controller->shutdownProcess();
197: 
198:         return $response;
199:     }
200: 
201: /**
202:  * Applies Routing and additionalParameters to the request to be dispatched.
203:  * If Routes have not been loaded they will be loaded, and app/Config/routes.php will be run.
204:  *
205:  * @param CakeEvent $event containing the request, response and additional params
206:  * @return void
207:  */
208:     public function parseParams($event) {
209:         $request = $event->data['request'];
210:         Router::setRequestInfo($request);
211:         $params = Router::parse($request->url);
212:         $request->addParams($params);
213: 
214:         if (!empty($event->data['additionalParams'])) {
215:             $request->addParams($event->data['additionalParams']);
216:         }
217:     }
218: 
219: /**
220:  * Get controller to use, either plugin controller or application controller
221:  *
222:  * @param CakeRequest $request Request object
223:  * @param CakeResponse $response Response for the controller.
224:  * @return mixed name of controller if not loaded, or object if loaded
225:  */
226:     protected function _getController($request, $response) {
227:         $ctrlClass = $this->_loadController($request);
228:         if (!$ctrlClass) {
229:             return false;
230:         }
231:         $reflection = new ReflectionClass($ctrlClass);
232:         if ($reflection->isAbstract() || $reflection->isInterface()) {
233:             return false;
234:         }
235:         return $reflection->newInstance($request, $response);
236:     }
237: 
238: /**
239:  * Load controller and return controller class name
240:  *
241:  * @param CakeRequest $request
242:  * @return string|boolean Name of controller class name
243:  */
244:     protected function _loadController($request) {
245:         $pluginName = $pluginPath = $controller = null;
246:         if (!empty($request->params['plugin'])) {
247:             $pluginName = $controller = Inflector::camelize($request->params['plugin']);
248:             $pluginPath = $pluginName . '.';
249:         }
250:         if (!empty($request->params['controller'])) {
251:             $controller = Inflector::camelize($request->params['controller']);
252:         }
253:         if ($pluginPath . $controller) {
254:             $class = $controller . 'Controller';
255:             App::uses('AppController', 'Controller');
256:             App::uses($pluginName . 'AppController', $pluginPath . 'Controller');
257:             App::uses($class, $pluginPath . 'Controller');
258:             if (class_exists($class)) {
259:                 return $class;
260:             }
261:         }
262:         return false;
263:     }
264: 
265: }
266: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs