Architectural question


i am stuck with an architectural question.

I have a db-table containing text for example. This text needs to be displayed in form of a widget inside a page.

But its not only text, but some divs around it, depending on the user being logged in or if he has the right to view/edit the text.

So this logic could be written in the widget.

But i also have to do the same logic in a controller, e.g. when the text is changed with a ajax-call.

Having this i would end up with having the same logic code in the widget and the controller, which is not DRY.

How can this be done? Do i have to use a portlet, or a component? I think i am missing something in the framework…

Any thoughts are welcome. Thanks in advance.

You can try to move all logic in the widget.

Take a look to this discussion

I am replying here, because i don’t want to hijack the other thread.

When i move all logic in the widget, how would i access this logic on a ajax call.

The ajax call must be handled by a controller, as far as i know. Is it possible to call a function (the render function) of a widget, from inside a controller action?

Yes, is possible.

Usually for ajax I create a view with written something like that:

<div id="ajaxRefreshed">

   <?php $this->render('_ajaxRefreshedSubview')?>


Instead of this render, you can instantiate your widget.

I usually use a subview because I use the same action for ordinary and ajax request:

if (Yii::app()->request->isAjaxRequest)