Controller.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since CakePHP(tm) v 0.2.9
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Controller;
  16. use Cake\Core\App;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Object;
  19. use Cake\Core\Plugin;
  20. use Cake\Error;
  21. use Cake\Event\Event;
  22. use Cake\Event\EventListener;
  23. use Cake\Event\EventManager;
  24. use Cake\Network\Request;
  25. use Cake\Network\Response;
  26. use Cake\ORM\TableRegistry;
  27. use Cake\Routing\RequestActionTrait;
  28. use Cake\Routing\Router;
  29. use Cake\Utility\Inflector;
  30. use Cake\Utility\MergeVariablesTrait;
  31. use Cake\Utility\ModelAwareTrait;
  32. use Cake\Utility\ViewVarsTrait;
  33. use Cake\View\View;
  34. /**
  35. * Application controller class for organization of business logic.
  36. * Provides basic functionality, such as rendering views inside layouts,
  37. * automatic model availability, redirection, callbacks, and more.
  38. *
  39. * Controllers should provide a number of 'action' methods. These are public methods on the controller
  40. * that are not prefixed with a '_' and not part of Controller. Each action serves as an endpoint for
  41. * performing a specific action on a resource or collection of resources. For example adding or editing a new
  42. * object, or listing a set of objects.
  43. *
  44. * You can access request parameters, using `$this->request`. The request object contains all the POST, GET and FILES
  45. * that were part of the request.
  46. *
  47. * After performing the required actions, controllers are responsible for creating a response. This usually
  48. * takes the form of a generated View, or possibly a redirection to another controller action. In either case
  49. * `$this->response` allows you to manipulate all aspects of the response.
  50. *
  51. * Controllers are created by Dispatcher based on request parameters and routing. By default controllers and actions
  52. * use conventional names. For example `/posts/index` maps to `PostsController::index()`. You can re-map URLs
  53. * using Router::connect().
  54. *
  55. * ### Life cycle callbacks
  56. *
  57. * CakePHP fires a number of life cycle callbacks during each request. By implementing a method
  58. * you can receive the related events. The available callbacks are:
  59. *
  60. * - `beforeFilter(Event $event)` - Called before the before each action. This is a good place to
  61. * do general logic that applies to all actions.
  62. * - `beforeRender(Event $event)` - Called before the view is rendered.
  63. * - `beforeRedirect(Cake\Event\Event $event $url, Cake\Network\Response $response)` - Called before
  64. * a redirect is done.
  65. * - `afterFilter(Event $event)` - Called after each action is complete and after the view is rendered.
  66. *
  67. * @property AclComponent $Acl
  68. * @property AuthComponent $Auth
  69. * @property CookieComponent $Cookie
  70. * @property EmailComponent $Email
  71. * @property PaginatorComponent $Paginator
  72. * @property RequestHandlerComponent $RequestHandler
  73. * @property SecurityComponent $Security
  74. * @property SessionComponent $Session
  75. * @link http://book.cakephp.org/2.0/en/controllers.html
  76. */
  77. class Controller extends Object implements EventListener {
  78. use MergeVariablesTrait;
  79. use ModelAwareTrait;
  80. use RequestActionTrait;
  81. use ViewVarsTrait;
  82. /**
  83. * The name of this controller. Controller names are plural, named after the model they manipulate.
  84. *
  85. * @var string
  86. * @link http://book.cakephp.org/2.0/en/controllers.html#controller-attributes
  87. */
  88. public $name = null;
  89. /**
  90. * An array containing the names of helpers this controller uses. The array elements should
  91. * not contain the "Helper" part of the class name.
  92. *
  93. * Example: `public $helpers = array('Html', 'Js', 'Time', 'Ajax');`
  94. *
  95. * @var mixed A single name as a string or a list of names as an array.
  96. * @link http://book.cakephp.org/2.0/en/controllers.html#components-helpers-and-uses
  97. */
  98. public $helpers = array();
  99. /**
  100. * An instance of a Cake\Network\Request object that contains information about the current request.
  101. * This object contains all the information about a request and several methods for reading
  102. * additional information about the request.
  103. *
  104. * @var Cake\Network\Request
  105. * @link http://book.cakephp.org/2.0/en/controllers/request-response.html#Request
  106. */
  107. public $request;
  108. /**
  109. * An instance of a Response object that contains information about the impending response
  110. *
  111. * @var Cake\Network\Response
  112. * @link http://book.cakephp.org/2.0/en/controllers/request-response.html#cakeresponse
  113. */
  114. public $response;
  115. /**
  116. * The class name to use for creating the response object.
  117. *
  118. * @var string
  119. */
  120. protected $_responseClass = 'Cake\Network\Response';
  121. /**
  122. * Settings for pagination.
  123. *
  124. * Used to pre-configure pagination preferences for the various
  125. * tables your controller will be paginating.
  126. *
  127. * @var array
  128. * @see Cake\Controller\Component\PaginatorComponent
  129. */
  130. public $paginate = [];
  131. /**
  132. * The name of the views subfolder containing views for this controller.
  133. *
  134. * @var string
  135. */
  136. public $viewPath = null;
  137. /**
  138. * The name of the layouts subfolder containing layouts for this controller.
  139. *
  140. * @var string
  141. */
  142. public $layoutPath = null;
  143. /**
  144. * The name of the view file to render. The name specified
  145. * is the filename in /app/View/<SubFolder> without the .ctp extension.
  146. *
  147. * @var string
  148. */
  149. public $view = null;
  150. /**
  151. * The name of the layout file to render the view inside of. The name specified
  152. * is the filename of the layout in /app/View/Layout without the .ctp
  153. * extension.
  154. *
  155. * @var string
  156. */
  157. public $layout = 'default';
  158. /**
  159. * The view theme to use.
  160. *
  161. * @see Cake\View\View::$theme
  162. * @var string
  163. */
  164. public $theme = null;
  165. /**
  166. * Set to true to automatically render the view
  167. * after action logic.
  168. *
  169. * @var boolean
  170. */
  171. public $autoRender = true;
  172. /**
  173. * Set to true to automatically render the layout around views.
  174. *
  175. * @var boolean
  176. */
  177. public $autoLayout = true;
  178. /**
  179. * Instance of ComponentRegistry used to create Components
  180. *
  181. * @var ComponentRegistry
  182. */
  183. public $Components = null;
  184. /**
  185. * Array containing the names of components this controller uses. Component names
  186. * should not contain the "Component" portion of the class name.
  187. *
  188. * Example: `public $components = array('Session', 'RequestHandler', 'Acl');`
  189. *
  190. * @var array
  191. * @link http://book.cakephp.org/2.0/en/controllers/components.html
  192. */
  193. public $components = array('Session');
  194. /**
  195. * The name of the View class this controller sends output to.
  196. *
  197. * @var string
  198. */
  199. public $viewClass = 'Cake\View\View';
  200. /**
  201. * Instance of the View created during rendering. Won't be set until after
  202. * Controller::render() is called.
  203. *
  204. * @var Cake\View\View
  205. */
  206. public $View;
  207. /**
  208. * File extension for view templates. Defaults to CakePHP's conventional ".ctp".
  209. *
  210. * @var string
  211. */
  212. public $ext = '.ctp';
  213. /**
  214. * Automatically set to the name of a plugin.
  215. *
  216. * @var string
  217. */
  218. public $plugin = null;
  219. /**
  220. * Used to define methods a controller that will be cached. To cache a
  221. * single action, the value is set to an array containing keys that match
  222. * action names and values that denote cache expiration times (in seconds).
  223. *
  224. * Example:
  225. *
  226. * {{{
  227. * public $cacheAction = array(
  228. * 'view/23/' => 21600,
  229. * 'recalled/' => 86400
  230. * );
  231. * }}}
  232. *
  233. * $cacheAction can also be set to a strtotime() compatible string. This
  234. * marks all the actions in the controller for view caching.
  235. *
  236. * @var mixed
  237. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/cache.html#additional-configuration-options
  238. */
  239. public $cacheAction = false;
  240. /**
  241. * Holds all passed params.
  242. *
  243. * @var mixed
  244. */
  245. public $passedArgs = array();
  246. /**
  247. * Holds current methods of the controller. This is a list of all the methods reachable
  248. * via URL. Modifying this array, will allow you to change which methods can be reached.
  249. *
  250. * @var array
  251. */
  252. public $methods = array();
  253. /**
  254. * Holds any validation errors produced by the last call of the validateErrors() method/
  255. *
  256. * @var array Validation errors, or false if none
  257. */
  258. public $validationErrors = null;
  259. /**
  260. * Instance of the Cake\Event\EventManager this controller is using
  261. * to dispatch inner events.
  262. *
  263. * @var Cake\Event\EventManager
  264. */
  265. protected $_eventManager = null;
  266. /**
  267. * Constructor.
  268. *
  269. * @param Cake\Network\Request $request Request object for this controller. Can be null for testing,
  270. * but expect that features that use the request parameters will not work.
  271. * @param Cake\Network\Response $response Response object for this controller.
  272. */
  273. public function __construct($request = null, $response = null) {
  274. if ($this->name === null) {
  275. list(, $this->name) = namespaceSplit(get_class($this));
  276. $this->name = substr($this->name, 0, -10);
  277. }
  278. if (!$this->viewPath) {
  279. $viewPath = $this->name;
  280. if (isset($request->params['prefix'])) {
  281. $viewPath = Inflector::camelize($request->params['prefix']) . DS . $viewPath;
  282. }
  283. $this->viewPath = $viewPath;
  284. }
  285. $this->_setModelClass($this->name);
  286. $this->modelFactory('Table', ['Cake\ORM\TableRegistry', 'get']);
  287. $childMethods = get_class_methods($this);
  288. $parentMethods = get_class_methods('Cake\Controller\Controller');
  289. $this->methods = array_diff($childMethods, $parentMethods);
  290. if ($request instanceof Request) {
  291. $this->setRequest($request);
  292. }
  293. if ($response instanceof Response) {
  294. $this->response = $response;
  295. }
  296. $this->Components = new ComponentRegistry($this);
  297. }
  298. /**
  299. * Provides backwards compatibility to avoid problems with empty and isset to alias properties.
  300. *
  301. * @param string $name
  302. * @return boolean
  303. */
  304. public function __get($name) {
  305. if ($name === $this->modelClass) {
  306. list($plugin, $class) = pluginSplit($name, true);
  307. if (!$plugin) {
  308. $plugin = $this->plugin ? $this->plugin . '.' : null;
  309. }
  310. $this->loadModel($plugin . $this->modelClass);
  311. return $this->{$this->modelClass};
  312. }
  313. return false;
  314. }
  315. /**
  316. * Sets the request objects and configures a number of controller properties
  317. * based on the contents of the request. The properties that get set are
  318. *
  319. * - $this->request - To the $request parameter
  320. * - $this->plugin - To the $request->params['plugin']
  321. * - $this->view - To the $request->params['action']
  322. * - $this->autoLayout - To the false if $request->params['bare']; is set.
  323. * - $this->autoRender - To false if $request->params['return'] == 1
  324. * - $this->passedArgs - The the combined results of params['named'] and params['pass]
  325. *
  326. * @param Cake\Network\Request $request
  327. * @return void
  328. */
  329. public function setRequest(Request $request) {
  330. $this->request = $request;
  331. $this->plugin = isset($request->params['plugin']) ? Inflector::camelize($request->params['plugin']) : null;
  332. $this->view = isset($request->params['action']) ? $request->params['action'] : null;
  333. if (isset($request->params['pass'])) {
  334. $this->passedArgs = $request->params['pass'];
  335. }
  336. if (!empty($request->params['return']) && $request->params['return'] == 1) {
  337. $this->autoRender = false;
  338. }
  339. if (!empty($request->params['bare'])) {
  340. $this->autoLayout = false;
  341. }
  342. }
  343. /**
  344. * Dispatches the controller action. Checks that the action
  345. * exists and isn't private.
  346. *
  347. * @param Cake\Network\Request $request
  348. * @return mixed The resulting response.
  349. * @throws Cake\Error\PrivateActionException When actions are not public or prefixed by _
  350. * @throws Cake\Error\MissingActionException When actions are not defined.
  351. */
  352. public function invokeAction(Request $request) {
  353. try {
  354. $method = new \ReflectionMethod($this, $request->params['action']);
  355. if ($this->_isPrivateAction($method, $request)) {
  356. throw new Error\PrivateActionException(array(
  357. 'controller' => $this->name . "Controller",
  358. 'action' => $request->params['action'],
  359. 'prefix' => isset($request->params['prefix']) ? $request->params['prefix'] : '',
  360. 'plugin' => $request->params['plugin'],
  361. ));
  362. }
  363. return $method->invokeArgs($this, $request->params['pass']);
  364. } catch (\ReflectionException $e) {
  365. throw new Error\MissingActionException(array(
  366. 'controller' => $this->name . "Controller",
  367. 'action' => $request->params['action'],
  368. 'prefix' => isset($request->params['prefix']) ? $request->params['prefix'] : '',
  369. 'plugin' => $request->params['plugin'],
  370. ));
  371. }
  372. }
  373. /**
  374. * Check if the request's action is marked as private, with an underscore,
  375. * or if the request is attempting to directly accessing a prefixed action.
  376. *
  377. * @param \ReflectionMethod $method The method to be invoked.
  378. * @param Cake\Network\Request $request The request to check.
  379. * @return boolean
  380. */
  381. protected function _isPrivateAction(\ReflectionMethod $method, Request $request) {
  382. $privateAction = (
  383. $method->name[0] === '_' ||
  384. !$method->isPublic() ||
  385. !in_array($method->name, $this->methods)
  386. );
  387. $prefixes = Router::prefixes();
  388. if (!$privateAction && !empty($prefixes)) {
  389. if (empty($request->params['prefix']) && strpos($request->params['action'], '_') > 0) {
  390. list($prefix) = explode('_', $request->params['action']);
  391. $privateAction = in_array($prefix, $prefixes);
  392. }
  393. }
  394. return $privateAction;
  395. }
  396. /**
  397. * Merge components, helpers vars from
  398. * parent classes.
  399. *
  400. * @return void
  401. */
  402. protected function _mergeControllerVars() {
  403. $pluginDot = null;
  404. if (!empty($this->plugin)) {
  405. $pluginDot = $this->plugin . '.';
  406. }
  407. $this->_mergeVars(
  408. ['components', 'helpers'],
  409. ['associative' => ['components', 'helpers']]
  410. );
  411. }
  412. /**
  413. * Returns a list of all events that will fire in the controller during it's lifecycle.
  414. * You can override this function to add you own listener callbacks
  415. *
  416. * @return array
  417. */
  418. public function implementedEvents() {
  419. return array(
  420. 'Controller.initialize' => 'beforeFilter',
  421. 'Controller.beforeRender' => 'beforeRender',
  422. 'Controller.beforeRedirect' => 'beforeRedirect',
  423. 'Controller.shutdown' => 'afterFilter',
  424. );
  425. }
  426. /**
  427. * Loads Model and Component classes.
  428. *
  429. * Using the $components properties, classes are loaded
  430. * and components have their callbacks attached to the EventManager.
  431. * It is also at this time that Controller callbacks are bound.
  432. *
  433. * See Controller::repository(); for more information on how models are loaded.
  434. *
  435. * @return mixed true if models found and instance created.
  436. * @see Controller::repository()
  437. * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::constructClasses
  438. * @throws MissingModelException
  439. */
  440. public function constructClasses() {
  441. $this->_mergeControllerVars();
  442. $this->_loadComponents();
  443. $this->getEventManager()->attach($this);
  444. return true;
  445. }
  446. /**
  447. * Loads the defined components using the Component factory.
  448. *
  449. * @return void
  450. */
  451. protected function _loadComponents() {
  452. if (empty($this->components)) {
  453. return;
  454. }
  455. $components = $this->Components->normalizeArray($this->components);
  456. foreach ($components as $properties) {
  457. list(, $class) = pluginSplit($properties['class']);
  458. $this->{$class} = $this->Components->load($properties['class'], $properties['settings']);
  459. }
  460. }
  461. /**
  462. * Returns the Cake\Event\EventManager manager instance for this controller.
  463. *
  464. * You can use this instance to register any new listeners or callbacks to the
  465. * controller events, or create your own events and trigger them at will.
  466. *
  467. * @return Cake\Event\EventManager
  468. */
  469. public function getEventManager() {
  470. if (empty($this->_eventManager)) {
  471. $this->_eventManager = new EventManager();
  472. }
  473. return $this->_eventManager;
  474. }
  475. /**
  476. * Overwrite the existing EventManager
  477. *
  478. * Useful for testing
  479. *
  480. * @param Cake\Event\EventManager $eventManager
  481. * @return void
  482. */
  483. public function setEventManager($eventManager) {
  484. $this->_eventManager = $eventManager;
  485. }
  486. /**
  487. * Perform the startup process for this controller.
  488. * Fire the Components and Controller callbacks in the correct order.
  489. *
  490. * - Initializes components, which fires their `initialize` callback
  491. * - Calls the controller `beforeFilter`.
  492. * - triggers Component `startup` methods.
  493. *
  494. * @return void
  495. */
  496. public function startupProcess() {
  497. $this->getEventManager()->dispatch(new Event('Controller.initialize', $this));
  498. $this->getEventManager()->dispatch(new Event('Controller.startup', $this));
  499. }
  500. /**
  501. * Perform the various shutdown processes for this controller.
  502. * Fire the Components and Controller callbacks in the correct order.
  503. *
  504. * - triggers the component `shutdown` callback.
  505. * - calls the Controller's `afterFilter` method.
  506. *
  507. * @return void
  508. */
  509. public function shutdownProcess() {
  510. $this->getEventManager()->dispatch(new Event('Controller.shutdown', $this));
  511. }
  512. /**
  513. * Redirects to given $url, after turning off $this->autoRender.
  514. * Script execution is halted after the redirect.
  515. *
  516. * @param string|array $url A string or array-based URL pointing to another location within the app,
  517. * or an absolute URL
  518. * @param integer $status Optional HTTP status code (eg: 404)
  519. * @param boolean $exit If true, exit() will be called after the redirect
  520. * @return void
  521. * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::redirect
  522. */
  523. public function redirect($url, $status = null, $exit = true) {
  524. $this->autoRender = false;
  525. $response = $this->response;
  526. if ($status && $response->statusCode() === 200) {
  527. $response->statusCode($status);
  528. }
  529. $event = new Event('Controller.beforeRedirect', $this, [$response, $url, $status]);
  530. $this->getEventManager()->dispatch($event);
  531. if ($event->isStopped()) {
  532. return;
  533. }
  534. if ($url !== null && !$response->location()) {
  535. $response->location(Router::url($url, true));
  536. }
  537. if ($exit) {
  538. $response->send();
  539. $this->_stop();
  540. }
  541. }
  542. /**
  543. * Internally redirects one action to another. Does not perform another HTTP request unlike Controller::redirect()
  544. *
  545. * Examples:
  546. *
  547. * {{{
  548. * setAction('another_action');
  549. * setAction('action_with_parameters', $parameter1);
  550. * }}}
  551. *
  552. * @param string $action The new action to be 'redirected' to.
  553. * Any other parameters passed to this method will be passed as parameters to the new action.
  554. * @return mixed Returns the return value of the called action
  555. */
  556. public function setAction($action) {
  557. $this->request->params['action'] = $action;
  558. $this->view = $action;
  559. $args = func_get_args();
  560. unset($args[0]);
  561. return call_user_func_array(array(&$this, $action), $args);
  562. }
  563. /**
  564. * Instantiates the correct view class, hands it its data, and uses it to render the view output.
  565. *
  566. * @param string $view View to use for rendering
  567. * @param string $layout Layout to use
  568. * @return Cake\Network\Response A response object containing the rendered view.
  569. * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::render
  570. */
  571. public function render($view = null, $layout = null) {
  572. $event = new Event('Controller.beforeRender', $this);
  573. $this->getEventManager()->dispatch($event);
  574. if ($event->isStopped()) {
  575. $this->autoRender = false;
  576. return $this->response;
  577. }
  578. $this->View = $this->_getViewObject();
  579. $this->autoRender = false;
  580. $this->response->body($this->View->render($view, $layout));
  581. return $this->response;
  582. }
  583. /**
  584. * Returns the referring URL for this request.
  585. *
  586. * @param string $default Default URL to use if HTTP_REFERER cannot be read from headers
  587. * @param boolean $local If true, restrict referring URLs to local server
  588. * @return string Referring URL
  589. * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::referer
  590. */
  591. public function referer($default = null, $local = false) {
  592. if (!$this->request) {
  593. return '/';
  594. }
  595. $referer = $this->request->referer($local);
  596. if ($referer === '/' && $default) {
  597. return Router::url($default, true);
  598. }
  599. return $referer;
  600. }
  601. /**
  602. * Handles pagination of records in Table objects.
  603. *
  604. * Will load the referenced Table object, and have the PaginatorComponent
  605. * paginate the query using the request date and settings defined in `$this->paginate`.
  606. *
  607. * This method will also make the PaginatorHelper available in the view.
  608. *
  609. * @param Table|string $object Table to paginate (e.g: Table instance, or 'Model')
  610. * @return ORM\ResultSet Query results
  611. * @link http://book.cakephp.org/3.0/en/controllers.html#Controller::paginate
  612. */
  613. public function paginate($object = null) {
  614. if (is_object($object)) {
  615. $table = $object;
  616. }
  617. if (is_string($object) || $object === null) {
  618. $try = [$object, $this->modelClass];
  619. foreach ($try as $tableName) {
  620. if (empty($tableName)) {
  621. continue;
  622. }
  623. $table = TableRegistry::get($tableName);
  624. break;
  625. }
  626. }
  627. $this->Paginator = $this->Components->load('Paginator');
  628. if (
  629. !in_array('Paginator', $this->helpers) &&
  630. !array_key_exists('Paginator', $this->helpers)
  631. ) {
  632. $this->helpers[] = 'Paginator';
  633. }
  634. return $this->Paginator->paginate($table, $this->paginate);
  635. }
  636. /**
  637. * Called before the controller action. You can use this method to configure and customize components
  638. * or perform logic that needs to happen before each controller action.
  639. *
  640. * @param Event $event An Event instance
  641. * @return void
  642. * @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
  643. */
  644. public function beforeFilter(Event $event) {
  645. }
  646. /**
  647. * Called after the controller action is run, but before the view is rendered. You can use this method
  648. * to perform logic or set view variables that are required on every request.
  649. *
  650. * @param Event $event An Event instance
  651. * @return void
  652. * @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
  653. */
  654. public function beforeRender(Event $event) {
  655. }
  656. /**
  657. * The beforeRedirect method is invoked when the controller's redirect method is called but before any
  658. * further action.
  659. *
  660. * If this method returns false the controller will not continue on to redirect the request.
  661. * The $url, $status and $exit variables have same meaning as for the controller's method. You can also
  662. * return a string which will be interpreted as the URL to redirect to or return associative array with
  663. * key 'url' and optionally 'status' and 'exit'.
  664. *
  665. * @param Event $event An Event instance
  666. * @param string|array $url A string or array-based URL pointing to another location within the app,
  667. * or an absolute URL
  668. * @param integer $status Optional HTTP status code (eg: 404)
  669. * @param boolean $exit If true, exit() will be called after the redirect
  670. * @return mixed
  671. * false to stop redirection event,
  672. * string controllers a new redirection URL or
  673. * array with the keys url, status and exit to be used by the redirect method.
  674. * @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
  675. */
  676. public function beforeRedirect(Event $event, $url, $status = null, $exit = true) {
  677. }
  678. /**
  679. * Called after the controller action is run and rendered.
  680. *
  681. * @param Event $event An Event instance
  682. * @return void
  683. * @link http://book.cakephp.org/2.0/en/controllers.html#request-life-cycle-callbacks
  684. */
  685. public function afterFilter(Event $event) {
  686. }
  687. /**
  688. * Constructs the view class instance based on the controller property
  689. *
  690. * @return View
  691. */
  692. protected function _getViewObject() {
  693. $viewClass = $this->viewClass;
  694. if ($this->viewClass !== 'View') {
  695. list($plugin, $viewClass) = pluginSplit($viewClass, true);
  696. $viewClass = App::classname($viewClass, 'View', 'View');
  697. }
  698. return new $viewClass($this);
  699. }
  700. }