MVC Question

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

MVC Question

Post 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
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: MVC Question

Post 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.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: MVC Question

Post 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
}
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: MVC Question

Post by allspiritseve »

The only thing that could be considered in the model layer is the array this returns:

Code: Select all

$currents->headlinesQuery()
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.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: MVC Question

Post by psurrena »

It holds all the queries for "currents", it extends a class that contains loops
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: MVC Question

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MVC Question

Post 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.
(#10850)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: MVC Question

Post 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?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MVC Question

Post 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.
(#10850)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: MVC Question

Post by psurrena »

I will try a view class and multiple templates.

Thanks again!
Post Reply