Does Yii have equvalent of HMVC in Codeigniter

I need advice from someone who used develop in CI and then shifted to Yii on whether Yii has an equvalent of HMVC?

Can you explain what it is / what you’d use it for?

Typically you will not need it in Yii since there are widgets.

I think what Eddy is asking for is the ability to make request (and obtain a response) from a controller.

Like what can be done using the curl library from php

Alse, this extension implements that library in yii terms!

PoL

In CI HMVC is a method to call one controller from another w/o additional HTTP request. When I was in CI camp we used it for widget-like functionality.

Oh… I understand now, thanks samdark!!!

Well I’ve never used CI before. But I search that term HMVC in google and what I see make me think that the solution I’m proposing is tee correct one… my bad :(

Nice thread,

thanks for commenting samdark

the difference between widget and HMVC

is reusability

ex : if I have a controller used in ajax , with HMVC i could reuse the same controller to simply push the snippet in a view, just like a renderPartial but with all the logic that comes with the controler.

it’s true this is possible to do with widgets, but if I have my controller doing all the logics and building what I need, for the moment I would have to duplicate the controller into a widget, where with HMVC we could simply use the same controller action. it also quite practical for testing ajax features.

so I’d say widgets are great for anything that isn’t allready in a controller, otherwise we get duplication.

so both are great to have , hope Yii will concider it.

something like renderAction($path) would be super.

@pol : using Curl for this would do the trick but would be a bit too much extra processing

Well Tibor … you are summoning a rather old thread, but otherwise perhaps I wouldn’t find it :)

I am coming from CI too and am using HMVC. As a library (actually not exactly library because it modifies a lot of the core functionality of CI) HMVC was great as one can call it as a widget or directly from the url as separate page. The alternative here in Yii as I see it is the modules and the good news are it is part of the framework and not a fork made by someone else …

http://www.yiiframework.com/doc/guide/1.1/en/basics.module#creating-module

I agree with Tibor Katelbach. If there are some ajax controllers which produces html pieces that should also be added to the page not only via ajax then I prefer HMMC rather than widgets. But I don’t see a problem, some kind of HMVC can be reached with CWebApplication::runController() or I usually do something like this:




Yii::import('app.components.action.ajax.ShoppingCartAction');

$cartAction = new ShoppingCartAction($linkToCurrentController, 'ShoppingCartAction');

$cartAction ->run();



You cannot use kohana3-like remote requests http://techportal.ibuildings.com/2010/02/22/scaling-web-applications-with-hmvc/ but you usually don’t have to

On the subject of the thread - see http://www.yiiframework.com/doc/api/1.1/CWebApplication/#runController-detail and http://www.yiiframework.com/doc/api/1.1/CAction#runWithParams-detail

very interesting @ololo, could you please detail your parameters

is ShoppingCartAction a cOntroler extended class ?

what would $linkToCurrentController be ?

I’ve implemented HMVC for Yii just for fun.

Still don’t think it’s very useful since we have widgets.

(Controller) $this->forward( ‘othercontolleractionhere’) doesn’t do what you need?

Sound like, though, what you’re really wanting is class based actions and controller components/behaviors. You can define those methods and actions that you’d want to repeat as external elements which you then attach to various controllers. Then you’re just writing it once and using it where you need.

Something to consider is that you can extend the core $this->render() method to generate different data as required. Out of the box Yii only generates $content and sends that to the layout, but you could generate any number of data to be displayed in your layout.

Example:


public function render($view,$data=null,$col1view=null,$col1data=null,$return=false)

	{

		if($this->beforeRender($view))

		{

                    if($col1view)

                    {

                        $col1=$this->renderPartial($col1view,$col1data,true);

                    } else {

                        $col1 = null;

                    }

                    $output=$this->renderPartial($view,$data,true);

			if(($layoutFile=$this->getLayoutFile($this->layout))!==false)

				$output=$this->renderFile($layoutFile,array('content'=>$output,'col1'=>$col1),true);


			$this->afterRender($view,$output);


			$output=$this->processOutput($output);


			if($return)

				return $output;

			else

				echo $output;

		}

	}

I kind of got flamed previously when I suggested this approach, but I am using it very successfully in a CMS I am building.

This isn’t HMVC or a widget, it is Yii basic core function to render a page. You can put a custom ‘render’ method in the controller component and modify it on a controller/controller basis.

doodle

got 2 doodle, there’s a better way. Search for clips.

Yes, I have used clips, and clips are a different way, sometimes better.

doodle




Yii::import('app.components.action.ajax.ShoppingCartAction');

$cartAction = new ShoppingCartAction($linkToCurrentController, 'ShoppingCartAction');

$cartAction ->run();



> is ShoppingCartAction a cOntroler extended class ?

  • ShoppingCartAction is a usual action class which is attached to your AJAX controller with CController::actions() method



class ShoppingCartAction extends CAction {

    

    public function run() {

        // some controller logic



> what would $linkToCurrentController be ?

  • it depends on where you put this line of code. if it’s in a controller then replace $linkToCurrentController with $this, if it’s in a widget then you should use $this->controller instead (action calls from widgets sounds strange, but you can do this)

There is no troubles of using HMVC in Yii to my mind. HMVC is just action calls from other actions and Yii already have action classes (which can be used in other controllers/views), runController() and forward() methods. So you can implement HMVC in different ways, but you can also use widgets

I know this is an old thread, but I think this is an interesting discussion. This is currently the only thing in Yii I’m struggling with. The functionality I want to implement is somewhere between a widget and a module. HMVC look likes it’s what I need, but I’m not sure.

I’ll give you an example: during my last project I had to create a page where users could select holiday accommodations. On the left there was a sidebar with filters (faceted search) and users could add their selections to a list (like shopping cart).

Because this functionality had to be re-used for different users in different places, I implemented it as a widget, but this didn’t feel right, because controllers didn’t work and I didn’t need the assets manager.

But implementing it as a module wasn’t right either, cause I needed to include it within other modules, each having different url rules and layouts.

So when I read about HMVC, it seemed like it could handle the job better then the widget implementation. I could have a seperate module, and use that as some sort of child-module for the current one. Or am I missing the point here?

Did you look into using external actions?

http://www.yiiframework.com/wiki/170/actions-code-reuse-with-caction/

Nope, I’m gonna have a look… thanks