大胆假设,小心求证         记得这句话是以前读高三的时候,一个数学老师(圆头,有点胖胖的老师,不是很记得),经常挂在嘴边的一句话,

对于我们不理解,或者说是无法确定的东西,我们不妨先大胆地去假设,然后再一步一步逼近,去认清那个东西。

让我敬佩的还是高三()时候 的物理老师,正式厉害,我的物理成绩也是妥妥的,做题无比的淡定。以前所有的忧虑情绪皆不见了。做物理题目就是享受。

扯远了。。。

回到正题,caKephp 中所谓的component 是个什么概念呢?不妨大胆假设,小心求证!

其实,在cakephp中,component 这个概念其实就是 controller 中一些想在不同的controller中都能够复用的方法罢了,可以都放到 AppController类中,

或者,独立出来,放到一个 component 中,执行每一个controller 的时候,会自动加载 comonents  ,只要这个comopnet 有在controller中引用,引用很简单,就是定义 public $components = array('xxxx') ,定义一个这样的属性就可以了。

----------------------------------------------------------------------

 All the business logic should go in my controllers, but what if I want to re-use something elsewhere?

Good question. You will almost always have to create some complex logic for an application, and you usually want to re-use part of that logic. The most common way to include an application-wide function or variable so that it's available in every controller is to define it in your AppController file. This file basically consists of an empty class that extends Cake's internal Controller class, and is located in the /cake/ directory. You can move it to your /app/ directory and create methods that will be available in all of your custom controllers that extend AppController. Even if you're not planning to use an AppController at first, it's often wise to create custom controllers which extend AppController rather than the Controller class.

An easy way to create custom classes handling a specific task is to create a component. Components can be loaded automatically in controllers (and only inside controllers) by adding a variable named $components:

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

CakePHP comes with some default components such as Session, which offers convenient ways to organize session data, or RequestHandler, which can be used to determine more information about HTTP requests. These are documented in the CakePHP manual:

Controller Extensions (“Components”)

A Component is a class that aids in controller logic. If you have some logic you want to share between controllers (or applications), a component is usually a good fit. As an example, the core EmailComponent class makes creating and sending emails a snap. Rather than writing a controller method in a single controller that performs this logic, you can package the logic so it can be shared.

Controllers are also fitted with callbacks. These callbacks are available for your use, just in case you need to insert some logic between CakePHP’s core operations. Callbacks available include:

  • afterFilter(), executed after all controller logic, including the rendering of the view
  • beforeFilter(), executed before any controller action logic
  • beforeRender(), executed after controller logic, but before the view is rendered

cakephp2.3.8中何为component的更多相关文章

  1. vue中extend/component/mixins/extends的区别

    vue中extend/component/mixins/extends的区别 教你写一个vue toast弹窗组件 Vue.extend构造器的延伸

  2. React 中的 Component、PureComponent、无状态组件 之间的比较

    React 中的 Component.PureComponent.无状态组件之间的比较 table th:first-of-type { width: 150px; } 组件类型 说明 React.c ...

  3. React Native 中的component 的生命周期

    React Native中的component跟Android中的activity,fragment等一样,存在生命周期,下面先给出component的生命周期图 getDefaultProps ob ...

  4. spring中的@component

    @component (把普通pojo实例化到spring容器中,相当于配置文件中的 <bean id="" class=""/>) 泛指各种组件, ...

  5. net core中Vue.component单独一个文件不运行,不报错的处理

    Vue.component代码段原先是放到view下的cshtml中的,可以正常运行,后来为了方便代码管理,将这块代码块单独放到一个js文件中,结果点击按钮等等都没有任何反应了,同时js控制台也不报错 ...

  6. [原]Unity3D深入浅出 - 认识开发环境中的Component(组件)菜单

    Component(组件)是用来添加到GameObject对象上的一组相关属性,本质上每个组件都是一个类的实例,比如在Cube上添加一个Mesh网格,即面向对象的思维方式可以理解成Cube对象里包含了 ...

  7. 《学习记录》ng2-bootstrap中的component使用教程

    前序: 现在angular2已经除了集成的angular-cli,建议大家可以基于这个来快速开发ng2的项目,不用自己再搭建环境: 相关内容请前往:https://angular.cn/docs/ts ...

  8. 005 Spring和SpringBoot中的@Component 和@ComponentScan注解

    今天在看@ComponentScan,感觉不是太理解,下面做一个说明. 1.说明 ComponentScan做的事情就是告诉Spring从哪里找到bean 2.细节说明 如果你的其他包都在使用了@Sp ...

  9. 在nuxt项目中使用component组件

    编写组件页面 1.在components下新建一个新建组件页面,如下所示 2.新建完成之后初始页面的代码如下所示: 3.下面从element-ui上找一个顶部导航菜单写到组件页面. <el-me ...

随机推荐

  1. Node.js——render封装

    封装 挂在到res上

  2. 常用css属性拓展

    text-overflow:clip | ellipsis(默认值:clip)clip:当内联内容溢出块容器时,将溢出部分裁切掉.ellipsis:当内联内容溢出块容器时,将溢出部分替换为(...). ...

  3. 在死循环中使用Scanner获得键盘输入

    1. 编译时无错误和警告,且运行过程中无异常的代码示例 //编译时无错误和警告,且运行过程中无异常的代码示例package scanner_test; import java.util.*; publ ...

  4. vue-cli中使用rem,vue自适应

    2018年04月27日 09:25:34 董小礼 阅读数:4196    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_39588818/ar ...

  5. Windos无法验证文件数组签名

    参考链接:https://jingyan.baidu.com/article/09ea3ede6982c4c0aede39e6.html Windows无法验证文件数字签名而无法启动,照以下去做,可以 ...

  6. jsonP 现在360浏览器竟然阻止本机 jquery load一些html js什么的

    别的浏览器正常可以jquery.load本机文件,但是360浏览器不行了,缺德啊!! jsonP代码 index3.html <!DOCTYPE HTML PUBLIC "-//W3C ...

  7. bat copy

    @echo off regedit /s %~dp0regedit.reg                                          //注册注册表xcopy "D: ...

  8. 08CSS边框边距

    CSS边框边距 边框样式——border-style border-top-style border-bottom-style border-left-style border-right-style ...

  9. 执行jar包报错:udfFull.jar中没有主清单属性

    在windows系统的cmd命令行窗口中执行: java -jar udfFull.jar {"movie":"1287","rate":& ...

  10. hdfs深入:06、hdfs的写入过程

    7.HDFS的文件写入过程 详细步骤解析: 1. client发起文件上传请求,通过RPC与NameNode建立通讯,NameNode检查目标文件是否已存在,父目录是否存在,返回是否可以上传: 2. ...