3.6.2.2 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;}}?>
