call the controller member not working

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;

    }


}



the view code:




<?php

    echo $Msg;

?>



the view just echo the $msg, but nothing output.

the archive of files:




protected

    |--components

    |       |--views

    |       |    |--PortletDemo.php

    |       |--PortletDemo.php

    |

    |--controllers

    |       |--PortletDemoController.php



To get the controller that this widget belongs to you can use it’s attribute “controller” - http://www.yiiframew…ntroller-detail

so the call would be


$this->controller->getMsg();

Hi mdomba:

it works now , thanks for your help

Hi mdomba:

I got mistake before, when I change to &quot;&#036;this-&gt;controller-&gt;getMsg();&quot; there is an error like this:



SiteController and its behaviors do not have a method or closure named "getMsg". 



before it works just because I create a function menber for siteController to escape this error.

I thought that when calling "$this->controller->getMsg();", the controller is "siteController", not "PortletDemoController", right?

I think what I should do is to call PortletDemoController->getMsg(), but I don’t know how to retrive a controller in component.

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…

thanks mdomba.

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.




<?php $this->beginContent('//layouts/main'); ?>

<div class="container">

	<div class="span-19">

		<div id="content">

			<?php echo $content; ?>

		</div><!-- content -->

	</div>

	<div class="span-5 last">

		<div id="sidebar">

		<?php

			$this->widget('PortletDemo');

		?>

		</div><!-- sidebar -->

	</div>

</div>

<?php $this->endContent(); ?>



and still this question, can I call the member of other controllers?

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.

thanks mdomba, so kind you.

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.