CakePHP default dateTime()
In the FormHelper class of Cake 1.2 there’s a method for generating fields for a datetime. It is not documented very well but you can pass a timestamp into the “selected” parameter and it will default the dropdowns to the date and time of that timestamp.
For instance, if you make this call:
echo $form->dateTime('GiftCard.expire','MDY','NONE',strtotime('+1 month'));
it will result in three select fields; Month, Day, Year; and they will be defaulted to one month from today. strtotime(’+1 month’) returns a timestamp corresponding one month from today.
Extending CakePHP’s beforeFilter()
This is a simple addition to app_controller.php to allow a more customized beforeFilter() callback.
I set out to get a little more functionality out of the controller callback beforeFilter(). I had 2 requirements for this extended functionality:
- Specify which actions to apply the callback to.
- Ability to specify multiple methods beforeFilter would call.
Let me give a very simple example of how to use this snippet:
in your controller define $beforeFilter which is an array of methods to call and the parameters.
class TestingsController extends AppController
{
var $name = 'Testings';
var $beforeFilter = array('requireLogin'=>array('only'=>array('add','edit','delete')));
function index(){}
function view($id){}
function add(){}
function edit($id=null){}
function delete($id){}
}
The $beforeFilter instance variable holds an array of methods to be called before your actions are called. In the example above, it says to call the ‘requireLogin’ method only when the ‘add’,'edit’,'delete’ actions are being called.
This next example shows you how to make certain actions excluded from the callback:
class TestingsController extends AppController
{
var $name = 'Testings';
var $beforeFilter = array('requireLogin'=>array('except'=>array('index')));
function index(){}
function view($id){}
function add(){}
function edit($id=null){}
function delete($id){}
}
You can also send in a parameter called ‘args’ which will call your method with the args
var $beforeFilter = array('requireLogin'=>array('except'=>array('index'),
'args'=>array('arg1','arg2')));
In order to make this all happen you need to place this method in your app_controller.php class.
It will get called before every action. If you have not defined $beforeFilter then it will skip any processing.
function beforeFilter(){
if(empty($this->beforeFilter)) return true;
$failures = false;
foreach($this->beforeFilter as $func_name=>$func){
$call_func = true;
if(!empty($func['only'])){
if(!in_array($this->action,$func['only']))
$call_func = false;
}
if(!empty($func['except'])){
if(in_array($this->action,$func['except']))
$call_func = false;
}
if($call_func){
$args = (isset($func['args'])) ? implode(',',$func['args']) : null;
if(!$this->{$func_name}($args)){
$failures = true;
break;
}
}
}
return !$failures;
}
Rails-like Flash messages in CakePHP
Another thing lacking in CakePHP is it’s flash messaging mechanism. Right out of the box the flash() method in CakePHP displays your message on a stark page and then redirects to the page you want to go to. The nice thing about Ruby on Rails is that adding a flash message actually redirects you to your destination and then displays the message on that page. It’s much nicer, in my opinion.
Here’s a quick and easy way to get CakePHP’s flash() to act like Rails’ flash();
In app_controller.php put this method:
function flash($msg,$to){
$this->Session->setFlash($msg);
$this->redirect($to);
exit;
}
And in your layout add:
$session->flash();
Because we’re overriding the controller flash() method in app_controller.php you dont have to go and change any of your existing flash() calls.
Please view this article on the cake Bakery. it has this same methodology but adds some javascript highlighing and other stuff. This article has been removed from the cake bakery.
Recently
- 09.08 CakePHP default dateTime()
- 03.24 Extending CakePHP’s beforeFilter()
- 03.17 Rails-like Flash messages in CakePHP
- 03.08 CakePHP thumbnails with phpThumb
- 02.15 CakePHP model validation
- 02.10 CakePHP - saving dates in your model
- 09.19 Importing multiple pdf pages and documents into PDFlib - Part 2
- 09.18 Quick Link - Every CSS based layout you could want
- 09.04 Mongrel pid error fixed
- 09.04 Typo, Work in Progress