3.5.5 Controller Methoden
De originele tekst van deze rubriek is gewijzigd nadat deze is vertaald. Help om dit verschil op te lossen. Je kunt:
Voor een complete lijst van controller methoden en hun beschrijvingen dien je de CakePHP API te bezoeken. Zie http://api.cakephp.org/1.2/class_controller.html.
3.5.5.1 Interactie met views
3.5.5.1.1 set
set(string $var, mixed $value)
De set() methode is de manier om data van de controller naar de view over te brengen. Eenmaal een set() gebruikt maakt het mogelijk om deze variable in de view te gebruiken
<?php
// Allereerst definieren we de variable (in de controller) met de gewenste data
$this->set('color', 'blauw');
// Vervolgens kan je deze in de view aanroepen dmv:
?>
Je hebt een <?php echo $color; ?> t-shirt besteld.
<?php// Allereerst definieren we de variable (in de controller) met de gewenste data$this->set('color', 'blauw');// Vervolgens kan je deze in de view aanroepen dmv:?>Je hebt een <?php echo $color; ?> t-shirt besteld.
De set() methode kan ook worden gebruikt voor associatieve arrays. Op deze manier kan je snel informatie overbrengen.
Array keys zullen worden geinflecteerd voordat ze aan de view worden doorgegeven ('underscored_key' wordt 'underscoredKey', etc.):
<?php
$data = array(
'color' => 'pink',
'type' => 'sugar',
'base_price' => 23.95
);
// definieer $color, $type, and $basePrice
// en maak ze toegankelijk voor de view:
$this->set($data);
?>
<?php$data = array('color' => 'pink','type' => 'sugar','base_price' => 23.95);// definieer $color, $type, and $basePrice// en maak ze toegankelijk voor de view:$this->set($data);?>
3.5.5.1.2 render
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
render(string $action, string $layout, string $file)
The render() method is automatically called at the end of each requested controller action. This method performs all the view logic (using the data you’ve given in using the set() method), places the view inside its layout and serves it back to the end user.
The default view file used by render is determined by convention. If the search() action of the RecipesController is requested, the view file in /app/views/recipes/search.ctp will be rendered.
class RecipesController extends AppController {
...
function search() {
// Render the view in /views/recipes/search.ctp
$this->render();
}
...
} class RecipesController extends AppController {...function search() {// Render the view in /views/recipes/search.ctp$this->render();}...}
Although CakePHP will automatically call it (unless you’ve set $this->autoRender to false) after every action’s logic, you can use it to specify an alternate view file by specifying an action name in the controller using the $action parameter. If $action starts with '/', it is assumed to be a view or element file relative to the /app/views folder. This allows direct rendering of elements, very useful in ajax calls.
// Render the element in /views/elements/ajaxreturn.ctp
$this->render('/elements/ajaxreturn');
// Render the element in /views/elements/ajaxreturn.ctp$this->render('/elements/ajaxreturn');
You can also specify an alternate view or element file using the third parameter, $file. When using $file, don't forget to utilize a few of CakePHP’s global constants (such as VIEWS).
The $layout parameter allows you to specify the layout the view is rendered in.
3.5.5.2 Flow Control
3.5.5.2.1 redirect
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
redirect(mixed $url, integer $status, boolean $exit)
The flow control method you’ll use most often is redirect(). This method takes its first parameter in the form of a CakePHP-relative URL. When a user has successfully placed an order, you might wish to redirect them to a receipt screen.
function placeOrder() {
//Logic for finalizing order goes here
if($success) {
$this->redirect(array('controller' => 'orders', 'action' => 'thanks'));
} else {
$this->redirect(array('controller' => 'orders', 'action' => 'confirm'));
}
}
function placeOrder() {//Logic for finalizing order goes hereif($success) {$this->redirect(array('controller' => 'orders', 'action' => 'thanks'));} else {$this->redirect(array('controller' => 'orders', 'action' => 'confirm'));}}
You can also use a relative or absolute URL as the $url argument:
$this->redirect('/orders/thanks'));
$this->redirect('http://www.example.com');
$this->redirect('/orders/thanks'));$this->redirect('http://www.example.com');
You can also pass data to the action:
$this->redirect(array('action' => 'edit', $id));
$this->redirect(array('action' => 'edit', $id));
The second parameter of redirect() allows you to define an HTTP status code to accompany the redirect. You may want to use 301 (moved permanently) or 303 (see other), depending on the nature of the redirect.
The method will issue an exit() after the redirect unless you set the third parameter to false.
If you need to redirect to the referer page you can use:
$this->redirect($this->referer());
$this->redirect($this->referer());
3.5.5.2.2 flash
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
flash(string $message, string $url, integer $pause)
Like redirect(), the flash() method is used to direct a user to a new page after an operation. The flash() method is different in that it shows a message before passing the user on to another URL.
The first parameter should hold the message to be displayed, and the second parameter is a CakePHP-relative URL. CakePHP will display the $message for $pause seconds before forwarding the user on.
For in-page flash messages, be sure to check out SessionComponent’s setFlash() method.
3.5.5.3 Callbacks
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
CakePHP controllers come fitted with callbacks you can use to insert logic just before or after controller actions are rendered.
beforeFilter()
This function is executed before every action in the controller. It's a handy place to check for an active session or inspect user permissions.
beforeRender()
Called after controller action logic, but before the view is rendered. This callback is not used often, but may be needed if you are calling render() manually before the end of a given action.
afterFilter()
Called after every controller action, and after rendering is complete. This is the last controller method to run.
CakePHP also supports callbacks related to scaffolding.
_beforeScaffold($method)
$method name of method called example index, edit, etc.
_afterScaffoldSave($method)
$method name of method called either edit or update.
_afterScaffoldSaveError($method)
$method name of method called either edit or update.
_scaffoldError($method)
$method name of method called example index, edit, etc.
3.5.5.4 Other Useful Methods
3.5.5.4.1 constructClasses
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
This method loads the models required by the controller. This loading process is done by CakePHP normally, but this method is handy to have when accessing controllers from a different perspective. If you need CakePHP in a command-line script or some other outside use, constructClasses() may come in handy.
3.5.5.4.2 referer
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
string referer(mixed $default = null, boolean $local = false)
Returns the referring URL for the current request. Parameter $default can be used to supply a default URL to use if HTTP_REFERER cannot be read from headers. So, instead of doing this:
<?php
class UserController extends AppController {
function delete($id) {
// delete code goes here, and then...
if ($this->referer() != '/') {
$this->redirect($this->referer());
} else {
$this->redirect(array('action' => 'index'));
}
}
}
?>
<?phpclass UserController extends AppController {function delete($id) {// delete code goes here, and then...if ($this->referer() != '/') {$this->redirect($this->referer());} else {$this->redirect(array('action' => 'index'));}}}?>
you can do this:
<?php
class UserController extends AppController {
function delete($id) {
// delete code goes here, and then...
$this->redirect($this->referer(array('action' => 'index')));
}
}
?>
<?phpclass UserController extends AppController {function delete($id) {// delete code goes here, and then...$this->redirect($this->referer(array('action' => 'index')));}}?>
If $default is not set, the function defaults to the root of your domain - '/'.
Parameter $local if set to true, restricts referring URLs to local server.
3.5.5.4.3 disableCache
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
Used to tell the user’s browser not to cache the results of the current request. This is different than view caching, covered in a later chapter.
The headers sent to this effect are:
Expires: Mon, 26 Jul 1997 05:00:00 GMTLast-Modified: [current datetime] GMTCache-Control: no-store, no-cache, must-revalidateCache-Control: post-check=0, pre-check=0Pragma: no-cache
3.5.5.4.4 postConditions
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
postConditions(array $data, mixed $op, string $bool, boolean $exclusive)
Use this method to turn a set of POSTed model data (from HtmlHelper-compatible inputs) into a set of find conditions for a model. This function offers a quick shortcut on building search logic. For example, an administrative user may want to be able to search orders in order to know which items need to be shipped. You can use CakePHP’s Form- and HtmlHelpers to create a quick form based on the Order model. Then a controller action can use the data posted from that form to craft find conditions:
function index() {
$conditions=$this->postConditions($this->data);
$orders = $this->Order->find("all",compact('conditions'));
$this->set('orders', $orders);
}
function index() {$conditions=$this->postConditions($this->data);$orders = $this->Order->find("all",compact('conditions'));$this->set('orders', $orders);}
If $this->data[‘Order’][‘destination’] equals “Old Towne Bakery”, postConditions converts that condition to an array compatible for use in a Model->find() method. In this case, array(“Order.destination” => “Old Towne Bakery”).
If you want use a different SQL operator between terms, supply them using the second parameter.
/*
Contents of $this->data
array(
'Order' => array(
'num_items' => '4',
'referrer' => 'Ye Olde'
)
)
*/
//Let’s get orders that have at least 4 items and contain ‘Ye Olde’
$condtions=$this->postConditions(
$this->data,
array(
'num_items' => '>=',
'referrer' => 'LIKE'
)
);
$orders = $this->Order->find("all",compact('conditions'));
/*Contents of $this->dataarray('Order' => array('num_items' => '4','referrer' => 'Ye Olde'))*///Let’s get orders that have at least 4 items and contain ‘Ye Olde’$condtions=$this->postConditions($this->data,array('num_items' => '>=','referrer' => 'LIKE'));$orders = $this->Order->find("all",compact('conditions'));
The third parameter allows you to tell CakePHP what SQL boolean operator to use between the find conditions. String like ‘AND’, ‘OR’ and ‘XOR’ are all valid values.
Finally, if the last parameter is set to true, and the $op parameter is an array, fields not included in $op will not be included in the returned conditions.
3.5.5.4.5 paginate
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
This method is used for paginating results fetched by your models. You can specify page sizes, model find conditions and more. See the pagination section for more details on how to use paginate.
3.5.5.4.6 requestAction
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
requestAction(string $url, array $options)
This function calls a controller's action from any location and returns data from the action. The $url passed is a CakePHP-relative URL (/controllername/actionname/params). To pass extra data to the receiving controller action add to the $options array.
You can use requestAction() to retrieve a fully rendered view by passing 'return' in the options: requestAction($url, array('return'));
If used without caching requestAction can lead to poor performance. It is rarely appropriate to use in a controller or model.
requestAction is best used in conjunction with (cached) elements – as a way to fetch data for an element before rendering. Let's use the example of putting a "latest comments" element in the layout. First we need to create a controller function that will return the data.
// controllers/comments_controller.php
class CommentsController extends AppController {
function latest() {
return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10));
}
}
// controllers/comments_controller.phpclass CommentsController extends AppController {function latest() {return $this->Comment->find('all', array('order' => 'Comment.created DESC', 'limit' => 10));}}
If we now create a simple element to call that function:
// views/elements/latest_comments.ctp
$comments = $this->requestAction('/comments/latest');
foreach($comments as $comment) {
echo $comment['Comment']['title'];
}
// views/elements/latest_comments.ctp$comments = $this->requestAction('/comments/latest');foreach($comments as $comment) {echo $comment['Comment']['title'];}
We can then place that element anywhere at all to get the output using:
echo $this->element('latest_comments'); echo $this->element('latest_comments');
Written in this way, whenever the element is rendered, a request will be made to the controller to get the data, the data will be processed, and returned. However in accordance with the warning above it's best to make use of element caching to prevent needless processing. By modifying the call to element to look like this:
echo $this->element('latest_comments', array('cache'=>'+1 hour')); echo $this->element('latest_comments', array('cache'=>'+1 hour'));
The requestAction call will not be made while the cached element view file exists and is valid.
In addition, requestAction now takes array based cake style urls:
echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('return')); echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('return'));
This allows the requestAction call to bypass the usage of Router::url which can increase performance. The url based arrays are the same as the ones that HtmlHelper::link uses with one difference - if you are using named or passed parameters, you must put them in a second array and wrap them with the correct key. This is because requestAction only merges the named args array into the Controller::params member array and does not place the named args in the key 'named'.
echo $this->requestAction('/articles/featured/limit:3');
echo $this->requestAction('/articles/view/5');
echo $this->requestAction('/articles/featured/limit:3');echo $this->requestAction('/articles/view/5');
As an array in the requestAction would then be:
echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('named' => array('limit' => 3)));
echo $this->requestAction(array('controller' => 'articles', 'action' => 'view'), array('pass' => array(5)));
echo $this->requestAction(array('controller' => 'articles', 'action' => 'featured'), array('named' => array('limit' => 3)));echo $this->requestAction(array('controller' => 'articles', 'action' => 'view'), array('pass' => array(5)));
Unlike other places where array urls are analogous to string urls, requestAction treats them differently.
When using an array url in conjunction with requestAction() you must specify all parameters that you will need in the requested action. This includes parameters like $this->data and $this->params['form']. In addition to passing all required parameters, named and pass parameters must be done in the second array as seen above.
3.5.5.4.7 loadModel
Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen
loadModel(string $modelClass, mixed $id)
The loadModel function comes handy when you need to use a model which is not the controller's default model or its associated model.
$this->loadModel('Article');
$recentArticles = $this->Article->find('all', array('limit' => 5, 'order' => 'Article.created DESC'));
$this->loadModel('Article');$recentArticles = $this->Article->find('all', array('limit' => 5, 'order' => 'Article.created DESC'));
$this->loadModel('User', 2);
$user = $this->User->read();
$this->loadModel('User', 2);$user = $this->User->read();


























