Need a bit of help using CI to build a Website. |
[eluser]Oliver Ponder[/eluser]
[quote author="juanvillegas" date="1283057591"]I don’t believe that’s the way. At least if you want to stick strictly to MVC modelling. If you google images for “MVC” you’ll find no connectin between models and views, just view<->controller<->model . Justification for this is that models are usually closely related to a database table, and we don’t want the table structure to spread all over the application. As more we can hide the details of the table, the better. For example, in the code you posted that view is only going to work with a table having content as its main field. A better approach would be using Model::FIELD_CONTENT to ask the model for the content column name, and a better one to delegate this to controller. Also, the controller is there to control what information is forwarded to views and how. If we accept views having access to models then we don’t need controllers anymore than for rendering. My approach for this would be setting up some filters system like we do in Rails. For example, all methods requiring a “news” block have a before_filter procedure “instantiate_news”, which somehow (setting session, flashdata or just retrieving news). You can write libraries to simulate filters (coding in the constructor) and using $this->load->library in the controllers construct. [/quote] The problem I run into when doing such strict seperation of the view and model is that I end up repeating myself a LOT in the controller if I'm simply trying to get a modular piece of information into my main template. For example, say my site has 3 things going on. 1. Pages 2. People profiles 3. News items And I want recent news items to show up on pages and also on people profiles, that means I would be repeating myself in both the pages and people controllers, calling the news_model and passing the results to the view. And say you have a bunch of little bits, then it becomes quite annoying to have a controller which was originally intended for Pages calling a bunch of other models. I get your point though about the view becoming dependent on a certain setup of the model, which is what we should be trying to avoid. The filter stuff is going a little over my head though, but I'm getting started with rails, so hopefully I'll understand the correct approach better. EDIT: Sorry by the way, Lord Ve, for taking this to a slight tangent. I think the issue is relevant though to your original question
[eluser]Buso[/eluser]
[quote author="juanvillegas" date="1283057591"][quote author="Oliver Ponder" date="1283054862"]I'm not sure of its 'morally' wrong, but what I sometimes do is call a model from a sub view. So for example, in your main view you call a sub-view "news" (its ok to call views from views right?) Code: <? $this->load->view('sub/news'); ?> Which then iterates through results it obtains from a model your wrote for it Code: //views/sub/news.php I'd appreciate a more experienced coders comment on this approach.[/quote] I don't believe that's the way. At least if you want to stick strictly to MVC modelling. If you google images for "MVC" you'll find no connectin between models and views, just view<->controller<->model . [/quote] Here is a picture with the views related to the models : http://en.wikipedia.org/wiki/Model–View–Controller Quote:Justification for this is that models are usually closely related to a database table, and we don't want the table structure to spread all over the application. As more we can hide the details of the table, the better. For example, in the code you posted that view is only going to work with a table having content as its main field. A better approach would be using Model::FIELD_CONTENT to ask the model for the content column name, and a better one to delegate this to controller.We still need them to authorize and pick the next action to execute (maybe show a view, or redirect), which is their main purpose anyway. Quote:My approach for this would be setting up some filters system like we do in Rails. For example, all methods requiring a "news" block have a before_filter procedure "instantiate_news", which somehow (setting session, flashdata or just retrieving news). You can write libraries to simulate filters (coding in the constructor) and using $this->load->library in the controllers construct. To sum up, each framework and user has his own vision of the MVC pattern, which isn't wrong, and 'strict MVC' doesn't mean 'better'. For example, the data validation is suposed to go in the models, but codeigniter tells us to do it in the controllers, and it works fine for most ppl. Why would it be right to call a helper (which could also call models or not) from a view, and calling a model would be wrong? It's not like we were authenticating the user in the view. We are just fetching some extra data.
[eluser]Lord Ve[/eluser]
To be honest, I was hoping for a simple solution, but thus far, I can only understand the usage of about 1/3 of the above posts. But not something I need worry about right now as I am still learning. I have another question. Why doesn't this work: Code: //Login_Model With: Code: //Main Controller I get this: Quote:Severity: Noticeand Quote:Severity: Warning I'm not even sure I am doing it right to begin with. The data is there, but I can't seem to access it... Can see the values of $query if I use print_r for example...
[eluser]Buso[/eluser]
use result_array() if you need arrays otherwise access the data as object properties: $query->id
[eluser]juanvillegas[/eluser]
Two things regarding this: I have researched a little bit and i guess this is our solution (located in one of the core sections of the guide, shame on us! ) http://ellislab.com/codeigniter/user-gui...views.html It would be as easy as loading the header, passing on some data which would be the news in this case. A before filter is appended to every method needed the header with news, and ready up!. I think the article calls it dinamycally loading views. This is the MVC schema proposed by CI official guide: http://ellislab.com/codeigniter/user-gui...pflow.html But i agree there may be some others where view and models communicate. Regards
[eluser]Lord Ve[/eluser]
I'm already dynamically displaying views. ^_^ I load my view, template, and it calls the views needed and passes variables to them. But no worries. There just isn't a simple way to do what I asked in my original question... |
Welcome Guest, Not a member yet? Register Sign In |