Application controller class for organization of business logic. Provides basic functionality, such as rendering views inside layouts, automatic model availability, redirection, callbacks, and more.
Controllers should provide a number of 'action' methods. These are public methods on the controller that are not prefixed with a '_' and not part of Controller. Each action serves as an endpoint for performing a specific action on a resource or collection of resources. For example: adding or editing a new object, or listing a set of objects.
You can access request parameters, using $this->request
. The request object contains all the POST, GET and FILES that were part of the request.
After performing the required actions, controllers are responsible for creating a response. This usually takes the form of a generated View, or possibly a redirection to another controller action. In either case $this->response
allows you to manipulate all aspects of the response.
Controllers are created by Dispatcher based on request parameters and routing. By default controllers and actions use conventional names. For example /posts/index
maps to PostsController::index()
. You can re-map URLs using Router::connect().
__construct( CakeRequest $request null , CakeResponse $response null )
Constructor.
CakeRequest
$request
optional null CakeResponse
$response
optional null Object::__construct()
__get( string $name )
Provides backwards compatibility access to the request object properties. Also provides the params alias.
$name
mixed
__isset( string $name )
Provides backwards compatibility to avoid problems with empty and isset to alias properties. Lazy loads models using the loadModel() method if declared in $uses
$name
boolean
__set( string $name , mixed $value )
Provides backwards compatibility access for setting values to the request object.
$name
$value
_afterScaffoldSave( string $method )
Alias to afterScaffoldSave()
$method
boolean
Controller::afterScaffoldSave()
_afterScaffoldSaveError( string $method )
Alias to afterScaffoldSaveError()
$method
boolean
Controller::afterScaffoldSaveError()
_beforeScaffold( string $method )
Alias to beforeScaffold()
$method
boolean
Controller::beforeScaffold()
_getScaffold( CakeRequest $request )
Returns a scaffold object to use for dynamically scaffolded controllers.
CakeRequest
$request
Scaffold
_getViewObject( )
Constructs the view class instance based on the controller property
View
_isPrivateAction( ReflectionMethod $method , CakeRequest $request )
Check if the request's action is marked as private, with an underscore, or if the request is attempting to directly accessing a prefixed action.
$method
CakeRequest
$request
boolean
_mergeControllerVars( )
Merge components, helpers, and uses vars from Controller::$_mergeParent and PluginAppController.
_mergeUses( array $merge )
Helper method for merging the $uses property together.
Merges the elements not already in $this->uses into $this->uses.
$merge
_parseBeforeRedirect( mixed $response , string|array $url , integer $status , boolean $exit )
Parse beforeRedirect Response
$response
$url
$status
$exit
array
_scaffoldError( string $method )
Alias to scaffoldError()
$method
boolean
Controller::scaffoldError()
afterFilter( )
Called after the controller action is run and rendered.
afterScaffoldSave( string $method )
This method should be overridden in child classes.
$method
boolean
afterScaffoldSaveError( string $method )
This method should be overridden in child classes.
$method
boolean
beforeFilter( )
Called before the controller action. You can use this method to configure and customize components or perform logic that needs to happen before each controller action.
beforeRedirect( string|array $url , integer $status null , boolean $exit true )
The beforeRedirect method is invoked when the controller's redirect method is called but before any further action.
If this method returns false the controller will not continue on to redirect the request. The $url, $status and $exit variables have same meaning as for the controller's method. You can also return a string which will be interpreted as the URL to redirect to or return associative array with key 'url' and optionally 'status' and 'exit'.
$url
$status
optional null $exit
optional true mixed
beforeRender( )
Called after the controller action is run, but before the view is rendered. You can use this method to perform logic or set view variables that are required on every request.
beforeScaffold( string $method )
This method should be overridden in child classes.
$method
boolean
constructClasses( )
Loads Model classes based on the uses property see Controller::loadModel(); for more info. Loads Components and prepares them for initialization.
mixed
MissingModelException
Controller::loadModel()
disableCache( )
Forces the user's browser not to cache the results of the current request.
flash( string $message , string|array $url , integer $pause 1 , string $layout 'flash' )
Shows a message to the user for $pause seconds, then redirects to $url. Uses flash.ctp as the default layout for the message. Does not work if the current debug level is higher than 0.
$message
$url
$pause
optional 1 $layout
optional 'flash' getEventManager( )
Returns the CakeEventManager manager instance that is handling any callbacks. You can use this instance to register any new listeners or callbacks to the controller events, or create your own events and trigger them at will.
CakeEventManager
header( string $status )
Convenience and object wrapper method for CakeResponse::header().
$status
httpCodes( integer|array $code null )
Queries & sets valid HTTP response codes & messages.
$code
optional null array
implementedEvents( )
Returns a list of all events that will fire in the controller during its lifecycle. You can override this function to add your own listener callbacks
array
CakeEventListener::implementedEvents()
invokeAction( CakeRequest $request )
Dispatches the controller action. Checks that the action exists and isn't private.
CakeRequest
$request
mixed
PrivateActionException
MissingActionException
loadModel( string $modelClass null , integer|string $id null )
Loads and instantiates models required by this controller. If the model is non existent, it will throw a missing database table error, as CakePHP generates dynamic models for the time being.
$modelClass
optional null $id
optional null boolean
MissingModelException
paginate( Model|string $object null , string|array $scope array() , array $whitelist array() )
Handles automatic pagination of model records.
Model
|string $object
optional null $scope
optional array() $whitelist
optional array() array
postConditions( array $data array() , string|array $op null , string $bool 'AND' , boolean $exclusive false )
Converts POST'ed form data to a model conditions array, suitable for use in a Model::find() call.
$data
optional array() $op
optional null $bool
optional 'AND' $exclusive
optional false array|null
redirect( string|array $url , integer|array|null $status null , boolean $exit true )
Redirects to given $url, after turning off $this->autoRender. Script execution is halted after the redirect.
$url
$status
optional null $exit
optional true referer( string $default null , boolean $local false )
Returns the referring URL for this request.
$default
optional null $local
optional false string
render( string $view null , string $layout null )
Instantiates the correct view class, hands it its data, and uses it to render the view output.
$view
optional null $layout
optional null CakeResponse
scaffoldError( string $method )
This method should be overridden in child classes. If not it will render a scaffold error. Method MUST return true in child classes
$method
boolean
set( string|array $one , string|array $two null )
Saves a variable for use inside a view template.
$one
$two
optional null setAction( string $action )
Internally redirects one action to another. Does not perform another HTTP request unlike Controller::redirect()
Examples:
setAction('another_action'); setAction('action_with_parameters', $parameter1);
$action
mixed
setRequest( CakeRequest $request )
Sets the request objects and configures a number of controller properties based on the contents of the request. The properties that get set are
CakeRequest
$request
shutdownProcess( )
Perform the various shutdown processes for this controller. Fire the Components and Controller callbacks in the correct order.
shutdown
callback.afterFilter
method.startupProcess( )
Perform the startup process for this controller. Fire the Components and Controller callbacks in the correct order.
initialize
callbackbeforeFilter
.startup
methods.validate( )
Returns number of errors in a submitted FORM.
integer
validateErrors( )
Validates models passed by parameters. Takes a list of models as a variable argument. Example:
$errors = $this->validateErrors($this->Article, $this->User);
array
_mergeVars( array $properties , string $class , boolean $normalize true )
Merges this objects $property with the property in $class' definition. This classes value for the property will be merged on top of $class'
This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine this method as an empty function.
$properties
$class
$normalize
optional true _set( array $properties array() )
Allows setting of multiple properties of the object in a single line of code. Will only set properties that are part of a class declaration.
$properties
optional array() _stop( integer|string $status 0 )
Stop execution of the current script. Wraps exit() making testing easier.
$status
optional 0 dispatchMethod( string $method , array $params array() )
Calls a method on this object with the given parameters. Provides an OO wrapper for call_user_func_array
$method
$params
optional array() mixed
log( string $msg , integer $type LOG_ERR , null|string|array $scope null )
Convenience method to write a message to CakeLog. See CakeLog::write() for more information on writing to logs.
$msg
$type
optional LOG_ERR $scope
optional null boolean
requestAction( string|array $url , array $extra array() )
Calls a controller's method from any location. Can be used to connect controllers together or tie plugins into a main application. requestAction can be used to return rendered views or fetch the return value from controller actions.
Under the hood this method uses Router::reverse() to convert the $url parameter into a string URL. You should use URL formats that are compatible with Router::reverse()
POST and GET data can be simulated in requestAction. Use $extra['url']
for GET data. The $extra['data']
parameter allows POST data simulation.
$url
$extra
optional array() mixed
toString( )
Object-to-string conversion. Each class can override this method as necessary.
string
public AclComponent
public AuthComponent
public CookieComponent
public EmailComponent
public PaginatorComponent
public RequestHandlerComponent
public SecurityComponent
public SessionComponent
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/2.7/class-Controller.html