0

0

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

黄舟

黄舟

发布时间:2016-12-20 09:57:43

|

1416人浏览过

|

来源于php中文网

原创

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

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

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

class CheckComponent extends Object {

立即学习PHP免费学习笔记(深入)”;

//在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(就像在控制器中一样),他的值是一个你想使用的组件名的数组。

class MyComponent extends Object {

// 需要使用的其他组件
var $components = array('Session', 'Math');

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

}
?>

class MyComponent extends Object {

// 需要使用的其他组件

var $components = array('Session', 'Math');

function doStuff() {

$result = $this->Math->doComplexOperation(1, 2);

$this->Session->write('stuff', $result);

}

}

?>

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

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

class MathComponent extends Object {

function doComplexOperation($amount1, $amount2) {

return $amount1 + $amount2;

}

function doUberComplexOperation ($amount1, $amount2) {

$userInstance = ClassRegistry::init('User');

艾零三企业网站系统1.7 Build 120226
艾零三企业网站系统1.7 Build 120226

艾零三(a03web)企业网站系统免费版,本系统特点为后台有可视化的用户控件引用功能,无需编程即可使用丰富的企业网站功能,此版本为免费版,无任何功能限制。 系统环境 asp.net2.0+access,发布包中包含一个微服务器环境,不需要装Microsoft .NET Framework v2.0 也可以访问网站。 艾零三(a03web)企业网站系统包含功能有: 网站单页资料模块:可在后台分组设立

下载

$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.

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() {
       // ...
   }
}
?>

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() {

// ...

}

}

?>

class ExistingComponent extends Object {
   var $name = "Existing";

   function initialize(&$controller) {
       $this->Parent->bar();
   }

   function foo() {
       // ...
   }
}
?>

class ExistingComponent extends Object {

var $name = "Existing";

function initialize(&$controller) {

$this->Parent->bar();

}

function foo() {

// ...

}

}

 以上就是cakephp 组件中访问控制器的实例的内容,更多相关内容请关注PHP中文网(www.php.cn)!

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

22

2026.01.23

c++空格相关教程合集
c++空格相关教程合集

本专题整合了c++空格相关教程,阅读专题下面的文章了解更多详细内容。

24

2026.01.23

yy漫画官方登录入口地址合集
yy漫画官方登录入口地址合集

本专题整合了yy漫画入口相关合集,阅读专题下面的文章了解更多详细内容。

99

2026.01.23

漫蛙最新入口地址汇总2026
漫蛙最新入口地址汇总2026

本专题整合了漫蛙最新入口地址大全,阅读专题下面的文章了解更多详细内容。

132

2026.01.23

C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

15

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

65

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

61

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

63

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Django 教程
Django 教程

共28课时 | 3.4万人学习

React 教程
React 教程

共58课时 | 4.1万人学习

MongoDB 教程
MongoDB 教程

共17课时 | 2.3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号