9.1 Controllers: predefined behaviours and functionalities
9.1.1 Interception methods
Controllers extended by our AbstractController or ActionController have 2 interception methods: preHandle(Request $Req, Response $Res), that is called before the actual handling of the Request, and must return true, otherwise it stop Request handling execution; postHandle(Request $Req, Response $Res, $View=null), that is called after the Request handling. You can use these interception methods to provide default behaviours in certain situations. For instance, you can defined an application Controller base class with only these 2 methods defined, and then deriving all your application Controllers from this base class.
9.1.2 Request parameters
Controllers extended by our AbstractController or ActionController automatically provides you Request parameters, also casting them appropriately, and providing custom default values if not present. This is simply managed by the method signature and by defining details in the doc-comment:
/**
* @param Request $Req
* @param Response $Res
* @param integer $page
* @param string $orderBy
*/
function myAction(Request $Req, Response $Res, $page=1, $orderBy='dateFrom') {
// .. do stuff
}
In this example, we defined 2 parameters:
- $page, which is an integer, defaults to 1;
- $orderBy, a string, defaults to “dateFrom”.
So, inside your myAction() you will find these 2 parameters, pre populated with $_GET or $_POST values if presents, otherwise with their default values as you defined in the code.
Updated about 1 month ago