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?

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.
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.

14 Comments

you might consider this list of issues before going with good old "i use it, so it must be the best" response.
@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.
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).
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".
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 by Martin Fowler.
|
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.

Comments

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.

4 Comments

So there is one Controller per static page that simply returns the view?
One controller can be responsible for rendering more than one view, can it not?
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?
@Forty-Two , what you described is not a controller. It was closer to perversion of Presenter from MVP pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.