PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jul 23, 2019 1:45 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: View Files in MVC
PostPosted: Sat Mar 10, 2012 5:58 pm 
Offline
Forum Newbie

Joined: Sat Mar 10, 2012 5:38 pm
Posts: 3
Hi Guys, it's my first post here, I know a little about php and programming but this is confusing me a little.
I did do a quick search but there's tons of different stuff on this, it's quite overwhelming.

I've been creating a small app in a MVC structure, however I'm confused on how to format the view files/classes.

Currently a controller gets data from the model, the controller checks this data or whatever, then passes it to the view class for displaying it. I've been told that the view class should have a function that generates the HTML markup for the whole page and concatenates it into a php string, then a function to just echo the string out.

Sorry if this has been covered before, as I'm sure after just re-reading it now, it probably sounds a bit stupid to most people?


Top
 Profile  
 
 Post subject: Re: View Files in MVC
PostPosted: Sat Mar 10, 2012 7:42 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA


Top
 Profile  
 
 Post subject: Re: View Files in MVC
PostPosted: Sun Mar 11, 2012 6:18 am 
Offline
Forum Newbie

Joined: Sat Mar 10, 2012 5:38 pm
Posts: 3
Coming from a front-end development background, I would prefer to have a php file of HTML etc... with embedded PHP functions/loops to display the data passed to it. What are the dis-advantages of doing this, and how would you implement it? eg the controller checks the data passed from the model, then a function in the controller 'requires' the php/html file so that it can use the data?

Thanks


Top
 Profile  
 
 Post subject: Re: View Files in MVC
PostPosted: Sun Mar 11, 2012 2:30 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
You can do that just fine if the framework uses output buffering.
Syntax: [ Download ] [ Hide ]
function executeView() {
        ob_start();
        include($this->view);
        return ob_get_clean();
}

(it could easily be more complicated, but that's the gist of it)

Your view can have anything you want and it looks like a normal PHP script, jumping between HTML/whatever and PHP any time.


Top
 Profile  
 
 Post subject: Re: View Files in MVC
PostPosted: Fri Mar 30, 2012 3:16 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: View Files in MVC
PostPosted: Tue Apr 03, 2012 4:30 am 
Offline
Forum Newbie

Joined: Sat Mar 03, 2012 6:43 am
Posts: 19
Anthony there is many MVC implementations. That is something good because you can choose what best fits in your programming view, your experiences or what a programmer you like to be in the future. I rarely see MVC implementations that have no logic (to me) , so there are many alternative ways. So here is my opinion (and implementation I use) about views. Most often an application has few final views , this can be expressed as template files that uses geter methods of the current controller (u can use inheritance and interfaces to insure that the controllers have the methods defined by the final template).
There is also one more layer in View, the View Generator Classes , that takes an object (or a list of them) and generate html code for them. Controller can use them to set variables that the final template will use with getters methods. As said this just one approach… (there is also a third hybrid layer in this approach but not need to make it more complicated)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group