MVC Question
Moderator: General Moderators
MVC Question
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
My question is: Is the page with the arrays or the db data the model?
Thank you
- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: MVC Question
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.
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
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
}- allspiritseve
- DevNet Resident
- Posts: 1174
- Joined: Thu Mar 06, 2008 8:23 am
- Location: Ann Arbor, MI (USA)
Re: MVC Question
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.
Code: Select all
$currents->headlinesQuery()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
It holds all the queries for "currents", it extends a class that contains loops
Re: MVC Question
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: MVC Question
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.
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)
Re: MVC Question
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?
So html in a class is not so bad if that is the purpose of the class?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: MVC Question
Typically in PHP, a View class uses a template class and templates internally ... or generates the output with a library.psurrena wrote:Build a view class in addition to the template? Sounds good.
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.psurrena wrote:So html in a class is not so bad if that is the purpose of the class?
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)
Re: MVC Question
I will try a view class and multiple templates.
Thanks again!
Thanks again!