Table of Contents : El manual

Creating Custom Components

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');

MVC Class Access Within 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 beforeFilter() 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.

To access/use a model in a component is not generally recommended; however if weighing the possibilities this is what you want to do you'll need to instanciate your model class and use it manually. Here's an example:

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

	}

	//called after Controller::beforeFilter()
	function startup(&$controller) {
		// Saving a reference to the controller on the component instance
		$this->controller =& $controller;
	}

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

	function doUberComplexOperation ($amount1, $amount2) {
		$userInstance = ClassRegistry::init('User');
		$totalUsers = $userInstance->find('count');
		return ($amount1 + $amount2) / $totalUsers;
	}
}
?>
  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. // Saving a reference to the controller on the component instance
  9. $this->controller =& $controller;
  10. }
  11. function doComplexOperation($amount1, $amount2) {
  12. return $amount1 + $amount2;
  13. }
  14. function doUberComplexOperation ($amount1, $amount2) {
  15. $userInstance = ClassRegistry::init('User');
  16. $totalUsers = $userInstance->find('count');
  17. return ($amount1 + $amount2) / $totalUsers;
  18. }
  19. }
  20. ?>