EllisLab text mark
Advanced Search
     
What’s the best way to structure code when calling multiple controllers into one page view?
Posted: 11 March 2008 02:25 PM
Joined: 2002-03-12
17 posts

I have a comments controller/model/view which displays, adds comments, etc.  I want to display comments into several different sections of the site.  How is the best way of doing this without needing to make a controller to controller call in CI which doesn’t seem possible or without placing the same code into several different controllers.  So if I want to display comments as part of a blog page detail which is in the Blog controller and on a forum post which is in the forum controller.

What is the best way to structure the code so I can display and add the comments into different areas. Is there a good way to do it without using third party additions like HMVC or is it best to just use a third party addition? http://ellislab.com/forums/viewthread/72123/

 
Posted: 11 March 2008 02:58 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2008-01-16
90 posts

pull out the display_comments function from the controllers and instead put it in a custom helper file.  then load that helper into any controller method that needs it.

 
Posted: 11 March 2008 06:34 PM   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2939 posts
delay - 11 March 2008 06:25 PM

Is there a good way to do it without using third party additions like HMVC or is it best to just use a third party addition? http://ellislab.com/forums/viewthread/72123/

If you do find a better way (without breaking MVC), please post your solution, it may help others. wink

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 12 March 2008 05:21 AM   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2006-07-23
148 posts

I did something similar by putting the code in a library.

 Signature 

running man webdevelopment

 
Posted: 12 March 2008 11:33 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2007-10-25
203 posts

The answer to me seems fairly simple. Your comments controller should really only be used to taking input or action from the user and make changes in the model. The model should be your data provider, returning any comments for a given piece of content. Your view should be the one requesting data from the model. Therefore, you would have something like this.

User :: Request Content (id 1) >>
Controller :: Invoke Content View (id 1) >>
View(content) :: Request Content Data from Model(content) (id 1) >>
Model(content) :: Return Content Data to View(content) (id 1) >>
View(content) :: Display Content Data (id 1) >>
View(content) :: Invoke Comments View (id 1) >>
View(comments) :: Request Comment Data from Model(comments) (thread_id 1) >>
Model(comments) :: Return Comment Data to View(comments) (thread_id 1) >>
View(comments) :: Display Comment Data (thread_id 1) >>
:: Completed ::

 
Posted: 12 March 2008 12:36 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-13
514 posts

1 Quick Q?

Would this work:

$this->load->controller('comments'); 

Just and Idea.

 Signature 

Yonti - I am Currently looking for a business partner, to create the best developer hosting out there. If your interested PM me.

Fluxity Lighting - My other company.


I’m a Proud Supporter and Sponser of Tomorrows Web.

 
Posted: 12 March 2008 02:31 PM   [ # 6 ]   [ Rating: 0 ]
Joined: 2002-03-12
17 posts

Thanks everyone for all your comments.  I think at this point I have decided to create a comments library, to access it from various controllers. Helpers would work just as well but It seems others with similar problems use libraries more than helpers for this though.

WackyWebs.net:

$this->load->controller(‘comments’);

I wish that would work,  I would think that would be the easiest way to handle this problem, but CI doesn’t have this.  I am not sure why a controller to controller call would be bad for CI to implement,  I wouldn’t see why this would cause any more problem than loading it from a library from a performance aspect… We can load multiple models already so it seems like loading a controller in a similar way would be nice functionality that could make things more modular.  I am no CI guru so I don’t know what the technical problems would be in implementing it.

Pygon: I thought about doing it like this.  But I have set controllers up as the intermediary between the models and views and am not sure if it would be good to move all my controller logic into the model for the comments.  Still thinking about it though as I could avoid libraries altogether if I just went to MV instead of MVC for this.

Anyway thanks for all the input.  I love the CI forums:-)

 
Posted: 12 March 2008 03:03 PM   [ # 7 ]   [ Rating: 0 ]
Joined: 2007-10-25
203 posts

Honestly, I see no need for you to use a library.

Comment logic (not user logic or other logic as to whether to display it, etc) should be in your model anyway, allowing you to access the model from multiple controllers or views without duplication of code.

It’s not to say that you CAN’T use a library—you can do anything you want. It simply makes more sense for comment CRUD to be part of the model, not the controller.

Just a note: This question is best suited for the Code/Application Development section, since Ignited Code is typically where code solutions are posted/discussed and would recieve more attention if posted there.

 
Posted: 12 March 2008 05:17 PM   [ # 8 ]   [ Rating: 0 ]
Joined: 2002-03-12
17 posts

Yeah Pygon I thought about it some more and think you are right.  I should do it the simplest way, abstracting it another level into a library doesn’t make sense when I can just throw it all in the model and simplify it rather than complicate it with an additional abstraction layer.

Also sorry for posting in the wrong section.  I was reading several posts and hit new post without looking at which category I was in.  I thought about that only after I hit submit on the post:-( Doh…

 
Posted: 15 March 2008 03:40 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2007-12-25
252 posts

Did you guys looked at Modular Extensions?
http://codeigniter.com/wiki/Modular_Extensions_-_HMVC/

All about this issue was studied and many different approaches were taken into account before we get to the actual version of Modular Extensions and what it is capable of. To better understand it, take a look into the threads about modules in CodeIgniter:

I did start to ask about the best way to structure my application here:
http://ellislab.com/forums/viewthread/68847/

And we had other parallel discussions about the same theme:
http://ellislab.com/forums/viewthread/64087/
http://ellislab.com/forums/viewthread/71506/
http://ellislab.com/forums/viewthread/73899/

But the discussion about NESTED MVC started to ignite here:
http://ellislab.com/forums/viewthread/70425/
(I was arguing there why Libraries, Helpers, Plug-ins and Models are not suited for modular needs)

Then born the first version of Modular Extensions:
http://ellislab.com/forums/viewthread/71940/

And soon after, the second version:
http://ellislab.com/forums/viewthread/72123/

And the third version…
http://ellislab.com/forums/viewthread/72580/

Finally, the fourth and last version:
http://ellislab.com/forums/viewthread/73177/

We have some modules examples:
http://ellislab.com/forums/viewthread/73401/

And discussion about what is best:
http://ellislab.com/forums/viewthread/73312/

Modular Extensions is powerful, elegant, well done, very flexible, and solves any problem about modular needs in CI, including a complete HMVC or just for View partials. It was made based in the CI core, so it follows the CI standards, without hacking the core.

With it you can organize your code into separate folders, to write your own plug-and-play modules. But also, it allows you to load how many nested modules you want. The modules are very close to Controllers, so it is the best approach to the HMVC concept.

You can have only one controller for the public part of your application and modularize everything else in your application, if you want. There are many things you can do with it, in many possible ways. It is up to your imagination. Also, new features will come soon.

 Signature 

Oh God… Why didn’t you show me CodeIgniter before?