3.6.2 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;
}
}
?>
<?phpclass MathComponent extends Object {function doComplexOperation($amount1, $amount2) {return $amount1 + $amount2;}}?>
Once our component is finished, we can use it in the application’s controllers by placing the component's name (minus the "Component" part) in the controller’s $components array. The controller will automatically be given a new attribute named after the component, through which we can access an instance of it:
/* Make the new component available at $this->Math,
as well as the standard $this->Session */
var $components = array('Math', 'Session');
/* Make the new component available at $this->Math,as well as the standard $this->Session */var $components = array('Math', 'Session');
3.6.2.1 MVC Class Access Within Components
To get access to the controller instance from within your newly created component, you’ll need to implement the initialize() or startup() method. These special methods take a reference to the controller as their first parameter and are automatically called. The initialize method is called before the controller's beforeFilter() method, the startup() method 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, you will need to use the initialize() method of the component.
<?php
class CheckComponent extends Object {
//called before Controller::beforeFilter()
function initialize(&$controller) {
// saving the controller reference for later use
$this->controller =& $controller;
}
//called after Controller::beforeFilter()
function startup(&$controller) {
}
function redirectSomewhere($value) {
// utilizing a controller method
$this->controller->redirect($value);
}
}
?> <?phpclass CheckComponent extends Object {//called before Controller::beforeFilter()function initialize(&$controller) {// saving the controller reference for later use$this->controller =& $controller;}//called after Controller::beforeFilter()function startup(&$controller) {}function redirectSomewhere($value) {// utilizing a controller method$this->controller->redirect($value);}}?>
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.
<?php
class MyComponent extends Object {
// This component uses other components
var $components = array('Session', 'Math');
function doStuff() {
$result = $this->Math->doComplexOperation(1, 2);
$this->Session->write('stuff', $result);
}
}
?> <?phpclass MyComponent extends Object {// This component uses other componentsvar $components = array('Session', 'Math');function doStuff() {$result = $this->Math->doComplexOperation(1, 2);$this->Session->write('stuff', $result);}}?>
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 {
function doComplexOperation($amount1, $amount2) {
return $amount1 + $amount2;
}
function doUberComplexOperation ($amount1, $amount2) {
$userInstance = ClassRegistry::init('User');
$totalUsers = $userInstance->find('count');
return ($amount1 + $amount2) / $totalUsers;
}
}
?> <?phpclass MathComponent extends Object {function doComplexOperation($amount1, $amount2) {return $amount1 + $amount2;}function doUberComplexOperation ($amount1, $amount2) {$userInstance = ClassRegistry::init('User');$totalUsers = $userInstance->find('count');return ($amount1 + $amount2) / $totalUsers;}}?>
