Table of Contents : Le Manuel

$components, $helpers et $uses

The next most oft used controller attributes tell CakePHP what helpers, components, and models you’ll be using in conjunction with the current controller. Using these attributes make these MVC classes available to the controller as a class variable ($this->ModelName, for example).

Please note that each controller has some of these classes available by default, so you may not need to configure your controller at all.

Controllers have access to their primary model available by default. Our RecipesController will have the Recipe model class available at $this->Recipe, and our ProductsController also features the Product model at $this->Product.

The Html- and SessionHelpers are always available by default, as is the SessionComponent. To learn more about these classes, be sure to check out their respective sections later in this manual.

Let’s look at how to tell a CakePHP controller that you plan to use additional MVC classes.

<?php

class RecipesController extends AppController {
    var $name = 'Recipes';

    var $uses = array('Recipe', 'User');
    var $helpers = array('Html', 'Ajax');
    var $components = array('Session', 'Email');
}

?>   
  1. <?php
  2. class RecipesController extends AppController {
  3. var $name = 'Recipes';
  4. var $uses = array('Recipe', 'User');
  5. var $helpers = array('Html', 'Ajax');
  6. var $components = array('Session', 'Email');
  7. }
  8. ?>

When defining these attributes, make sure to include the default classes (like including the HtmlHelper in the $helpers array, for example) if you intend to use them.