|
Important |
|---|---|
|
While this section will treat most of the often-used functions in Cake's Controller, it's important to remember to use http://api.cakephp.org for a full reference. |
set(
|
$var, | |
$value); |
string |
$var;
|
mixed |
$value;
|
This function is the main way to get data from your controller to your view. You can use it to hand over anything: single values, whole arrays, etc. Once you've used set(), the variable can be accessed in your view: doing set('color', 'blue') in your controller makes $color available in the view.
validateErrors(
|
); |
|
;
|
Returns the number of errors generated by an unsuccessful save.
validate(
|
); |
|
;
|
Validates the model data according to the model's validation rules. For more on validation, see Chapter "Data Validation".
render(
|
$action, | |
| $layout, | ||
$file); |
string |
$action;
|
string |
$layout;
|
string |
$file;
|
You may not often need this function, because render is automatically called for you at the end of each controller action, and the view named after your action is rendered. Alternatively, you can call this function to render the view at any point in the controller logic.
redirect(
|
$url); |
string |
$url;
|
Tell your users where to go using this function. The URL passed here can be a Cake internal URL, or a fully qualified URL (http://...).
flash(
|
$message, | |
| $url, | ||
$pause); |
string |
$message;
|
string |
$url;
|
int |
$pause;
|
This function shows $message for $pause seconds inside of your
flash layout (found in
app/views/layouts/flash.thtml) then redirects the
user to the specified $url.
|
Important |
|---|---|
|
Cake's |
Cake controllers feature a number of callbacks you can use to insert logic before or after important controller functions. To utilize this functionality, declare these functions in your controller using the parameters and return values detailed here.
beforeFilter(
|
); |
|
;
|
Called before every controller action. A useful place to check for active sessions and check roles.
afterFilter(
|
); |
|
;
|
Called after every controller action.
beforeRender(
|
); |
|
;
|
Called after controller logic, and just before a view is rendered.
While these are functions part of Cake's Object class, they are also available inside the Controller:
requestAction(
|
$url, | |
$extra); |
string |
$url;
|
array |
$extra;
|
This function calls a controller's action from any location and returns the rendered view. The $url is a Cake URL (/controllername/actionname/params). If the $extra array includes a 'return' key, AutoRender is automatically set to true for the controller action.
You can use requestAction to get data from another controller action, or get a fully rendered view from a controller.
First, getting data from a controller is simple. You just use requestAction in the view where you need the data.
// Here is our simple controller:
class UsersController extends AppController
{
function getUserList()
{
return $this->User->findAll();
}
}
Imagine that we needed to create a simple table showing the users in the system. Instead of duplicating code in another controller, we can get the data from UsersController::getUserList() instead by using requestAction().
class ProductsController extends AppController
{
function showUserProducts()
{
$this->set('users', $this->requestAction('/users/getUserList'));
// Now the $users variable in the view will have the data from
// UsersController::getUserList().
}
}
If you have an often used element in your application that is not static, you might want to use requestAction() to inject it into your views. Let's say that rather than just passing the data from UsersController::getUserList, we actually wanted to render that action's view (which might consist of a table), inside another controller. This saves us from duplicating view code.
class ProgramsController extends AppController
{
function viewAll()
{
$this->set('userTable', $this->requestAction('/users/getUserList', array('return')));
// Now, we can echo out $userTable in this action's view to
// see the rendered view that is also available at /users/getUserList.
}
}
Please note that actions called using requestAction() are rendered using an empty layout - this way you don't have to worry about layouts getting rendered inside of layouts.
The requestAction() function is also useful in AJAX situations where a small element of a view needs to be populated before or during an AJAX update.
log(
|
$message, | |
$type = LOG_ERROR); |
string |
$message;
|
int |
$type = LOG_ERROR;
|
You can use this function to log different events that happen
within your web application. Logs can be found inside Cake's
/tmp directory.
If the $type is equal to the PHP constant LOG_DEBUG, the message is written to the log as a debug message. Any other type is written to the log as an error.
// Inside a controller, you can use log() to write entries:
$this->log('Mayday! Mayday!');
//Log entry:
06-03-28 08:06:22 Error: Mayday! Mayday!
$this->log("Look like {$_SESSION['user']} just logged in.", LOG_DEBUG);
//Log entry:
06-03-28 08:06:22 Debug: Looks like Bobby just logged in.
postConditions(
|
$data); |
array |
$data;
|
A method to which you can pass $this->data, and it will pass back an array formatted as a model conditions array.
For example, if I have a person search form:
// app/views/people/search.thtml:
<?php echo $html->input('Person/last_name'); ?>
Submitting the form with this element would result in the following $this->data array:
Array
(
[Person] => Array
(
[last_name] => Anderson
)
)
At this point, we can use postConditions() to format this data to use in model:
// app/controllers/people_controller.php:
$conditions = $this->postConditions($this->data);
// Yields an array looking like this:
Array
(
[Person.last_name] => Anderson
)
// Which can be used in model find operations:
$this->Person->findAll($conditions);