0

I am about to engage in creating a new web site for my company. I am looking to use PHP, as it is a small business and the total site will only have about 40-50 pages at the end. In the interest of learning and best practice, I would looking to use a PHP MVC framework. I have been trying a few out, and see how it would work out when you are serving dynamic content very nicely. What I am not understanding is how you rectify the MVC logic with static content.

By this I mean, how would the MVC pattern deal with a page that is not dynamic, like for instance, a company information page. Is there a controller for each page, that simply returns a view that contains all the HTML and CSS? Does the controller pull header and footer information from other controllers, and then pull the main content from a different view? I guess I am not understanding how that all works out. Could someone ELI noob?

thatidiotguy
  • 8,701
  • 13
  • 60
  • 105

4 Answers4

1

First, I recomend you Yii Framework,its fasts stable and have a nice documentation of all functions/methods. Other framework options(symfony,cakePhp,etc)are slow and with a learning curve as slow.

Yii manages content with controller->actions. When u connect to yourpage.com/site/index yii loads a SiteController ->actionIndex and renders html code in a view file.

You puts all website content (headers/footers/menus/etc) in page file, and the view file only loads a body view.

Stefan Luv
  • 1,179
  • 3
  • 12
  • 28
  • 1
    you might consider [this list of issues](http://stackoverflow.com/a/10960679/727208) before going with good old *"i use it, so it must be the best"* response. – tereško Sep 12 '12 at 21:41
  • 2
    @tereško I can make a huge list of advantages and disadvantages, but beside the point. Yii is the best choice to begin to understand the MVC learning curve (He has never worked with a framework, therefore other options you will be costly). Beyond that, frameworks like symfony are not made for small projects...are for most demanding development environments. So I did not respond in the style of "i use it, so it must be the best", would simply YII framework is best suited to their learning requirements, speed and performance. Other thing i dont use YII,right now I'm creating my own framework. – Stefan Luv Sep 13 '12 at 03:56
  • Yii is **NOT IMPLEMENTING MVC**. What it does is a mimicry Rails perversion of the design pattern. The framework uses global state for everything and favors configuration over convention. It is so filled with bad development practices that suggesting it as "good for newbies" is actually malevolent (at best). – tereško Sep 13 '12 at 15:03
  • Model View Controller(MVC),no matter the model, if it meets the requirements is a MVC. i recommend you to read first of all thishttp://www.yiiframework.com/doc/guide/1.1/en/basics.mvc Now you're talking about yii like "i dont use it, so it must be the worst". – Stefan Luv Sep 13 '12 at 16:12
  • 1
    FYI, I have quite extensive experience with Yii. But I get the impression, that you never actually studied the deign pattern outside the "material" provided by one framework or another. Please, do some research before you start claiming the knowledge. I would recommend to start with [PoEAA](http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420) by Martin Fowler. – tereško Sep 13 '12 at 17:01
  • You do not show your experience saying "yii is not a MVC". No need to refer to design patterns books when anybody can prove why your statement is wrong with a simple search on Google. Good luck. – Stefan Luv Sep 13 '12 at 17:30
  • Since you sneer on literature about application design (as every framework-expert would), let me explain it in a single sentence. Yii does not implement MVC because Model is layer (not an ActiveRecord ORM), Views are instances that handle presentation logic (not dumb templates) and Controllers only change state of model layer and current view (not deal with access controller or send data from ORM to tempalte). What Yii implements is not MVC but a sad parody. – tereško Sep 13 '12 at 20:24
  • i dont sneer on literature About application design, y seer your method to try to justify ilogics reasoning sending to others to read advanced literature (trying to imply that you handle it and devalue the opinions of others) You should first start with the basics like:http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller. Yii has all those features? YES, then Yii is an MVC framework. Far from trying to stay with the last word and keep trying to devalue my comment, you should understand how google can kill your baseless comments. – Stefan Luv Sep 13 '12 at 21:35
  • Please read the **Component interactions** segment from that wikipedia article you so helpfully linked: controller is supposed to change state of view and model, view is supposed to request information from model and model in web is passive. That is not how Yii works. – tereško Sep 13 '12 at 21:54
  • In Yii controllers change state of view and model(sends model to view renders(view,model)), model is pasive and views takes info for model. What is the difference? – Stefan Luv Sep 13 '12 at 22:04
  • in MVC, Controller responds to events, typically user actions, and invokes requests to model and PROBABLY to the view, is the basics of the basics. You talk about controller like a "strict MVC" (does not exist, of course) – Stefan Luv Sep 13 '12 at 22:17
  • The difference is that in Yii there is not view, just a template. "Controller" in Yii request data from ORM instances, manipulates it and bind it to the template. Then the controller renders the template. *(sorry .. had to edit , because it might been confusing)* – tereško Sep 13 '12 at 22:23
  • @StefanLuv Yii is as much "MVC" as CodeIgniter...that is to say, not at all. MVC is a defined application architecture that was created with Smalltalk decades ago...and Yii barely comes even _close_ to matching it. Not to mention that it's very difficult to implement true MVC in a server-side web app (easier on client-side). Beyond that, the coding practices Yii employs are hideous: lots of shared and global state with tightly coupled classes everywhere and a codebase littered with configuration classes/files everywhere. – Lusitanian Sep 14 '12 at 05:57
  • There are two possibilities, MVC MVC or not. The halftone answers like "I do not like because have many files" does not imply that yii not an MVC. I never said Yii seems the best code in the world, just its easy and intuitive for small projects and even has advantages over others who can not even install on shared hostings(for example). Although it may seem horrible code, speed avg is better than any other MVC framework in PHP...and yes,you can do same things what you do with the other frameworks. – Stefan Luv Sep 14 '12 at 16:17
1

Is there a controller for each page, that simply returns a view that contains all the HTML and CSS?

It is a common practice to have 1:1 relation between views and controllers. When implementing MVC, view would usually represent a page in website (like your user profile in StackOverflow) and it would change according to model layer state (think: you viewing your profile or somebody's else).

A specific controller would be responsible for dealing with all (or most of) the user interaction from that page.

In properly implemented MVC or MVC-inspired design pattern, the controller would only change the state of view (which was provided to it in the constructor). Generation of response (whether HTML page, HTTP header or JSON file) is handles by view itself. Since controller should not be creating the view, there is not point in returning it.

Does the controller pull header and footer information from other controllers, and then pull the main content from a different view?

That would be the behavior if you were using HMVC pattern (which is actually not directoly related to classical MVC).

In proper MVC or MVC-inspired structure, the view would acquire the data from model layer and, based on that information, assemble the HTML page from multiple templates. If you want to learn how to make simple template in PHP, read this article.

I guess I am not understanding how that all works out.

In that case you should start by reading "GUI Architectures" by Martin Fowler. And if you are still new to the whole OOP-thing, watch every lecture, that is listed at the bottom of this post.


The main topic

If you have large amount of static pages, it is possible to use MVC design pattern to create the site (you would basically treat them as cached structures, which view can assable), but it might be a bit pointless.

MVC is a complicated design pattern, which is aimed at bringing order to large applications by applying SoC.

MVC is not a magical sprinkling, that makes it all better, when applied to the forehead.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
  • Don't get me wrong. There will be a good amount of dynamic content. I just did not understand how I was going to fit in the static content as well. I would say it will end up being about 50/50. Thank you for all the article links. I'll work through those today. And, I am not completely green, I have done many large Java applications, and other small PHP projects, just not a PHP project of this scope. – thatidiotguy Sep 13 '12 at 14:15
  • The idea is that you have **some views** that deal with pre-rendered content. The rest are for the dynamic stuff. The static content that you cache/store will be at the level of templates. You this would also let you view instances to combine dynamic content with pre-rendered fragments. – tereško Sep 13 '12 at 14:49
0

The MVC structure is not really the best choice for static content, since all the M, V and C components are in the same document. Therefore, there is no "right way" to do this.

However, there are some workarounds.

One of them is turning the static content into a model (M) and injecting it into the main template (in the case it is a push template). Another is to make the content into a sub-template that extends the main template.

Felds Liscia
  • 339
  • 1
  • 10
0

The controllers job is to populate the model and then send that model to the view to display the data to the user. This pattern of responsibility doesn't change if the data is static instead of dynamic. The controller would just have less 'work' to do with static content.

Forty-Two
  • 7,535
  • 2
  • 37
  • 54
  • So there is one Controller per static page that simply returns the view? – thatidiotguy Sep 12 '12 at 20:58
  • One controller can be responsible for rendering more than one view, can it not? – Forty-Two Sep 12 '12 at 21:01
  • Certainly, so I could have something called Static_Controller, and it is responsible for retrieving static content based on the URL? And how would this be integrated so that content on a web page that is always the same, (header with logo, nav and a footer) are always the same? – thatidiotguy Sep 12 '12 at 21:03
  • @Forty-Two , what you described is not a controller. It was closer to perversion of Presenter from MVP pattern. – tereško Sep 12 '12 at 22:09