Page 1 of 1
MVC Question
Posted: Thu Oct 09, 2008 11:30 am
by psurrena
I am building where index.php gets the appropriate page file based on the URL (?page=work-default), loads a parser class and assigns a template to that class. The page file (from the URL) contains arrays that are matched up with tags in a template file which gets parsed. The file with the arrays usually gets data from the db.
My question is: Is the page with the arrays or the db data the model?
Thank you
Re: MVC Question
Posted: Thu Oct 09, 2008 12:11 pm
by allspiritseve
Hi psurrena,
The page with the arrays contains some controller logic, some model logic (which contains the data from the db), and (to a certain extent) some view logic. Which isn't to say it's invalid, but if you are set on using MVC, you may want to separate them a bit. If you post your page with the arrays, we can help you tease out what goes where.
Re: MVC Question
Posted: Thu Oct 09, 2008 12:17 pm
by psurrena
What I am calling the model page would contain information like this:
Code: Select all
require_once('./library/class/currents.class.php');
$currents=new Currents();
$currents->dbConnect();
//Template
$template="default.template.php";
//Content
foreach($currents->headlinesQuery() as $value){
$tags['content'].= "<p>{$currents->formatDate($value['headlines_date'])}<br />\n";
$tags['content'].= "<strong><a href=\"article-{$currents->dashReplace($value['headlines_title'])}-{$value['headlines_id']}.html\">{$value['headlines_title']}</a></strong><br />\n";
...more of the same
}
Re: MVC Question
Posted: Thu Oct 09, 2008 3:56 pm
by allspiritseve
The only thing that could be considered in the model layer is the array this returns:
I would suggest first of all, moving all of that HTML code into your template file. It's ok to use php foreach loops in your templates... in fact, I encourage you to do so rather than using a tag-replacement template engine, which it appears you are.
Also-- what does the Currents class do? It looks like it has several responsibilities. I would suggest splitting those into several different classes.
Re: MVC Question
Posted: Thu Oct 09, 2008 4:50 pm
by psurrena
It holds all the queries for "currents", it extends a class that contains loops
Re: MVC Question
Posted: Thu Oct 09, 2008 4:53 pm
by psurrena
In theory, there would be no html code but realistically, a little bit of html in the model saves me from creating a template for each and every section.
Re: MVC Question
Posted: Thu Oct 09, 2008 5:12 pm
by Christopher
You design seems fine. You have a clear Model class, which is the most important thing. You have combined the Controller and View, but so what.

That separation is mainly for reuse and orginazation. You don't look like you need either. You can always move the HTML building code out to an external file/class and load it like you do the Model -- if you want MVC.
And remember, you don't even need to to the same thing for every Controller. Some could combine the View with the Controller; others could load a separate View class.
Re: MVC Question
Posted: Thu Oct 09, 2008 8:44 pm
by psurrena
Build a view class in addition to the template? Sounds good.
So html in a class is not so bad if that is the purpose of the class?
Re: MVC Question
Posted: Thu Oct 09, 2008 10:40 pm
by Christopher
psurrena wrote:Build a view class in addition to the template? Sounds good.
Typically in PHP, a View class uses a template class and templates internally ... or generates the output with a library.
psurrena wrote:So html in a class is not so bad if that is the purpose of the class?
I think of a View class as containing presentation logic, but not the actual output (if possible). The actual HTML would be in a template or generated.
This separation is mainly for maintenance sanity. Keeping the templating PHP separate from the application logic PHP has become a best practice because it allows you to clearly see the output in one place, and the program flow in another. Mixing the two makes it difficult to see what either is doing -- hence is harder to maintain.
Re: MVC Question
Posted: Fri Oct 10, 2008 8:54 am
by psurrena
I will try a view class and multiple templates.
Thanks again!