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

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

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

扯远了。。。

回到正题,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——网站访问一般流程

  2. 原生jsonp跨域

    <script> // jsonp跨域原生写法 var script = document.createElement('script'); script.src = 'http://19 ...

  3. WebSocket 的一些简单页面推送使用

    因为做通信项目的时候,需要实时获取每个分机的当前状态,发现websocket还不错,只是对浏览器的要求比较高, 针对特定用户推送消息,网上有一些 public class GetHttpSession ...

  4. 谈谈JVM内存区域的划分

    我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互,而CPU运转速度越来越快,磁盘远远跟不上CPU的读写速度,才设计了内存,用户缓冲用户IO等待导致CPU的等 ...

  5. Django中使用多线程发送邮件

    1.settings.py 增加Email设置   #mail EMAIL_HOST = ‘smtp.gmail.com’                   #邮件smtp服务器 EMAIL_POR ...

  6. 【计算机网络】2.6 P2P应用

    第二章第六节 P2P应用 在本节内容开始前,我们要先来对P2P架构有一个宏观的认知: P2P:(Peer to Peer 对等结构)   以对等方式进行通信,并不区分客户端和服务端,而是平等关系进行通 ...

  7. hdfs深入:09、获取分布式文件系统客户端的几种方式

    FileSystem是一个抽象类:获取一个抽象类有两种方式:第一种:看这个抽象类有没有提供什么方法返回他本身第二种:找子类 具体代码如下: /** * 通过url注册的方式访问hdfs,了解,不会用到 ...

  8. ionic提供的配色方案

    .light #ffffff .stable #f8f8f8 .positive #387ef5 .calm #11c1f3 .balanced #33cd5f .energized #ffc900 ...

  9. 「 HDU P4734 」 F(x)

    # 题目大意 对于一个数 $x$,它的每一位数字分别是 $A_{n}A_{n-1}A_{n-2}\cdots A_{2}A_{1}$,定义其权重 $f(x)=\sum_{i=1}^{n}\left(A ...

  10. 关于C/C++的一些思考(4)

    C++的类型转换规则: 对于数值类型而言:当一个较小数值类型赋值给一个较大数值类型的时候,C++支持隐式的类型转换,不会有任何的损失: 对于数值类型而言,当一个较大数值类型赋值给一个较小数值类型时候, ...