【问题标题】:Modular Yii Components/Actions模块化 Yii 组件/动作
【发布时间】:2014-05-30 05:23:36
【问题描述】:

我正在使用 Yii 框架,对于我们的网站,我们希望使其更加模块化。这对我们来说意味着我们想要将我们的一些组件的逻辑分离到它们自己的组件/应用文件夹中。

目前我正在做的是用我自己的在每个 Controller 类上扩展 CController。

例如

class SiteController extends MyController

在 MyController 中,我将通过 actions() 方法加载组件:

public function actions() {
    return array(
        'login'=>array(
            'class'=>'application.components.Auth',
        ),
        'logout'=>array(
            'class'=>'application.components.Auth',
        ),
    );
}

在我的 Auth 组件中,我有一个 run() 方法:

public function run() {
//  Get the action string from the URL
    $uri = (Yii::app()->getRequest()->getRequestUri());
    $method = 'action'.ucfirst(substr($uri,strrpos($uri,'/')+1));

//  Run the method from the URI
    $this->$method();
}

它将运行我在 Auth 组件中的操作方法。我还应该提到我的 Auth 组件没有扩展 CWidget,它扩展了 CAction。

在 Auth 内部我有我的方法:actionLogin() 和 actionLogout() 来匹配 MyController 中的上述操作。

当我尝试渲染我希望保留在模块化组件中的视图文件时,它不会识别出组件文件夹中有视图文件,而是在默认视图或主题文件夹中查找。

我已尝试从受保护的根目录加载通行证:'//application/components/auth/login'

注意: 我尝试这样做的总体目标是让我能够创建我的网站的多个模块化部分/应用程序/组件,其中 URL 结构不会改变,并且基于我如何设置将加载的操作方法数组该 URL 的特定组件。我在站点上可能有两种形式的身份验证,或者未来的组件可能是能够根据我放入操作数组中的内容加载每周计划和每月计划组件。

我调查了模块,但发现 URL 不会保持一致。我欢迎对此提出任何建议。谢谢你。

【问题讨论】:

    标签: php yii modularity


    【解决方案1】:

    首先,在您的组件中创建一个列出本地操作处理程序的函数

    public static function actions(){
        return array(
            // name action and point to the location where the action class is
            'GetActivity'       =>'application.components.Auth.login',
            'GetActivitytype'   =>'application.components.Auth.logout',
        );
    }
    

    并按如下方式加载您的视图:-

    return $this->renderPartial('application.components.views.auth_login',
                                 array('model'    => $userModel),
                                 true
                               );
    

    【讨论】:

      猜你喜欢
      • 2011-04-20
      • 2023-03-21
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      相关资源
      最近更新 更多