You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
drodata
changed the title
Creating my first controller extension
Creating my first controller extension [CController or Controller]
Jul 7, 2015
drodata
changed the title
Creating my first controller extension [CController or Controller]
Creating my first controller extension [CController or Controller] :smoke:
Jul 7, 2015
drodata
changed the title
Creating my first controller extension [CController or Controller] :smoke:
Creating my first controller extension [CController or Controller] ❓
Jul 7, 2015
drodata
changed the title
Creating my first controller extension [CController or Controller] ❓
Creating my first controller extension [CController or Controller]
Jul 7, 2015
之前熟悉的 Controller 大多通过 Gii 来自动生成,假设创建一个
XyzController
Class, 外加一个index
action, Gii 生成的文件结构如下:假如想在另一个项目中直接使用这个 controller, 需要同时将文件
XyzController.php
和目录xyz/
分别复制到新项目的protected/
和views/
目录下。其实 Yii 中的 Controller 还有另一种形式(distributed as an extension ):controller extension. 它能将 controller 文件及其 views 文件放在同一个目录下,让代码重用(MVC 的精髓)变得更加简单。下面我们以
blog
application 中的SiteController
为例,将它转换成 controller extension.先对两种类型的 controller 对应的文件结构进行对比,以便对 controller extension 有一个直观的印象。
传统形式:
controller extension 形式:
转换的过程实际上就是移动代码的位置,代码几乎不需要做任何改动,除了一点以外:需要将
SiteController
Class 的父类由CController
改为CExtController
.CExtController
是CController
的子类.extensions
下放了很多插件,为了让结构清晰,我们可以在extensions/
下新建一个controller/
目录,专门放置 controller extension.2. Configuring in Application Configuration
controllerMap
property 可不是放在components
property 内。配置如下:
到此,转换工作已完成。重新访问
blog/site/login
页面,看上去和之前一样,但是SiteController
已经变成 controller extension.Question
在更改
SiteController
Class 的父类时,原先的声明是:我直接将
Controller
改为ExtControler
(不是CExtController
), 访问site/login
时出现如下错误:使用完整的
CExtController
没问题。这里面的疑惑是:CController
能省去C
prefix, 而CExtController
不能(该问题记录在 https://trello.com/c/5gxL5Q77/32-controller-extends-controller-or-ccontroller 内)?CController
在哪预先载入,从而可以直接使用?The text was updated successfully, but these errors were encountered: