Table of Contents : Le Manuel

Components

 

Introduction

Components are packages of logic that are shared between controllers. If you find yourself wanting to copy and paste things between controllers, you might consider wrapping some functionality in a component.

CakePHP also comes with a fantastic set of core components you can use to aid in:

  • Security
  • Sessions
  • Access control lists
  • Emails
  • Cookies
  • Authentication
  • Request handling

Each of these core components are detailed in their own chapters. For now, we’ll show you how to create your own components. Creating components keeps controller code clean and allows you to reuse code between projects.

Créer des Components personnalisés

Suppose our online application needs to perform a complex mathematical operation in many different parts of the application. We could create a component to house this shared logic for use in many different controllers.

The first step is to create a new component file and class. Create the file in /app/controllers/components/math.php. The basic structure for the component would look something like this.

<?php

class MathComponent extends Object {
    function doComplexOperation($amount1, $amount2) {
        return $amount1 + $amount2;
    }
}

?>
  1. <?php
  2. class MathComponent extends Object {
  3. function doComplexOperation($amount1, $amount2) {
  4. return $amount1 + $amount2;
  5. }
  6. }
  7. ?>

Once our component is finished, we can use it in the application’s controllers by placing the components name in the controller’s $components arrays.

//Make the new component available at $this->Math

var $components = array('Math', 'Session');
  1. //Make the new component available at $this->Math
  2. var $components = array('Math', 'Session');

Classe d'accès MVC dans les Components

To get access to the controller instance from within your newly created component, you’ll need to implement the startup() method. This special method takes a reference to the controller as its first parameter, and is automatically called after the controller’s beforeFilter() method. If for some reason you do not want the startup() method called when the controller is setting things up, set the class variable $disableStartup to true.

If you want to insert some logic before a controller’s before has been called, use the initialize() method of the component.

<?php

class MathComponent extends Object {
    //called before Controller:beforeFilter()
    function initialize() {

    }

    //called after Controller::beforeFilter()
    function startup(&$controller) {

    }

    function doComplexOperation($amount1, $amount2) {
        return $amount1 + $amount2;
    }
}

?>
  1. <?php
  2. class MathComponent extends Object {
  3. //called before Controller:beforeFilter()
  4. function initialize() {
  5. }
  6. //called after Controller::beforeFilter()
  7. function startup(&$controller) {
  8. }
  9. function doComplexOperation($amount1, $amount2) {
  10. return $amount1 + $amount2;
  11. }
  12. }
  13. ?>

You might also want to utilize other components inside a custom component. To do so, just create a $components class variable (just like you would in a controller) as an array that holds the names of components you wish to utilize.