Autres méthodes utiles
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.
Returns the referring URL for the current request.
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.
Use this method to turn a set of POSTed model data (from HtmlHelper-compatible inputs) into 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() {
$o = $this->Orders->findAll($this->postConditions($this->data));
$this->set('orders', $o);
}
function index() {$o = $this->Orders->findAll($this->postConditions($this->data));$this->set('orders', $o);}
If $this->data[‘Order’][‘destination’] equals “Old Towne Bakery”, postConditions converts that condition to an array compatible for use in a Model->findAll() 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’
$o = $this->Order->findAll($this->postConditions(
$this->data,
array('>=', 'LIKE')
));
/*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’$o = $this->Order->findAll($this->postConditions($this->data,array('>=', 'LIKE')));
The key in specifying the operators is the order of the columns in the $this->data array. Since num_items is first, the >= operator applies to it.
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.
This method is used for paginating results fetched by your models. You can specify page sizes, model find conditions and more. Details on this method follow later. Check out the pagination chapter later on in this manual.
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). If the $options array includes a 'return' value, AutoRender is automatically set to true for the controller action, having requestAction hand you back a fully rendered view.
Note: while you can use requestAction() to retrieve a fully rendered view, the performance hit you take on running through the whole view layer another time isn’t often worth it. The requestAction() method is best used in conjunction with elements–as a way to fetch business logic for an element before rendering.
First, let’s look at how to get data from a controller action. First, we need to set up a controller action that returns some data we might need in various places throughout the application:
// Here is our simple controller:
class UsersController extends AppController {
function getUserList() {
return $this->User->findAll('User.active = 1');
}
}
// Here is our simple controller:class UsersController extends AppController {function getUserList() {return $this->User->findAll('User.active = 1');}}
Imagine that we needed to create a simple table showing the active users in the system. Instead of duplicating list-generating 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().
}
}
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 element in your application that is not static, you might want to use requestAction() to grab controller-like logic for the element as you inject it into your views. While elements always have access to any view variables the controller has passed, this is one way to get element data from another controller.
If you have created a controller action that supplies the logic needed, you can grab that data and pass it to the second parameter of the view’s renderElement() method using requestAction().
renderElement(
'users',
$this->requestAction('/users/getUserList')
);
?>
<?phpecho $this->renderElement('users',$this->requestAction('/users/getUserList'));?>
If the $options array contains a ‘return’ value, the controller action is rendered inside an empty layout and returned. In this way, 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.
constructClasses
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.
referrer
Returns the referring URL for the current request.
disableCache
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.
postConditions
postConditions(array $data, mixed $op, string $bool, boolean $exclusive)
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 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() {
$o = $this->Orders->findAll($this->postConditions($this->data));
$this->set('orders', $o);
}
function index() {$o = $this->Orders->findAll($this->postConditions($this->data));$this->set('orders', $o);}
If $this->data[‘Order’][‘destination’] equals “Old Towne Bakery”, postConditions converts that condition to an array compatible for use in a Model->findAll() 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’
$o = $this->Order->findAll($this->postConditions(
$this->data,
array('>=', 'LIKE')
));
/*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’$o = $this->Order->findAll($this->postConditions($this->data,array('>=', 'LIKE')));
The key in specifying the operators is the order of the columns in the $this->data array. Since num_items is first, the >= operator applies to it.
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.
paginate
This method is used for paginating results fetched by your models. You can specify page sizes, model find conditions and more. Details on this method follow later. Check out the pagination chapter later on in this manual.
requestAction
requestAction(string $url, array $options)
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). If the $options array includes a 'return' value, AutoRender is automatically set to true for the controller action, having requestAction hand you back a fully rendered view.
Note: while you can use requestAction() to retrieve a fully rendered view, the performance hit you take on running through the whole view layer another time isn't often worth it. The requestAction() method is best used in conjunction with elements–as a way to fetch business logic for an element before rendering.
First, let's look at how to get data from a controller action. First, we need to set up a controller action that returns some data we might need in various places throughout the application:
// Here is our simple controller:
class UsersController extends AppController {
function getUserList() {
return $this->User->findAll('User.active = 1');
}
}
// Here is our simple controller:class UsersController extends AppController {function getUserList() {return $this->User->findAll('User.active = 1');}}
Imagine that we needed to create a simple table showing the active users in the system. Instead of duplicating list-generating 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().
}
}
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 element in your application that is not static, you might want to use requestAction() to grab controller-like logic for the element as you inject it into your views. While elements always have access to any view variables the controller has passed, this is one way to get element data from another controller.
If you have created a controller action that supplies the logic needed, you can grab that data and pass it to the second parameter of the view's renderElement() method using requestAction().
renderElement(
'users',
$this->requestAction('/users/getUserList')
);
?>
<?phpecho $this->renderElement('users',$this->requestAction('/users/getUserList'));?>
If the $options array contains a 'return' value, the controller action is rendered inside an empty layout and returned. In this way, 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.

login to add a comment