耶.从模块控制器访问功能 [英] yii. Accessing a function from a modules controller

查看:19
本文介绍了耶.从模块控制器访问功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个模块,在它的默认控制器下有一个名为 renderPageLinks 的函数,它返回一个数组供小部件使用.

I have a module in my app and under its default controller it has a function called renderPageLinks which returns a array for consumption by a widget.

小部件 genMenu 从/themes/jui/views/layouts/main.php 调用(它生成一个菜单)我需要将数据从 renderPageLinks 传递到小部件作为价值:

The widget, genMenu, is called from /themes/jui/views/layouts/main.php (it generates a menu) I need to pass the data from the renderPageLinks to the widget as a value:

$this->widget('pageLinkGen', array('pages' => renderPageLinks()));

问题是 Yii 找不到函数 renderPage Links.

The problem is Yii cant find the function renderPage Links.

我尝试了以下不同的组合以使用..

i tried diffrent combinations of the following with to avail..

$this->widget('pageLinkGen', array('pages' => 'application.module.QuickDial.default.renderPageLinks()'));

有什么建议吗?

附言我试图将 renderPageLinks() 移动到控制器 pageLinkGen 但 Yii 找不到 renderPageLinks() 中使用的模型.

p.s. I have tried to move renderPageLinks() to the controller pageLinkGen but Yii can't find the model used in renderPageLinks().

推荐答案

假设您查询的模型在您的模块内,有 3 种解决方法.

Assuming that the model that you are querying is within your module, there are 3 workarounds.

您可以做的是在 QuickDialModule.php 文件中定义您的 renderPageLinks() 函数,即在 QuickDialModule 类中.然后你可以像这样使用它:

What you can do is define your renderPageLinks() function in the QuickDialModule.php file, i.e inside the QuickDialModule class. Then you can use it like this:

Yii::app()->getModule('QuickDial')->renderPageLinks();

您必须在 QuickDialModule 类中编写此函数:

You have to write this function inside your QuickDialModule class :

Class QuickDialModule extends CWebModule{
   public function init(){
     // ... code ...
   }
   // ... code ... other functions

   public function renderPageLinks(){
       // ... do whatever you were doing inside the function ...
   }
}

编辑:
控制器仅在应用程序收到用户的 url 请求时才由 yii 实例化.

Edit:
Controllers are instantiated by yii only when the application receives url requests from the user.

通过声明您的函数 static,您还有另一种解决方法.但是,您必须将具有该函数的类的php 文件 导入到main.php config 文件中的yii 自动加载数组中.因此,将您的 defaultcontroller renderPageLinks() 函数更改为静态:

You have another work around by declaring your function static. But then you'll have to import the php file that has the class that has the function, into the yii autoloading array in the main.php config file. So change your defaultcontroller renderPageLinks() function to static:

public static function renderPageLinks(){
   // do whatever you were doing
}

自动加载控制器,通过修改protected/config/文件夹内的主要配置main.php:

Autoload the controller, by modifying main configuration main.php inside protected/config/ folder:

// autoloading model and component classes
'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.modules.quickdial.controllers.*' // this line is added
),

然后直接调用你的静态函数:

Then call your static function directly:

$this->widget('pageLinkGen', array('pages' => DefaultController::renderPageLinks()));

当然要让这个静态方法起作用,你必须只有一个带有控制器 DefaultController 的模块,否则你不能导入其他模块的控制器,否则可能会出现名称冲突.

Of course for this static method to work, you must have only one module with controller DefaultController, or you must not import other modules' controllers, in any case name conflicts could arise.

如果你把函数移到主模块中的控制器中(即你提到的 pageLinkGen 控制器),那么你必须将你需要的模型导入到主模块的配置 main.php 中(这样 yii 就可以找到它),自动加载导入数组添加:

If you move the function into a controller in the main module(i.e pageLinkGen controller that you have mentioned), then you'll have to import the model that you need into the main module's config main.php(so that yii can find it), to autoloading import array add :

  // autoloading model and component classes
'import'=>array(
    'application.models.*',
    'application.components.*',
    'application.modules.quickdial.models.*' // this line is added
),

以便您的控制器可以找到模型.

so that your controller can find the model.

这篇关于耶.从模块控制器访问功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