Welcome Guest, Not a member yet? Register   Sign In
I need a little help with planning my application... (noob alert!)
#1

[eluser]littlejim84[/eluser]
Hello again. No doubt this question has been asked many times on here, but I'm wanting to get a personal response for me to truely understand what I need to know.

So... M-V-C... Model, View, Controller - right? So...

'View':
This is purely for actually showing the page right? I should be doing my god damn best to keep this as free from PHP as possible... So keep any logic, sums, working stuff out, out of this View section? I'm presuming that the only things that are PHP on this page, should be the variables that hold data and maybe foreach loops to cycle through rows of data... Right? ...Is it normal practice to keep Views without top and bottom templates and just load the template views around loading the actual page content? How do people here deal with templates... I usually have a top part (logo, header, nav menu) and a bottom part (footer, copyright info), so what is the best way to have this in a view? Just have includes inside the view itself or call the template views by loading them in the controller?

'Controller':
This holds all the logic right? So calling stuff from a database, working out what goes where, loading views etc... Anything else I should know here?

'Model':
Now this one is where I'm a little confused... What exactly should the model bit hold? Database stuff? ... This, with the Controller, seems to overlap...


I've started to get into CodeIgniter as I need a more standard way of working. I need to not worry about the little things and just get on with writing web apps for people, and not worry if my database calls are secure and solid, etc. etc. I really really want to know how to use CodeIgniter to it's full - so I need to make sure I'm using it correctly right from the off....

Sorry for the noob questions Smile
#2

[eluser]parrots[/eluser]
Views -- I personally don't mind some logic in views (like "if logged in show this, else don't show"), but yes you should try your best to keep most of the logic in the controller, where it makes sense. Basically I try to never get data within a view from a model, you should use the controller to get all the data for the page but then the page can manipulate the data passed in as needed with logic.

For templates I've been using http://codeigniter.com/wiki/layout_library/. Makes having a common page layout pretty easy and having the individual views only have to worry about the content area.

Models/Controllers -- Yes the models have the DB stuff, but they don't tend to overlap with controllers. For example you might have a getByUserName method on an account model: that function would contain the SQL to look up a user given the passed username. This method would be called by the controller. The idea is that you shouldn't call the database directly within a controller, you should instead call a model that contains the necessary code to query/insert/update/delete.
#3

[eluser]littlejim84[/eluser]
Thank you for your reply (and your patience with my noob-ness)

So, could you explain to me how this is meant to flow then... For example, i want a simple site where on the index page a database will look for the newest news story and then display it on the page. So in CodeIgniter, I have to start off in the controller (for example: Welcome) and inside this controller I then load a model (called: FindNews?) which runs a query to find the newest news item in the database, then I pass that back to the controller and then get the controller to spit out the view??

Everything starts with the controller right? The controller is the thing that starts things off right... But I was reading on Wikipedia about MVC and it says that the view calls the model? But doesn't mean that the view will now have loads of model code in it???

I suppose I'm just confused on how to set out my app correctly in CI. Especially as I need a background admin area for my client... This is going to lead to subfolders in my controllers and views etc... Could get messy?!
#4

[eluser]Popcorn[/eluser]
littlejim84 : There are different ways of implementing MVC and personally I use the same method described by parrots.

I start with the controller as the hub of the page. It calls the model, it displays the view and handles the logic of the code. It's probably the easiest way to learn it. If you need to try a different method for some reason or another I would do so later after you have a foundation of the MVC framework.
#5

[eluser]littlejim84[/eluser]
Cheers Popcorn, that does sounds like thats what I should do.

But it's the model I suppose I'm having the real trouble with... So I've called my Welcome controller, but in what sense dose the model exactly do here... So I have a function in the model which says "Right, go into the database, get me the latest news article and return the article to the controller" - is that right? So my controller basically calls the model function to get the news article and that gets returned to the controller... The controller then outputs the view, with the article data passed, and the view actually displays it?

Please be super anal with me here... If this is wrong, please say! I know I'm being a little annoying here, but once I've grasped the concept, then that's it then - it's in my head!
#6

[eluser]zdknudsen[/eluser]
I'd rather not be "super anal" with you, but I can say that the MVC pattern is pretty open for interpretation. However, I believe most people go about it like you describe (including me):

The controller contains all the business logic, and calls a given model for data retrieval. The controller then loads a view to which it also sends the data it got from the model.

Oh, and the model doesn't necessarily need to fetch the data from a database. If, for instance, you were storing everything in a text file, the model would read from that instead. And that's the beaty of it. That you wouldn't need to touch the controller in order to change where you stored your data. Then again, that's how I see things - I know there's a lot of opinions on how MVC should be implemented.
#7

[eluser]Popcorn[/eluser]
Yes, that's exactly my workflow. I am still not sure if it's the correct way of doing things, but so far everything is working out for me so I would suggest you do the same.
#8

[eluser]parrots[/eluser]
In your example, yes it'd come into the Welcome controller - probably the index function, since that is the default. For the model I tend to name (and think of) my models as logical objects of data (that's just me, other ways to do this), so I'd probably name the model "News" and have a "getLatest" function. Once the model hits the db and gets the result it would pass it back to the controller, which would in tern pass it into the view. Then at the very end of the index function in the controller it would call the view.

The view will use the data provided by the model but it shouldn't directly call models. The idea is that the controller contains all the business logic and the view simply displays data that has already been generated.

You're on the right path it seems, it'll make more sense once you start to code with it.
#9

[eluser]John_Betong[/eluser]
Hi LittleJim84,

>>> But it’s the model I suppose I’m having the real trouble with…

Somebody a long time ago on this forum, reckoned a Model could or should be considered as an external source that you cannot access. Instead you should send requests to the Model, the Model then does all the work and sends information back to you in the format that you expected.
 

Here are a couple of methods in my Jokes Controller:
Quote: //===========================================
function first() {
$data = $this->m_lib->m_first(NULL);

$this->j_view($data);
}//endfunc



//===========================================
function last() {
$data = $this->m_lib->m_last(NULL);
$data['joke_last'] = $_SERVER['SERVER_NAME'] .' - Joke of the day';

$this->j_view($data);
}//endfunc


//===========================================
function random($idx='', $title='default') {

// ensure parameters are correct order
if ((is_numeric($idx)) OR (is_numeric($title))) {
if (is_numeric($title)) {
$idx = $title;
}
$data = $this->m_lib->m_getjoke(NULL, $idx);

}else{
$data = $this->m_lib->m_random(NULL);
}

$this->j_view($data) ;
}//endfunc
 
Using the MVC methodology I find is good for separating code into logical sections. Virtually it is a way of documenting your code as it is being written.
 
 
 
#10

[eluser]Moiz[/eluser]
Dont know if theis will help, but in a nutshell, A Model is diff from a controller coz you cannot call a function of the controller on your view, however you can do so from the model.

each function of the controller can be used to carry out certain tasks and then load a page. a call to a view can be made from a function of a controller. However if you need some data to be dispalyed on your page withen its execution, then you rather use the model.

you can call $this->ModelName->display_user_data() on the view however this will not work - $this->ControllerName->display_data()

Im new to CI too.. so if i'm wrong, do let me know




Theme © iAndrew 2016 - Forum software by © MyBB