I’m practice to use portlet, but when I try to call a controller function, it return nothing. how can I call it?
the code of Portlet
<?php
Yii::import('zii.widgets.CPortlet');
class PortletDemo extends CPortlet {
public $Msg;
public function init() {
$this->hideOnEmpty = true;
$controllerId = Yii::import('application.controllers.PortletDemoController');
if (method_exists($controllerId, 'getMsg')) {
$this->Msg = $controllerId->getMsg();
}
parent::init();
}
protected function renderContent() {
if (isset($this->Msg)) {
$this->render('PortletDemo', array('Msg' => $this->Msg));
}
}
}
?>
the code of controller:
<?php
class PortletDemoController extends Controller {
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout = '//layouts/column2';
public function getMsg() {
$msg = 'This is a msg for test Portlet';
return $msg;
}
}
With $this->controller you get the controller that this widget belongs to…
You just named one file PortletDemoController this does not mean that it is the portlet controller… portlet is a widget with some additional attributes… check the documentation for widgets to get a better understanding - http://www.yiiframework.com/doc/guide/1.1/en/basics.view#widget
Depending on what you want to do in the getMsg() function… you can put it in the SiteController… or even in the same portlet… if this is used so that you can set different messages… maybe you can use an attribute instead of a method…
I’m just do excise to using Yii, and I wanna know the work flow of this framework. I know I can use attribute or other ways to display it, using Model also can do it. Just because I can’t understand how the portlets work, so… thanks for your help, I know why I got siteController now, that’s because I use this widget in sitecontroller, protected->views->layouts->column2.php.
But… why would you call another controller method ?
If a method is in another controller it belongs "there"… if you need some method "here" then put it here…
On the other hand… if you need to have one method that you can call from different controllers (widgets)… then you can put it in the base controller class…
The code generated by Gii creates the Controller class in protected/components/controller.php… .and all application controllers are extended from that class… so if you put there a method… that method can be called from any controller in your app.
that’s what I think, why call a method in other control. I just saw a demo before and want to try use protlet like that, it’s fine, I won’t do that anymore, thanks.