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 1.2 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 1.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

Classes

  • AclBase
  • AclBehavior
  • AclComponent
  • AclNode
  • AclShell
  • Aco
  • AcoAction
  • AjaxHelper
  • ApcEngine
  • ApiShell
  • App
  • AppController
  • AppHelper
  • AppModel
  • Aro
  • AuthComponent
  • BakeShell
  • BehaviorCollection
  • Cache
  • CacheEngine
  • CacheHelper
  • CakeErrorController
  • CakeLog
  • CakeSchema
  • CakeSession
  • CakeSocket
  • ClassRegistry
  • Component
  • Configure
  • ConnectionManager
  • ConsoleShell
  • ContainableBehavior
  • Controller
  • ControllerTask
  • CookieComponent
  • DataSource
  • DbAcl
  • DbAclSchema
  • DbConfigTask
  • DboAdodb
  • DboDb2
  • DboFirebird
  • DboMssql
  • DboMysql
  • DboMysqlBase
  • DboMysqli
  • DboOdbc
  • DboOracle
  • DboPostgres
  • DboSource
  • DboSqlite
  • DboSybase
  • Debugger
  • EmailComponent
  • ErrorHandler
  • ExtractTask
  • File
  • FileEngine
  • Flay
  • Folder
  • FormHelper
  • Helper
  • HtmlHelper
  • HttpSocket
  • I18n
  • I18nModel
  • i18nSchema
  • I18nShell
  • Inflector
  • IniAcl
  • JavascriptHelper
  • JsHelper
  • JsHelperObject
  • L10n
  • MagicDb
  • MagicFileResource
  • MediaView
  • MemcacheEngine
  • Model
  • ModelBehavior
  • ModelTask
  • Multibyte
  • NumberHelper
  • Object
  • Overloadable
  • Overloadable2
  • PagesController
  • PaginatorHelper
  • Permission
  • PluginTask
  • ProjectTask
  • RequestHandlerComponent
  • Router
  • RssHelper
  • Sanitize
  • Scaffold
  • ScaffoldView
  • SchemaShell
  • Security
  • SecurityComponent
  • SessionComponent
  • SessionHelper
  • SessionsSchema
  • Set
  • Shell
  • String
  • TestSuiteShell
  • TestTask
  • TextHelper
  • ThemeView
  • TimeHelper
  • TranslateBehavior
  • TreeBehavior
  • Validation
  • View
  • ViewTask
  • XcacheEngine
  • Xml
  • XmlElement
  • XmlHelper
  • XmlManager
  • XmlNode
  • XmlTextNode

