Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating my first controller extension [CController or Controller] #4

Open
drodata opened this issue Jul 5, 2015 · 0 comments
Open

Comments

@drodata
Copy link
Owner

drodata commented Jul 5, 2015

之前熟悉的 Controller 大多通过 Gii 来自动生成,假设创建一个 XyzController Class, 外加一个 index action, Gii 生成的文件结构如下:

protected/
    controllers/
        XyzController.php
    views/
        xyz/
            index.php

假如想在另一个项目中直接使用这个 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 有一个直观的印象。

传统形式:

protected/
    controllers/
        SiteController.php
    views/
        site/
            login.php
            logout.php

controller extension 形式:

protected/
    extensions/
    site/
        SiteController.php
        views/
            login.php
            logout.php

转换的过程实际上就是移动代码的位置,代码几乎不需要做任何改动,除了一点以外:需要将 SiteController Class 的父类由 CController 改为 CExtController.CExtControllerCController 的子类.

A controller distributed as an extension should extend from CExtController, instead of CController.

extensions 下放了很多插件,为了让结构清晰,我们可以在 extensions/ 下新建一个 controller/ 目录,专门放置 controller extension.

2. Configuring in Application Configuration

In order to use a controller extension, we need to configure the CWebApplication::controllerMap property in the application configuration:

return array(
    'controllerMap'=>array(
        'xyz'=>array(
            'class'=>'ext.xyz.XyzClass',
            'property1'=>'value1',
            'property2'=>'value2',
        ),
        // other controllers
    ),
);

⚠️ controllerMap property 可不是放在 components property 内。

配置如下:

// config/main.php
return array(
    ...
    'controllerMap' => array(
        'site' => array(
            'class' => 'ext.controller.site.SiteController',
        ),
    ),
    ...
    'components' => array(),
);

到此,转换工作已完成。重新访问 blog/site/login 页面,看上去和之前一样,但是 SiteController 已经变成 controller extension.

Question

在更改 SiteController Class 的父类时,原先的声明是:

class SiteController extends Controller {}

我直接将 Controller 改为 ExtControler (不是 CExtController), 访问 site/login 时出现如下错误:

Error 500
include(ExtController.php): failed to open stream: No such file or directory

使用完整的 CExtController 没问题。这里面的疑惑是:

@drodata drodata changed the title Creating my first controller extension Creating my first controller extension [CController or Controller] Jul 7, 2015
@drodata 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 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 drodata changed the title Creating my first controller extension [CController or Controller] ❓ Creating my first controller extension [CController or Controller] Jul 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant