REVERSEFOLDS
Random CakePHP Tips
recipes from around the world
As I find more and more tips, I'll organize this page.
Inserting data
$this->Model->create();
Model-less controller
var $uses = null;
Setting page title and heading
<h2><?php echo $this->pageTitle="Page title"; ?></h2>
Including/Requiring files
require_once(WWW_ROOT . DS . 'files' . 'includeme.txt');
Automatic Model Name
Inflector::underscore($this->name)
Selected Option
$html->selectTag('Dealer/cars', array('audi' => 'Audi TT'), 'audi');
LIKE clause
$this->Customer->findAll(array('address' => "LIKE %{$searchBy}%"));
Missing Controller
You can either leave the default 404 message or default to your homepage (or some other page. To do that create an error.php file in your app directory:Download
<?php
/**
* error.php
*
* custom error handler to redirect invalid urls to homepage
*/
class AppError extends ErrorHandler
{
function __construct($method, $messages) {
static $__previousError = null;
$this->__dispatch =& new Dispatcher();
if ($__previousError != array($method, $messages)) {
$__previousError = array($method, $messages);
if (!class_exists('AppController')) {
loadController(null);
}
$this->controller =& new AppController();
$this->__dispatch->start($this->controller);
if (method_exists($this->controller, 'apperror')) {
return $this->controller->appError($method, $messages);
}
} else {
$this->controller =& new Controller();
}
call_user_func_array(array(&$this, $method), $messages);
}
function missingController($params) {
$this->controller->redirect('/');
}
}//AppError
?>
/**
* error.php
*
* custom error handler to redirect invalid urls to homepage
*/
class AppError extends ErrorHandler
{
function __construct($method, $messages) {
static $__previousError = null;
$this->__dispatch =& new Dispatcher();
if ($__previousError != array($method, $messages)) {
$__previousError = array($method, $messages);
if (!class_exists('AppController')) {
loadController(null);
}
$this->controller =& new AppController();
$this->__dispatch->start($this->controller);
if (method_exists($this->controller, 'apperror')) {
return $this->controller->appError($method, $messages);
}
} else {
$this->controller =& new Controller();
}
call_user_func_array(array(&$this, $method), $messages);
}
function missingController($params) {
$this->controller->redirect('/');
}
}//AppError
?>