tags:

views:

469

answers:

3

As far as best practices go is this recommended? I have a comments controller + model that needs to be called in an items and profiles controller. The comments controller automatically loads the comments model.

Is it acceptable to call the comments controller directly from the items and profile controller, or is the "best practice" way to call the comments model instead?

I ask because in kohana, the view isn't a singleton class so if I were to call a controller within another controller I end up with two views. On the other hand, if I were to just call the model, there would be duplicate code within the items and profiles controller.

All you MVC experts help! =)

+1  A: 

I'd say it depends on what your controller returns.

If your controller can return DTOs/Models, then sure, calling a controller is a good idea.

Your controller defines a strict, documented, and hopefully stable entry points to your functionality. Therefore, you can completly ignore the implementation of Comments, or change it at will, so long as you dont change the signature of the Controller method.

However, if your Controller is unable to return raw data, then it's definitely prolematic, because you're getting html (or other markup) in return. That means that you'll have to pipe that html directly into the view you want, or extract the data from it. Both of those is a bad idea.

Piping the raw html implies that if you change the view for Comments, the display of comments on the Profile module will inevitably change. this might be what you want, but I'd go for flexibility.

Now, if you wanted data, you'll have to extract the data from the markup your view returned, which is heavy and counter productive.

Leprechaun
this is the exact problem i am having, the controller returns raw data. what would you recommend based on this fact?
jusunlee
+2  A: 

Generally, I'd go for the "Fat Model" approach.

I'm not sure what code you're really worried about duplicating.

There are a couple ways you could do this:

First way: - Interrogate your Comments model to return some comments. - Pass the comment data into your view. - Render the comments in the view, possibly using some view helper

Second Way: - Realize that there's no reason your view can't talk directly to your model. - Write a view helper that grabs the data it needs directly from the model, and renders it.

I prefer the second way. Some people have a problem letting their view layer talk to the model (in a read-only fashion!), but I'm not one of them.

timdev
So in your first way, would you recommend bypassing the comment controller and have the item/profile controller load the comment model?
jusunlee
Yeah, exactly. Why would you involve the comments controller? Just have the profile controller use whatever models it needs. Controllers are there to handle user interaction in a sane way. They're not the data police, if you see what I mean.
timdev
+2  A: 

If you use Kohana 3, you can benefit from the HMVC feature. You can execute controllers within controllers.

http://forum.kohanaphp.com/comments.php?DiscussionID=2768

Check the above link for some examples

Sid Vel
They just redid their forum as I was using this link reference... was very helpful to me...http://forum.kohanaframework.org/discussion/2768/
Serhiy