Introduction to CakePHP Controllers

Naming conventions:

  • RecipesController
  • recipes_controller.php
  • extends AppController

Different Variables
var $name = ‘Recipes’;
var $uses = array(‘Recipe’, ‘User’);
var $helpers = array(‘Ajax’);
var $components = array(‘Email’);

$layout
$this->layout = ‘ajax’;

/app/views/layouts/default.ctp
$pageTitle
need to add $title_for_layout tag using the below inside a method
$this->pageTitle = ‘My search engine optimized title’;
$params
$this->params
access to information that has been handed to the controller via POST or GET operations
$this->params['form']
Any POST data from any form is stored here, including information also found in $_FILES.
$this->params['isAjax']
Stores 1 if the current request is an ajax call, 0 if not. This variable is only set if the RequestHandler Component is being used in the controller.
$this->params['controller']
Stores the name of the current controller handling the request. For example, if the URL /posts/view/1 was requested, $this->params['controller'] would equal “posts”.
$this->params['action']
Stores the name of the current action handling the request. For example, if the URL /posts/view/1 was requested, $this->params['action'] would equal “view”.
$this->params['pass']
Stores the GET query string passed with the current request. For example, if the URL /posts/view/?var1=3&var2=4 was requested, $this->params['pass'] would equal “?var1=3&var2=4?.
$this->params['url']
Stores the current URL requested, along with key-value pairs of get variables. For example, if the URL /posts/view/?var1=3&var2=4 was called, $this->params['url'] would contain:
[url] => Array
(
[url] => posts/view
[var1] => 3
[var2] => 4
)
$this->data
Used to handle POST data sent from the FormHelper forms to the controller.
[/html]

[html]
When the form is submitted to the controller via POST, the data shows up in this->data
$this->data['User']['first_name'];
$this->params['named']
Stores any named parameters in the url query string in the form /key:value/. For example, if the URL /posts/view/var1:3/var2:4 was requested, $this->params['named'] would be an array containing:
[named] => Array
(
[var1] => 3
[var2] => 4
)
set(string $var, mixed $value)
The set() method is the main way to send data from your controller to your view. Once you’ve used set(), the variable can be accessed in your view.
$this->set(‘color’, ‘pink’);
You have selected <?php echo $color; ?> icing for the cake.
$data = array(
‘color’ => ‘pink’,
‘type’ => ’sugar’,
‘base_price’ => 23.95
);

this->set($data);
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.

function search() {
// Render the view in /views/recipes/search.ctp
$this->render();
}

redirect(string $url, integer $status, boolean $exit)

if($success) {
$this->redirect(array(‘controller’ => ‘orders’, ‘action’ => ‘thanks’));
} else {
$this->redirect(array(‘controller’ => ‘orders’, ‘action’ => ‘confirm’));
}

flash(string $message, string $url, integer $pause)
Similarly, 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.
Callbacks
akePHP 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.
afterRender()
Called after an action has been rendered.
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.