Functions

  • __enclose
  • make_clean_css
  • mb_encode_mimeheader
  • mb_stripos
  • mb_stristr
  • mb_strlen
  • mb_strpos
  • mb_strrchr
  • mb_strrichr
  • mb_strripos
  • mb_strrpos
  • mb_strstr
  • mb_strtolower
  • mb_strtoupper
  • mb_substr
  • mb_substr_count
  • write_css_cache
   1: <?php
   2: /* SVN FILE: $Id$ */
   3: /**
   4:  * Base controller class.
   5:  *
   6:  * PHP versions 4 and 5
   7:  *
   8:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
   9:  * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10:  *
  11:  * Licensed under The MIT License
  12:  * Redistributions of files must retain the above copyright notice.
  13:  *
  14:  * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15:  * @link          http://cakephp.org CakePHP(tm) Project
  16:  * @package       cake
  17:  * @subpackage    cake.cake.libs.controller
  18:  * @since         CakePHP(tm) v 0.2.9
  19:  * @version       $Revision$
  20:  * @modifiedby    $LastChangedBy$
  21:  * @lastmodified  $Date$
  22:  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
  23:  */
  24: /**
  25:  * Include files
  26:  */
  27: App::import('Core', array('Component', 'View'));
  28: /**
  29:  * Controller
  30:  *
  31:  * Application controller class for organization of business logic.
  32:  * Provides basic functionality, such as rendering views inside layouts,
  33:  * automatic model availability, redirection, callbacks, and more.
  34:  *
  35:  * @package       cake
  36:  * @subpackage    cake.cake.libs.controller
  37:  * @link          http://book.cakephp.org/view/49/Controllers
  38:  *
  39:  */
  40: class Controller extends Object {
  41: /**
  42:  * The name of this controller. Controller names are plural, named after the model they manipulate.
  43:  *
  44:  * @var string
  45:  * @access public
  46:  * @link http://book.cakephp.org/view/52/name
  47:  */
  48:     var $name = null;
  49: /**
  50:  * Stores the current URL, relative to the webroot of the application.
  51:  *
  52:  * @var string
  53:  * @access public
  54:  */
  55:     var $here = null;
  56: /**
  57:  * The webroot of the application. Helpful if your application is placed in a folder under the current domain name.
  58:  *
  59:  * @var string
  60:  * @access public
  61:  */
  62:     var $webroot = null;
  63: /**
  64:  * The name of the currently requested controller action.
  65:  *
  66:  * @var string
  67:  * @access public
  68:  */
  69:     var $action = null;
  70: /**
  71:  * An array containing the class names of models this controller uses.
  72:  *
  73:  * Example: var $uses = array('Product', 'Post', 'Comment');
  74:  *
  75:  * Can be set to array() to use no models.  Can be set to false to 
  76:  * use no models and prevent the merging of $uses with AppController
  77:  *
  78:  * @var mixed A single name as a string or a list of names as an array.
  79:  * @access protected
  80:  * @link http://book.cakephp.org/view/53/components-helpers-and-uses
  81:  */
  82:     var $uses = false;
  83: /**
  84:  * An array containing the names of helpers this controller uses. The array elements should
  85:  * not contain the "Helper" part of the classname.
  86:  *
  87:  * Example: var $helpers = array('Html', 'Javascript', 'Time', 'Ajax');
  88:  *
  89:  * @var mixed A single name as a string or a list of names as an array.
  90:  * @access protected
  91:  * @link http://book.cakephp.org/view/53/components-helpers-and-uses
  92:  */
  93:     var $helpers = array('Html', 'Form');
  94: /**
  95:  * Parameters received in the current request: GET and POST data, information
  96:  * about the request, etc.
  97:  *
  98:  * @var array
  99:  * @access public
 100:  * @link http://book.cakephp.org/view/55/The-Parameters-Attribute-params
 101:  */
 102:     var $params = array();
 103: /**
 104:  * Data POSTed to the controller using the HtmlHelper. Data here is accessible
 105:  * using the $this->data['ModelName']['fieldName'] pattern.
 106:  *
 107:  * @var array
 108:  * @access public
 109:  */
 110:     var $data = array();
 111: /**
 112:  * Holds pagination defaults for controller actions. The keys that can be included
 113:  * in this array are: 'conditions', 'fields', 'order', 'limit', 'page', and 'recursive',
 114:  * similar to the keys in the second parameter of Model::find().
 115:  *
 116:  * Pagination defaults can also be supplied in a model-by-model basis by using
 117:  * the name of the model as a key for a pagination array:
 118:  *
 119:  * var $paginate = array(
 120:  *      'Post' => array(...),
 121:  *      'Comment' => array(...)
 122:  *  );
 123:  *
 124:  * @var array
 125:  * @access public
 126:  * @link http://book.cakephp.org/view/164/Pagination
 127:  */
 128:     var $paginate = array('limit' => 20, 'page' => 1);
 129: /**
 130:  * The name of the views subfolder containing views for this controller.
 131:  *
 132:  * @var string
 133:  * @access public
 134:  */
 135:     var $viewPath = null;
 136: /**
 137:  * The name of the layouts subfolder containing layouts for this controller.
 138:  *
 139:  * @var string
 140:  * @access public
 141:  */
 142:     var $layoutPath = null;
 143: /**
 144:  * Contains variables to be handed to the view.
 145:  *
 146:  * @var array
 147:  * @access public
 148:  */
 149:     var $viewVars = array();
 150: /**
 151:  * Text to be used for the $title_for_layout layout variable (usually
 152:  * placed inside <title> tags.)
 153:  *
 154:  * @var boolean
 155:  * @access public
 156:  * @link http://book.cakephp.org/view/54/Page-related-Attributes-layout-and-pageTitle
 157:  */
 158:     var $pageTitle = false;
 159: /**
 160:  * An array containing the class names of the models this controller uses.
 161:  *
 162:  * @var array Array of model objects.
 163:  * @access public
 164:  */
 165:     var $modelNames = array();
 166: /**
 167:  * Base URL path.
 168:  *
 169:  * @var string
 170:  * @access public
 171:  */
 172:     var $base = null;
 173: /**
 174:  * The name of the layout file to render the view inside of. The name specified
 175:  * is the filename of the layout in /app/views/layouts without the .ctp
 176:  * extension.
 177:  *
 178:  * @var string
 179:  * @access public
 180:  * @link http://book.cakephp.org/view/54/Page-related-Attributes-layout-and-pageTitle
 181:  */
 182:     var $layout = 'default';
 183: /**
 184:  * Set to true to automatically render the view
 185:  * after action logic.
 186:  *
 187:  * @var boolean
 188:  * @access public
 189:  */
 190:     var $autoRender = true;
 191: /**
 192:  * Set to true to automatically render the layout around views.
 193:  *
 194:  * @var boolean
 195:  * @access public
 196:  */
 197:     var $autoLayout = true;
 198: /**
 199:  * Instance of Component used to handle callbacks.
 200:  *
 201:  * @var string
 202:  * @access public
 203:  */
 204:     var $Component = null;
 205: /**
 206:  * Array containing the names of components this controller uses. Component names
 207:  * should not contain the "Component" portion of the classname.
 208:  *
 209:  * Example: var $components = array('Session', 'RequestHandler', 'Acl');
 210:  *
 211:  * @var array
 212:  * @access public
 213:  * @link http://book.cakephp.org/view/53/components-helpers-and-uses
 214:  */
 215:     var $components = array();
 216: /**
 217:  * The name of the View class this controller sends output to.
 218:  *
 219:  * @var string
 220:  * @access public
 221:  */
 222:     var $view = 'View';
 223: /**
 224:  * File extension for view templates. Defaults to Cake's conventional ".ctp".
 225:  *
 226:  * @var string
 227:  * @access public
 228:  */
 229:     var $ext = '.ctp';
 230: /**
 231:  * The output of the requested action.  Contains either a variable
 232:  * returned from the action, or the data of the rendered view;
 233:  * You can use this var in child controllers' afterFilter() callbacks to alter output.
 234:  *
 235:  * @var string
 236:  * @access public
 237:  */
 238:     var $output = null;
 239: /**
 240:  * Automatically set to the name of a plugin.
 241:  *
 242:  * @var string
 243:  * @access public
 244:  */
 245:     var $plugin = null;
 246: /**
 247:  * Used to define methods a controller that will be cached. To cache a
 248:  * single action, the value is set to an array containing keys that match
 249:  * action names and values that denote cache expiration times (in seconds).
 250:  *
 251:  * Example: var $cacheAction = array(
 252:  *      'view/23/' => 21600,
 253:  *      'recalled/' => 86400
 254:  *  );
 255:  *
 256:  * $cacheAction can also be set to a strtotime() compatible string. This
 257:  * marks all the actions in the controller for view caching.
 258:  *
 259:  * @var mixed
 260:  * @access public
 261:  * @link http://book.cakephp.org/view/346/Caching-in-the-Controller
 262:  */
 263:     var $cacheAction = false;
 264: /**
 265:  * Used to create cached instances of models a controller uses.
 266:  * When set to true, all models related to the controller will be cached.
 267:  * This can increase performance in many cases.
 268:  *
 269:  * @var boolean
 270:  * @access public
 271:  */
 272:     var $persistModel = false;
 273: /**
 274:  * Holds all params passed and named.
 275:  *
 276:  * @var mixed
 277:  * @access public
 278:  */
 279:     var $passedArgs = array();
 280: /**
 281:  * Triggers Scaffolding
 282:  *
 283:  * @var mixed
 284:  * @access public
 285:  * @link http://book.cakephp.org/view/105/Scaffolding
 286:  */
 287:     var $scaffold = false;
 288: /**
 289:  * Holds current methods of the controller
 290:  *
 291:  * @var array
 292:  * @access public
 293:  * @link
 294:  */
 295:     var $methods = array();
 296: /**
 297:  * This controller's primary model class name, the Inflector::classify()'ed version of 
 298:  * the controller's $name property.
 299:  *
 300:  * Example: For a controller named 'Comments', the modelClass would be 'Comment'
 301:  *
 302:  * @var string
 303:  * @access public
 304:  */
 305:     var $modelClass = null;
 306: /**
 307:  * This controller's model key name, an underscored version of the controller's $modelClass property.
 308:  *
 309:  * Example: For a controller named 'ArticleComments', the modelKey would be 'article_comment'
 310:  *
 311:  * @var string
 312:  * @access public
 313:  */
 314:     var $modelKey = null;
 315: /**
 316:  * Holds any validation errors produced by the last call of the validateErrors() method/
 317:  *
 318:  * @var array Validation errors, or false if none
 319:  * @access public
 320:  */
 321:     var $validationErrors = null;
 322: /**
 323:  * Constructor.
 324:  *
 325:  */
 326:     function __construct() {
 327:         if ($this->name === null) {
 328:             $r = null;
 329:             if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
 330:                 die (__("Controller::__construct() : Can not get or parse my own class name, exiting."));
 331:             }
 332:             $this->name = $r[1];
 333:         }
 334: 
 335:         if ($this->viewPath == null) {
 336:             $this->viewPath = Inflector::underscore($this->name);
 337:         }
 338:         $this->modelClass = Inflector::classify($this->name);
 339:         $this->modelKey = Inflector::underscore($this->modelClass);
 340:         $this->Component =& new Component();
 341: 
 342:         $childMethods = get_class_methods($this);
 343:         $parentMethods = get_class_methods('Controller');
 344: 
 345:         foreach ($childMethods as $key => $value) {
 346:             $childMethods[$key] = strtolower($value);
 347:         }
 348: 
 349:         foreach ($parentMethods as $key => $value) {
 350:             $parentMethods[$key] = strtolower($value);
 351:         }
 352:         $this->methods = array_diff($childMethods, $parentMethods);
 353:         parent::__construct();
 354:     }
 355: /**
 356:  * Merge components, helpers, and uses vars from AppController and PluginAppController.
 357:  *
 358:  * @return void
 359:  * @access protected
 360:  */
 361:     function __mergeVars() {
 362:         $pluginName = Inflector::camelize($this->plugin);
 363:         $pluginController = $pluginName . 'AppController';
 364: 
 365:         if (is_subclass_of($this, 'AppController') || is_subclass_of($this, $pluginController)) {
 366:             $appVars = get_class_vars('AppController');
 367:             $uses = $appVars['uses'];
 368:             $merge = array('components', 'helpers');
 369:             $plugin = null;
 370: 
 371:             if (!empty($this->plugin)) {
 372:                 $plugin = $pluginName . '.';
 373:                 if (!is_subclass_of($this, $pluginController)) {
 374:                     $pluginController = null;
 375:                 }
 376:             } else {
 377:                 $pluginController = null;
 378:             }
 379: 
 380:             if ($uses == $this->uses && !empty($this->uses)) {
 381:                 if (!in_array($plugin . $this->modelClass, $this->uses)) {
 382:                     array_unshift($this->uses, $plugin . $this->modelClass);
 383:                 } elseif ($this->uses[0] !== $plugin . $this->modelClass) {
 384:                     $this->uses = array_flip($this->uses);
 385:                     unset($this->uses[$plugin . $this->modelClass]);
 386:                     $this->uses = array_flip($this->uses);
 387:                     array_unshift($this->uses, $plugin . $this->modelClass);
 388:                 }
 389:             } elseif ($this->uses !== null || $this->uses !== false) {
 390:                 $merge[] = 'uses';
 391:             }
 392: 
 393:             foreach ($merge as $var) {
 394:                 if (!empty($appVars[$var]) && is_array($this->{$var})) {
 395:                     if ($var === 'components') {
 396:                         $normal = Set::normalize($this->{$var});
 397:                         $app = Set::normalize($appVars[$var]);
 398:                         if ($app !== $normal) {
 399:                             $this->{$var} = Set::merge($app, $normal);
 400:                         }
 401:                     } else {
 402:                         $this->{$var} = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
 403:                     }
 404:                 }
 405:             }
 406:         }
 407: 
 408:         if ($pluginController && $pluginName != null) {
 409:             $appVars = get_class_vars($pluginController);
 410:             $uses = $appVars['uses'];
 411:             $merge = array('components', 'helpers');
 412: 
 413:             if ($this->uses !== null || $this->uses !== false) {
 414:                 $merge[] = 'uses';
 415:             }
 416: 
 417:             foreach ($merge as $var) {
 418:                 if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
 419:                     if ($var === 'components') {
 420:                         $normal = Set::normalize($this->{$var});
 421:                         $app = Set::normalize($appVars[$var]);
 422:                         if ($app !== $normal) {
 423:                             $this->{$var} = Set::merge($app, $normal);
 424:                         }
 425:                     } else {
 426:                         $this->{$var} = array_merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
 427:                     }
 428:                 }
 429:             }
 430:         }
 431:     }
 432: /**
 433:  * Loads Model classes based on the the uses property
 434:  * see Controller::loadModel(); for more info.
 435:  * Loads Components and prepares them for initialization.
 436:  *
 437:  * @return mixed true if models found and instance created, or cakeError if models not found.
 438:  * @access public
 439:  * @see Controller::loadModel()
 440:  * @link http://book.cakephp.org/view/429/constructClasses
 441:  */
 442:     function constructClasses() {
 443:         $this->__mergeVars();
 444:         $this->Component->init($this);
 445: 
 446:         if ($this->uses !== null || ($this->uses !== array())) {
 447:             if (empty($this->passedArgs) || !isset($this->passedArgs['0'])) {
 448:                 $id = false;
 449:             } else {
 450:                 $id = $this->passedArgs['0'];
 451:             }
 452: 
 453:             if ($this->uses === false) {
 454:                 $this->loadModel($this->modelClass, $id);
 455:             } elseif ($this->uses) {
 456:                 $uses = is_array($this->uses) ? $this->uses : array($this->uses);
 457:                 $modelClassName = $uses[0];
 458:                 if (strpos($uses[0], '.') !== false) {
 459:                     list($plugin, $modelClassName) = explode('.', $uses[0]);
 460:                 }
 461:                 $this->modelClass = $modelClassName;
 462:                 foreach ($uses as $modelClass) {
 463:                     $this->loadModel($modelClass);
 464:                 }
 465:             }
 466:         }
 467:         return true;
 468:     }
 469: /**
 470:  * Loads and instantiates models required by this controller.
 471:  * If Controller::persistModel; is true, controller will create cached model instances on first request,
 472:  * additional request will used cached models.
 473:  * If the model is non existent, it will throw a missing database table error, as Cake generates
 474:  * dynamic models for the time being.
 475:  *
 476:  * @param string $modelClass Name of model class to load
 477:  * @param mixed $id Initial ID the instanced model class should have
 478:  * @return mixed true when single model found and instance created error returned if models not found.
 479:  * @access public
 480:  */
 481:     function loadModel($modelClass = null, $id = null) {
 482:         if ($modelClass === null) {
 483:             $modelClass = $this->modelClass;
 484:         }
 485:         $cached = false;
 486:         $object = null;
 487:         $plugin = null;
 488:         if ($this->uses === false) {
 489:             if ($this->plugin) {
 490:                 $plugin = $this->plugin . '.';
 491:             }
 492:         }
 493: 
 494:         if (strpos($modelClass, '.') !== false) {
 495:             list($plugin, $modelClass) = explode('.', $modelClass);
 496:             $plugin = $plugin . '.';
 497:         }
 498: 
 499:         if ($this->persistModel === true) {
 500:             $cached = $this->_persist($modelClass, null, $object);
 501:         }
 502: 
 503:         if (($cached === false)) {
 504:             $this->modelNames[] = $modelClass;
 505: 
 506:             if (!PHP5) {
 507:                 $this->{$modelClass} =& ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id));
 508:             } else {
 509:                 $this->{$modelClass} = ClassRegistry::init(array('class' => $plugin . $modelClass, 'alias' => $modelClass, 'id' => $id));
 510:             }
 511: 
 512:             if (!$this->{$modelClass}) {
 513:                 return $this->cakeError('missingModel', array(array('className' => $modelClass, 'webroot' => '', 'base' => $this->base)));
 514:             }
 515: 
 516:             if ($this->persistModel === true) {
 517:                 $this->_persist($modelClass, true, $this->{$modelClass});
 518:                 $registry = ClassRegistry::getInstance();
 519:                 $this->_persist($modelClass . 'registry', true, $registry->__objects, 'registry');
 520:             }
 521:         } else {
 522:             $this->_persist($modelClass . 'registry', true, $object, 'registry');
 523:             $this->_persist($modelClass, true, $object);
 524:             $this->modelNames[] = $modelClass;
 525:         }
 526:     }
 527: /**
 528:  * Redirects to given $url, after turning off $this->autoRender.
 529:  * Script execution is halted after the redirect.
 530:  *
 531:  * @param mixed $url A string or array-based URL pointing to another location within the app, or an absolute URL
 532:  * @param integer $status Optional HTTP status code (eg: 404)
 533:  * @param boolean $exit If true, exit() will be called after the redirect
 534:  * @return mixed void if $exit = false. Terminates script if $exit = true
 535:  * @access public
 536:  * @link http://book.cakephp.org/view/425/redirect
 537:  */
 538:     function redirect($url, $status = null, $exit = true) {
 539:         $this->autoRender = false;
 540: 
 541:         if (is_array($status)) {
 542:             extract($status, EXTR_OVERWRITE);
 543:         }
 544:         $response = $this->Component->beforeRedirect($this, $url, $status, $exit);
 545: 
 546:         if ($response === false) {
 547:             return;
 548:         }
 549:         if (is_array($response)) {
 550:             foreach ($response as $resp) {
 551:                 if (is_array($resp) && isset($resp['url'])) {
 552:                     extract($resp, EXTR_OVERWRITE);
 553:                 } elseif ($resp !== null) {
 554:                     $url = $resp;
 555:                 }
 556:             }
 557:         }
 558: 
 559:         if (function_exists('session_write_close')) {
 560:             session_write_close();
 561:         }
 562: 
 563:         if (!empty($status)) {
 564:             $codes = array(
 565:                 100 => 'Continue',
 566:                 101 => 'Switching Protocols',
 567:                 200 => 'OK',
 568:                 201 => 'Created',
 569:                 202 => 'Accepted',
 570:                 203 => 'Non-Authoritative Information',
 571:                 204 => 'No Content',
 572:                 205 => 'Reset Content',
 573:                 206 => 'Partial Content',
 574:                 300 => 'Multiple Choices',
 575:                 301 => 'Moved Permanently',
 576:                 302 => 'Found',
 577:                 303 => 'See Other',
 578:                 304 => 'Not Modified',
 579:                 305 => 'Use Proxy',
 580:                 307 => 'Temporary Redirect',
 581:                 400 => 'Bad Request',
 582:                 401 => 'Unauthorized',
 583:                 402 => 'Payment Required',
 584:                 403 => 'Forbidden',
 585:                 404 => 'Not Found',
 586:                 405 => 'Method Not Allowed',
 587:                 406 => 'Not Acceptable',
 588:                 407 => 'Proxy Authentication Required',
 589:                 408 => 'Request Time-out',
 590:                 409 => 'Conflict',
 591:                 410 => 'Gone',
 592:                 411 => 'Length Required',
 593:                 412 => 'Precondition Failed',
 594:                 413 => 'Request Entity Too Large',
 595:                 414 => 'Request-URI Too Large',
 596:                 415 => 'Unsupported Media Type',
 597:                 416 => 'Requested range not satisfiable',
 598:                 417 => 'Expectation Failed',
 599:                 500 => 'Internal Server Error',
 600:                 501 => 'Not Implemented',
 601:                 502 => 'Bad Gateway',
 602:                 503 => 'Service Unavailable',
 603:                 504 => 'Gateway Time-out'
 604:             );
 605:             if (is_string($status)) {
 606:                 $codes = array_flip($codes);
 607:             }
 608: 
 609:             if (isset($codes[$status])) {
 610:                 $code = $msg = $codes[$status];
 611:                 if (is_numeric($status)) {
 612:                     $code = $status;
 613:                 }
 614:                 if (is_string($status)) {
 615:                     $msg = $status;
 616:                 }
 617:                 $status = "HTTP/1.1 {$code} {$msg}";
 618:             } else {
 619:                 $status = null;
 620:             }
 621:         }
 622: 
 623:         if (!empty($status)) {
 624:             $this->header($status);
 625:         }
 626:         if ($url !== null) {
 627:             $this->header('Location: ' . Router::url($url, true));
 628:         }
 629: 
 630:         if (!empty($status) && ($status >= 300 && $status < 400)) {
 631:             $this->header($status);
 632:         }
 633: 
 634:         if ($exit) {
 635:             $this->_stop();
 636:         }
 637:     }
 638: /**
 639:  * Convenience method for header()
 640:  *
 641:  * @param string $status
 642:  * @return void
 643:  * @access public
 644:  */
 645:     function header($status) {
 646:         header($status);
 647:     }
 648: /**
 649:  * Saves a variable for use inside a view template.
 650:  *
 651:  * @param mixed $one A string or an array of data.
 652:  * @param mixed $two Value in case $one is a string (which then works as the key).
 653:  *   Unused if $one is an associative array, otherwise serves as the values to $one's keys.
 654:  * @return void
 655:  * @access public
 656:  * @link http://book.cakephp.org/view/427/set
 657:  */
 658:     function set($one, $two = null) {
 659:         $data = array();
 660: 
 661:         if (is_array($one)) {
 662:             if (is_array($two)) {
 663:                 $data = array_combine($one, $two);
 664:             } else {
 665:                 $data = $one;
 666:             }
 667:         } else {
 668:             $data = array($one => $two);
 669:         }
 670: 
 671:         foreach ($data as $name => $value) {
 672:             if ($name === 'title') {
 673:                 $this->pageTitle = $value;
 674:             } else {
 675:                 if ($two === null && is_array($one)) {
 676:                     $this->viewVars[Inflector::variable($name)] = $value;
 677:                 } else {
 678:                     $this->viewVars[$name] = $value;
 679:                 }
 680:             }
 681:         }
 682:     }
 683: /**
 684:  * Internally redirects one action to another. Examples:
 685:  *
 686:  * setAction('another_action');
 687:  * setAction('action_with_parameters', $parameter1);
 688:  *
 689:  * @param string $action The new action to be redirected to
 690:  * @param mixed  Any other parameters passed to this method will be passed as
 691:  *               parameters to the new action.
 692:  * @return mixed Returns the return value of the called action
 693:  * @access public
 694:  */
 695:     function setAction($action) {
 696:         $this->action = $action;
 697:         $args = func_get_args();
 698:         unset($args[0]);
 699:         return call_user_func_array(array(&$this, $action), $args);
 700:     }
 701: /**
 702:  * Controller callback to tie into Auth component. Only called when AuthComponent::authorize is set to 'controller'.
 703:  *
 704:  * @return bool true if authorized, false otherwise
 705:  * @access public
 706:  * @link http://book.cakephp.org/view/396/authorize
 707:  */
 708:     function isAuthorized() {
 709:         trigger_error(sprintf(__('%s::isAuthorized() is not defined.', true), $this->name), E_USER_WARNING);
 710:         return false;
 711:     }
 712: /**
 713:  * Returns number of errors in a submitted FORM.
 714:  *
 715:  * @return integer Number of errors
 716:  * @access public
 717:  */
 718:     function validate() {
 719:         $args = func_get_args();
 720:         $errors = call_user_func_array(array(&$this, 'validateErrors'), $args);
 721: 
 722:         if ($errors === false) {
 723:             return 0;
 724:         }
 725:         return count($errors);
 726:     }
 727: /**
 728:  * Validates models passed by parameters. Example:
 729:  *
 730:  * $errors = $this->validateErrors($this->Article, $this->User);
 731:  *
 732:  * @param mixed A list of models as a variable argument
 733:  * @return array Validation errors, or false if none
 734:  * @access public
 735:  */
 736:     function validateErrors() {
 737:         $objects = func_get_args();
 738: 
 739:         if (empty($objects)) {
 740:             return false;
 741:         }
 742: 
 743:         $errors = array();
 744:         foreach ($objects as $object) {
 745:             if (isset($this->{$object->alias})) {
 746:                 $object =& $this->{$object->alias};
 747:             }
 748:             $object->set($object->data);
 749:             $errors = array_merge($errors, $object->invalidFields());
 750:         }
 751: 
 752:         return $this->validationErrors = (!empty($errors) ? $errors : false);
 753:     }
 754: /**
 755:  * Instantiates the correct view class, hands it its data, and uses it to render the view output.
 756:  *
 757:  * @param string $action Action name to render
 758:  * @param string $layout Layout to use
 759:  * @param string $file File to use for rendering
 760:  * @return string Full output string of view contents
 761:  * @access public
 762:  * @link http://book.cakephp.org/view/428/render
 763:  */
 764:     function render($action = null, $layout = null, $file = null) {
 765:         $this->beforeRender();
 766: 
 767:         $viewClass = $this->view;
 768:         if ($this->view != 'View') {
 769:             if (strpos($viewClass, '.') !== false) {
 770:                 list($plugin, $viewClass) = explode('.', $viewClass);
 771:             }
 772:             $viewClass = $viewClass . 'View';
 773:             App::import('View', $this->view);
 774:         }
 775: 
 776:         $this->Component->beforeRender($this);
 777: 
 778:         $this->params['models'] = $this->modelNames;
 779: 
 780:         if (Configure::read() > 2) {
 781:             $this->set('cakeDebug', $this);
 782:         }
 783: 
 784:         $View =& new $viewClass($this);
 785: 
 786:         if (!empty($this->modelNames)) {
 787:             $models = array();
 788:             foreach ($this->modelNames as $currentModel) {
 789:                 if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model')) {
 790:                     $models[] = Inflector::underscore($currentModel);
 791:                 }
 792:                 if (isset($this->$currentModel) && is_a($this->$currentModel, 'Model') && !empty($this->$currentModel->validationErrors)) {
 793:                     $View->validationErrors[Inflector::camelize($currentModel)] =& $this->$currentModel->validationErrors;
 794:                 }
 795:             }
 796:             $models = array_diff(ClassRegistry::keys(), $models);
 797:             foreach ($models as $currentModel) {
 798:                 if (ClassRegistry::isKeySet($currentModel)) {
 799:                     $currentObject =& ClassRegistry::getObject($currentModel);
 800:                     if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) {
 801:                         $View->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors;
 802:                     }
 803:                 }
 804:             }
 805:         }
 806: 
 807:         $this->autoRender = false;
 808:         $this->output .= $View->render($action, $layout, $file);
 809: 
 810:         return $this->output;
 811:     }
 812: /**
 813:  * Returns the referring URL for this request.
 814:  *
 815:  * @param string $default Default URL to use if HTTP_REFERER cannot be read from headers
 816:  * @param boolean $local If true, restrict referring URLs to local server
 817:  * @return string Referring URL
 818:  * @access public
 819:  * @link http://book.cakephp.org/view/430/referer
 820:  */
 821:     function referer($default = null, $local = false) {
 822:         $ref = env('HTTP_REFERER');
 823:         if (!empty($ref) && defined('FULL_BASE_URL')) {
 824:             $base = FULL_BASE_URL . $this->webroot;
 825:             if (strpos($ref, $base) === 0) {
 826:                 $return =  substr($ref, strlen($base));
 827:                 if ($return[0] != '/') {
 828:                     $return = '/'.$return;
 829:                 }
 830:                 return $return;
 831:             } elseif (!$local) {
 832:                 return $ref;
 833:             }
 834:         }
 835: 
 836:         if ($default != null) {
 837:             return $default;
 838:         }
 839:         return '/';
 840:     }
 841: /**
 842:  * Forces the user's browser not to cache the results of the current request.
 843:  *
 844:  * @return void
 845:  * @access public
 846:  * @link http://book.cakephp.org/view/431/disableCache
 847:  */
 848:     function disableCache() {
 849:         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
 850:         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 851:         header("Cache-Control: no-store, no-cache, must-revalidate");
 852:         header("Cache-Control: post-check=0, pre-check=0", false);
 853:         header("Pragma: no-cache");
 854:     }
 855: /**
 856:  * Shows a message to the user for $pause seconds, then redirects to $url.
 857:  * Uses flash.ctp as the default layout for the message.
 858:  * Does not work if the current debug level is higher than 0.
 859:  *
 860:  * @param string $message Message to display to the user
 861:  * @param mixed $url Relative string or array-based URL to redirect to after the time expires
 862:  * @param integer $pause Time to show the message
 863:  * @return void Renders flash layout
 864:  * @access public
 865:  * @link http://book.cakephp.org/view/426/flash
 866:  */
 867:     function flash($message, $url, $pause = 1) {
 868:         $this->autoRender = false;
 869:         $this->set('url', Router::url($url));
 870:         $this->set('message', $message);
 871:         $this->set('pause', $pause);
 872:         $this->set('page_title', $message);
 873:         $this->render(false, 'flash');
 874:     }
 875: /**
 876:  * Converts POST'ed form data to a model conditions array, suitable for use in a Model::find() call.
 877:  *
 878:  * @param array $data POST'ed data organized by model and field
 879:  * @param mixed $op A string containing an SQL comparison operator, or an array matching operators to fields
 880:  * @param string $bool SQL boolean operator: AND, OR, XOR, etc.
 881:  * @param boolean $exclusive If true, and $op is an array, fields not included in $op will not be included in the returned conditions
 882:  * @return array An array of model conditions
 883:  * @access public
 884:  * @link http://book.cakephp.org/view/432/postConditions
 885:  */
 886:     function postConditions($data = array(), $op = null, $bool = 'AND', $exclusive = false) {
 887:         if (!is_array($data) || empty($data)) {
 888:             if (!empty($this->data)) {
 889:                 $data = $this->data;
 890:             } else {
 891:                 return null;
 892:             }
 893:         }
 894:         $cond = array();
 895: 
 896:         if ($op === null) {
 897:             $op = '';
 898:         }
 899: 
 900:         $arrayOp = is_array($op);
 901:         foreach ($data as $model => $fields) {
 902:             foreach ($fields as $field => $value) {
 903:                 $key = $model.'.'.$field;
 904:                 $fieldOp = $op;
 905:                 if ($arrayOp) {
 906:                     if (array_key_exists($key, $op)) {
 907:                         $fieldOp = $op[$key];
 908:                     } elseif (array_key_exists($field, $op)) {
 909:                         $fieldOp = $op[$field];
 910:                     } else {
 911:                         $fieldOp = false;
 912:                     }
 913:                 }
 914:                 if ($exclusive && $fieldOp === false) {
 915:                     continue;
 916:                 }
 917:                 $fieldOp = strtoupper(trim($fieldOp));
 918:                 if ($fieldOp === 'LIKE') {
 919:                     $key = $key.' LIKE';
 920:                     $value = '%'.$value.'%';
 921:                 } elseif ($fieldOp && $fieldOp != '=') {
 922:                     $key = $key.' '.$fieldOp;
 923:                 }
 924:                 $cond[$key] = $value;
 925:             }
 926:         }
 927:         if ($bool != null && strtoupper($bool) != 'AND') {
 928:             $cond = array($bool => $cond);
 929:         }
 930:         return $cond;
 931:     }
 932: /**
 933:  * Handles automatic pagination of model records.
 934:  *
 935:  * @param mixed $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
 936:  * @param mixed $scope Conditions to use while paginating
 937:  * @param array $whitelist List of allowed options for paging
 938:  * @return array Model query results
 939:  * @access public
 940:  * @link http://book.cakephp.org/view/165/Controller-Setup
 941:  */
 942:     function paginate($object = null, $scope = array(), $whitelist = array()) {
 943:         if (is_array($object)) {
 944:             $whitelist = $scope;
 945:             $scope = $object;
 946:             $object = null;
 947:         }
 948:         $assoc = null;
 949: 
 950:         if (is_string($object)) {
 951:             $assoc = null;
 952: 
 953:             if (strpos($object, '.') !== false) {
 954:                 list($object, $assoc) = explode('.', $object);
 955:             }
 956: 
 957:             if ($assoc && isset($this->{$object}->{$assoc})) {
 958:                 $object =& $this->{$object}->{$assoc};
 959:             } elseif ($assoc && isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$assoc})) {
 960:                 $object =& $this->{$this->modelClass}->{$assoc};
 961:             } elseif (isset($this->{$object})) {
 962:                 $object =& $this->{$object};
 963:             } elseif (isset($this->{$this->modelClass}) && isset($this->{$this->modelClass}->{$object})) {
 964:                 $object =& $this->{$this->modelClass}->{$object};
 965:             }
 966:         } elseif (empty($object) || $object === null) {
 967:             if (isset($this->{$this->modelClass})) {
 968:                 $object =& $this->{$this->modelClass};
 969:             } else {
 970:                 $className = null;
 971:                 $name = $this->uses[0];
 972:                 if (strpos($this->uses[0], '.') !== false) {
 973:                     list($name, $className) = explode('.', $this->uses[0]);
 974:                 }
 975:                 if ($className) {
 976:                     $object =& $this->{$className};
 977:                 } else {
 978:                     $object =& $this->{$name};
 979:                 }
 980:             }
 981:         }
 982: 
 983:         if (!is_object($object)) {
 984:             trigger_error(sprintf(__('Controller::paginate() - can\'t find model %1$s in controller %2$sController', true), $object, $this->name), E_USER_WARNING);
 985:             return array();
 986:         }
 987:         $options = array_merge($this->params, $this->params['url'], $this->passedArgs);
 988: 
 989:         if (isset($this->paginate[$object->alias])) {
 990:             $defaults = $this->paginate[$object->alias];
 991:         } else {
 992:             $defaults = $this->paginate;
 993:         }
 994: 
 995:         if (isset($options['show'])) {
 996:             $options['limit'] = $options['show'];
 997:         }
 998: 
 999:         if (isset($options['sort'])) {
1000:             $direction = null;
1001:             if (isset($options['direction'])) {
1002:                 $direction = strtolower($options['direction']);
1003:             }
1004:             if ($direction != 'asc' && $direction != 'desc') {
1005:                 $direction = 'asc';
1006:             }
1007:             $options['order'] = array($options['sort'] => $direction);
1008:         }
1009: 
1010:         if (!empty($options['order']) && is_array($options['order'])) {
1011:             $alias = $object->alias ;
1012:             $key = $field = key($options['order']);
1013: 
1014:             if (strpos($key, '.') !== false) {
1015:                 list($alias, $field) = explode('.', $key);
1016:             }
1017:             $value = $options['order'][$key];
1018:             unset($options['order'][$key]);
1019: 
1020:             if (isset($object->{$alias}) && $object->{$alias}->hasField($field)) {
1021:                 $options['order'][$alias . '.' . $field] = $value;
1022:             } elseif ($object->hasField($field)) {
1023:                 $options['order'][$object->alias . '.' . $field] = $value;
1024:             }
1025:         }
1026:         $vars = array('fields', 'order', 'limit', 'page', 'recursive');
1027:         $keys = array_keys($options);
1028:         $count = count($keys);
1029: 
1030:         for ($i = 0; $i < $count; $i++) {
1031:             if (!in_array($keys[$i], $vars, true)) {
1032:                 unset($options[$keys[$i]]);
1033:             }
1034:             if (empty($whitelist) && ($keys[$i] === 'fields' || $keys[$i] === 'recursive')) {
1035:                 unset($options[$keys[$i]]);
1036:             } elseif (!empty($whitelist) && !in_array($keys[$i], $whitelist)) {
1037:                 unset($options[$keys[$i]]);
1038:             }
1039:         }
1040:         $conditions = $fields = $order = $limit = $page = $recursive = null;
1041: 
1042:         if (!isset($defaults['conditions'])) {
1043:             $defaults['conditions'] = array();
1044:         }
1045: 
1046:         $type = 'all';
1047: 
1048:         if (isset($defaults[0])) {
1049:             $type = $defaults[0];
1050:             unset($defaults[0]);
1051:         }
1052: 
1053:         $options = array_merge(array('page' => 1, 'limit' => 20), $defaults, $options);
1054:         $options['limit'] = (int) $options['limit'];
1055:         if (empty($options['limit']) || $options['limit'] < 1) {
1056:             $options['limit'] = 1;
1057:         }
1058: 
1059:         extract($options);
1060: 
1061:         if (is_array($scope) && !empty($scope)) {
1062:             $conditions = array_merge($conditions, $scope);
1063:         } elseif (is_string($scope)) {
1064:             $conditions = array($conditions, $scope);
1065:         }
1066:         if ($recursive === null) {
1067:             $recursive = $object->recursive;
1068:         }
1069: 
1070:         $extra = array_diff_key($defaults, compact(
1071:             'conditions', 'fields', 'order', 'limit', 'page', 'recursive'
1072:         ));
1073:         if ($type !== 'all') {
1074:             $extra['type'] = $type;
1075:         }
1076: 
1077:         if (method_exists($object, 'paginateCount')) {
1078:             $count = $object->paginateCount($conditions, $recursive, $extra);
1079:         } else {
1080:             $parameters = compact('conditions');
1081:             if ($recursive != $object->recursive) {
1082:                 $parameters['recursive'] = $recursive;
1083:             }
1084:             $count = $object->find('count', array_merge($parameters, $extra));
1085:         }
1086:         $pageCount = intval(ceil($count / $limit));
1087: 
1088:         if ($page === 'last' || $page >= $pageCount) {
1089:             $options['page'] = $page = $pageCount;
1090:         } elseif (intval($page) < 1) {
1091:             $options['page'] = $page = 1;
1092:         }
1093:         $page = $options['page'] = (integer)$page;
1094: 
1095:         if (method_exists($object, 'paginate')) {
1096:             $results = $object->paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra);
1097:         } else {
1098:             $parameters = compact('conditions', 'fields', 'order', 'limit', 'page');
1099:             if ($recursive != $object->recursive) {
1100:                 $parameters['recursive'] = $recursive;
1101:             }
1102:             $results = $object->find($type, array_merge($parameters, $extra));
1103:         }
1104:         $paging = array(
1105:             'page'      => $page,
1106:             'current'   => count($results),
1107:             'count'     => $count,
1108:             'prevPage'  => ($page > 1),
1109:             'nextPage'  => ($count > ($page * $limit)),
1110:             'pageCount' => $pageCount,
1111:             'defaults'  => array_merge(array('limit' => 20, 'step' => 1), $defaults),
1112:             'options'   => $options
1113:         );
1114:         $this->params['paging'][$object->alias] = $paging;
1115: 
1116:         if (!in_array('Paginator', $this->helpers) && !array_key_exists('Paginator', $this->helpers)) {
1117:             $this->helpers[] = 'Paginator';
1118:         }
1119:         return $results;
1120:     }
1121: /**
1122:  * Called before the controller action.
1123:  *
1124:  * @access public
1125:  * @link http://book.cakephp.org/view/60/Callbacks
1126:  */
1127:     function beforeFilter() {
1128:     }
1129: /**
1130:  * Called after the controller action is run, but before the view is rendered.
1131:  *
1132:  * @access public
1133:  * @link http://book.cakephp.org/view/60/Callbacks
1134:  */
1135:     function beforeRender() {
1136:     }
1137: /**
1138:  * Called after the controller action is run and rendered.
1139:  *
1140:  * @access public
1141:  * @link http://book.cakephp.org/view/60/Callbacks
1142:  */
1143:     function afterFilter() {
1144:     }
1145: /**
1146:  * This method should be overridden in child classes.
1147:  *
1148:  * @param string $method name of method called example index, edit, etc.
1149:  * @return boolean Success
1150:  * @access protected
1151:  * @link http://book.cakephp.org/view/60/Callbacks
1152:  */
1153:     function _beforeScaffold($method) {
1154:         return true;
1155:     }
1156: /**
1157:  * This method should be overridden in child classes.
1158:  *
1159:  * @param string $method name of method called either edit or update.
1160:  * @return boolean Success
1161:  * @access protected
1162:  * @link http://book.cakephp.org/view/60/Callbacks
1163:  */
1164:     function _afterScaffoldSave($method) {
1165:         return true;
1166:     }
1167: /**
1168:  * This method should be overridden in child classes.
1169:  *
1170:  * @param string $method name of method called either edit or update.
1171:  * @return boolean Success
1172:  * @access protected
1173:  * @link http://book.cakephp.org/view/60/Callbacks
1174:  */
1175:     function _afterScaffoldSaveError($method) {
1176:         return true;
1177:     }
1178: /**
1179:  * This method should be overridden in child classes.
1180:  * If not it will render a scaffold error.
1181:  * Method MUST return true in child classes
1182:  *
1183:  * @param string $method name of method called example index, edit, etc.
1184:  * @return boolean Success
1185:  * @access protected
1186:  * @link http://book.cakephp.org/view/60/Callbacks
1187:  */
1188:     function _scaffoldError($method) {
1189:         return false;
1190:     }
1191: }
1192: ?>
1193: 
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