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.2 API

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