What's the best way to structure code when calling multiple controllers into one page view? |
[eluser]delay[/eluser]
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/
[eluser]dtrenz[/eluser]
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.
[eluser]wiredesignz[/eluser]
[quote author="delay" date="1205274343"]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/[/quote] If you do find a better way (without breaking MVC), please post your solution, it may help others. ![]()
[eluser]frenzal[/eluser]
I did something similar by putting the code in a library.
[eluser]Pygon[/eluser]
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 ::
[eluser]Tom Glover[/eluser]
1 Quick Q? Would this work: Code: $this->load->controller('comments'); Just and Idea.
[eluser]delay[/eluser]
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: Quote:$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:-)
[eluser]Pygon[/eluser]
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.
[eluser]delay[/eluser]
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...
[eluser]Edemilson Lima[/eluser]
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. |
Welcome Guest, Not a member yet? Register Sign In |