{53} - 3.5.2.2 $components, $helpers and $uses

{5217} - 3.5.2.2 $components, $helpers and $uses

The next most often 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).

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

Each of these variables are merged with their inherited values, therefore it is not necessary (for example) to redeclare the Form helper, or anything that is declared in your App controller.

Differences

Lines: 1-2Lines: 1-18
 <title>$components, $helpers and $uses</title> <title>$components, $helpers and $uses</title>
 +<p>The next most often 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 (<code>$this-&gt;ModelName</code>, for example).</p>
 +<p class="note">Each controller has some of these classes available by default, so you may not need to configure your controller at all.</p>
 +<p>Controllers have access to their primary model available by default. Our RecipesController will have the Recipe model class available at <code>$this-&gt;Recipe</code>, and our ProductsController also features the Product model at <code>$this-&gt;Product</code>.</p>
 +<p>The Html, Form, and Session Helpers 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.</p>
 +<p>Let’s look at how to tell a CakePHP controller that you plan to use additional MVC classes.</p>
 +<pre>
 +&lt;?php
 +class RecipesController extends AppController {
 + var $name = 'Recipes';
 + var $uses = array('Recipe', 'User');
 + var $helpers = array('Ajax');
 + var $components = array('Email');
 +}
 +?&gt;
 +</pre>
 +<p>Each of these variables are merged with their inherited values, therefore it is not necessary (for example) to redeclare the Form helper, or anything that is declared in your App controller.</p>