yii. Accessing a function from a modules controller

北城余情 提交于 2019-12-13 19:12:48

问题


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.

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()));

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()'));

Any suggestions?

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


回答1:


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

One

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();

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 ...
   }
}

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

Two

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
}

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()));

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.

Three

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.




回答2:


If the controller has the renderPageLinks function, and assuming $this is a reference to the correct controller, try calling the function by referencing the controller directly:

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

As far as I know PHP does not support implicit $this




回答3:


bool.dev's suggetion was great but still didn't work in my instance, I believe this was due to disorganization on my end. I first moved the models to /protected/models/ and then created a CPortlet object that included the page rendering and call. I based this on the documentation found here in the blog demonstration. I did not split the view form the controller in this instance because currently it is only calling the Bootstrap BootNavbar widget. Future themes may use a separate view page.

Thanks again for all of your help.




回答4:


I think you should call YourModule::renderPageLinks() method first and just pass returned results:

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


来源:https://stackoverflow.com/questions/9590046/yii-accessing-a-function-from-a-modules-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!