PHP头条
热点:

cakephp组件中访问控制器的实例


如果要在组件中访问控制器的实例(instance),需要实现组件的initialize()或者startup()方法。这两个特殊的方法接收一个到控制器的引用作为第一个参数并且被自动调用。initialize()方法在控制器的beforeFilter()方法执行前被自动调用,startup()方法在beforeFilter方法执行后被自动调用。如果出于某些原因你不想startup()方法在控制器执行构筑操作的时候被调用,那么可以设置类成员变量$disableStartuptrue

如果你想在控制器的beforeFilter()之前干点什么的话,initialize()方法是最合适的选择。

controller =& $controller;
	}

	//在Controller::beforeFilter()之后被调用
	function startup(&$controller) {
	}

	function redirectSomewhere($value) {
		// 使用控制器的方法
		$this->controller->redirect($value);
	}
}
?>
  1. class CheckComponent extends Object {
  2. //在Controller::beforeFilter()之前被调用
  3. function initialize(&$controller) {
  4. // saving the controller reference for later use
  5. $this->controller =& $controller;
  6. }
  7. //在Controller::beforeFilter()之后被调用
  8. function startup(&$controller) {
  9. }
  10. function redirectSomewhere($value) {
  11. // 使用控制器的方法
  12. $this->controller->redirect($value);
  13. }
  14. }
  15. ?>

有时候你也许会需要在组件中使用其他的组件,你只需要在组件中声明类成员变量$components(就像在控制器中一样),他的值是一个你想使用的组件名的数组。

Math->doComplexOperation(1, 2);
		$this->Session->write('stuff', $result);
	}

}
?>
  1. class MyComponent extends Object {
  2. // 需要使用的其他组件
  3. var $components = array('Session', 'Math');
  4. function doStuff() {
  5. $result = $this->Math->doComplexOperation(1, 2);
  6. $this->Session->write('stuff', $result);
  7. }
  8. }
  9. ?>

在组件中使用或者访问模型(model)并不是很推荐;不过这种可能性也不是没有,在这种情况下,你需要手动的生成模型的实例进行使用。下面是一个例子:

find('count');
		return ($amount1 + $amount2) / $totalUsers;
	}
}
?>
  1. class MathComponent extends Object {
  2. function doComplexOperation($amount1, $amount2) {
  3. return $amount1 + $amount2;
  4. }
  5. function doUberComplexOperation ($amount1, $amount2) {
  6. $userInstance = ClassRegistry::init('User');
  7. $totalUsers = $userInstance->find('count');
  8. return ($amount1 + $amount2) / $totalUsers;
  9. }
  10. }
  11. ?>
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.

Existing->foo();
    }

    function bar() {
        // ...
    }
}
?>
  1. class CustomComponent extends Object {
  2. var $name = "Custom"; // the name of your component
  3. var $components = array( "Existing" ); // the other component your component uses
  4. function initialize(&$controller) {
  5. $this->Existing->foo();
  6. }
  7. function bar() {
  8. // ...
  9. }
  10. }
  11. ?>
Parent->bar();
    }

    function foo() {
        // ...
    }
}
?>
  1. class ExistingComponent extends Object {
  2. var $name = "Existing";
  3. function initialize(&$controller) {
  4. $this->Parent->bar();
  5. }
  6. function foo() {
  7. // ...
  8. }
  9. }

www.phpzy.comtrue/phpkj/12169.htmlTechArticlecakephp组件中访问控制器的实例 如果要在组件中访问控制器的实例( instance ),需要实现组件的 initialize() 或者 startup() 方法。这两个特殊的方法接收一个到控制器的引用作为第一个参数并...

相关文章

相关频道:

PHP之友评论

今天推荐