cakephp 组件中访问控制器的实例
如果你想在控制器的beforeFilter()之前干点什么的话,initialize()方法是最合适的选择。
<?php
class CheckComponent extends Object {
//在Controller::beforeFilter()之前被调用
function initialize(&$controller) {
// saving the controller reference for later use
$this->controller =& $controller;
}
//在Controller::beforeFilter()之后被调用
function startup(&$controller) {
}
function redirectSomewhere($value) {
// 使用控制器的方法
$this->controller->redirect($value);
}
}
?>
<?php
class CheckComponent extends Object {
//在Controller::beforeFilter()之前被调用
function initialize(&$controller) {
// saving the controller reference for later use
$this->controller =& $controller;
}
//在Controller::beforeFilter()之后被调用
function startup(&$controller) {
}
function redirectSomewhere($value) {
// 使用控制器的方法
$this->controller->redirect($value);
}
}
?>
有时候你也许会需要在组件中使用其他的组件,你只需要在组件中声明类成员变量$components(就像在控制器中一样),他的值是一个你想使用的组件名的数组。
<?php
class MyComponent extends Object {
// 需要使用的其他组件
var $components = array('Session', 'Math');
function doStuff() {
$result = $this->Math->doComplexOperation(1, 2);
$this->Session->write('stuff', $result);
}
}
?>
<?php
class MyComponent extends Object {
// 需要使用的其他组件
var $components = array('Session', 'Math');
function doStuff() {
$result = $this->Math->doComplexOperation(1, 2);
$this->Session->write('stuff', $result);
}
}
?>
在组件中使用或者访问模型(model)并不是很推荐;不过这种可能性也不是没有,在这种情况下,你需要手动的生成模型的实例进行使用。下面是一个例子:
<?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;
}
}
?>
<?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;
}
}
?>
See comments for this section
3.6.3.3 Using other Components in your Component
Translate
View just this section
Comments (0)
History
There is no translation yet for this section. Please help out and translate this.. More information about translations
Sometimes one of your components may need to use another.
You can include other components in your component the exact same way you include them in controllers: Use the$componentsvar.
<?php
class CustomComponent extends Object {
var $name = "Custom"; // the name of your component
var $components = array( "Existing" ); // the other component your component uses
function initialize(&$controller) {
$this->Existing->foo();
}
function bar() {
// ...
}
}
?>
<?php
class CustomComponent extends Object {
var $name = "Custom"; // the name of your component
var $components = array( "Existing" ); // the other component your component uses
function initialize(&$controller) {
$this->Existing->foo();
}
function bar() {
// ...
}
}
?>
<?php
class ExistingComponent extends Object {
var $name = "Existing";
function initialize(&$controller) {
$this->Parent->bar();
}
function foo() {
// ...
}
}
?>
<?php
class ExistingComponent extends Object {
var $name = "Existing";
function initialize(&$controller) {
$this->Parent->bar();
}
function foo() {
// ...
}
}
以上就是cakephp 组件中访问控制器的实例的内容,更多相关内容请关注PHP中文网(www.php.cn)!