Write your own MVC with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Write your own MVC with PHP

Post by lauthiamkok »

this is the best MVC tutorial I came across so far,
http://php-html.net/tutorials/model-vie ... er-in-php/

I am new to MVC and I am still confused with the class Model and the class book in the example of that tutorial...

for instance, I have my website and the content of my webiste is pulled from a database... so when I request a specific page for instance, http://www.mysite.com/index.php?pg=profile or with clean url http://www.mysite.com/profile

so usually, I will get the content from the SQL query,

$sql = "
SELECT * FROM root_pages
WHERE root_pages.pg_url = '".$_REQUEST['pg']."'
";

so then should this $sql be put in the book class or model class??

or this should be in the controller class actually? :?: :?:

I have a directory folder which keeps all my CMS files, which contains update, insert, delete SQL queries, html forms, and pages that display the db items into lists, etc.

so if I implement MVC on my website, then I am going to relocate these CMS files into different locations, like some in the model folder, and some in view folder and others in controller folder, is it correct??

many thanks,
Lau
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Write your own MVC with PHP

Post by markusn00b »

Anything related to data manipulation (retrieval, creation, etc.) should be in the appropriate model. Models handle data. Remember that.

Edit: BTW, I recommend picking up one of the MVC frameworks and understanding the MVC design pattern through using them.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Write your own MVC with PHP

Post by Christopher »

lauthiamkok wrote:so usually, I will get the content from the SQL query,

$sql = "
SELECT * FROM root_pages
WHERE root_pages.pg_url = '".$_REQUEST['pg']."'
";

so then should this $sql be put in the book class or model class??
Neither because it has nothing to do with Books and "Model" is a bad name for anything that is not a base class for acutal model classes (like Books) to use. You model class should be called whatever it represents. It sounds like Content or Pages or something may make sense. Also, you should pass in $_REQUEST['pg'] as a parameter to the model for it to use internally.
lauthiamkok wrote:or this should be in the controller class actually? :?: :?:
No, the controller is only for program flow and request processing.
lauthiamkok wrote:I have a directory folder which keeps all my CMS files, which contains update, insert, delete SQL queries, html forms, and pages that display the db items into lists, etc.

so if I implement MVC on my website, then I am going to relocate these CMS files into different locations, like some in the model folder, and some in view folder and others in controller folder, is it correct??
Yes. Typically there are controllers/, models, and views/ directories. Templates can go in the views/ directory or in their own directory.
(#10850)
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: Write your own MVC with PHP

Post by lauthiamkok »

thanks Christopher! I have a better idea now then :mrgreen:

this is another tutorial and I think it is pretty good too, only that Function split() is deprecated but it is still being used by this 'tutor'! 8O

http://johnsquibb.com/tutorials/mvc-fra ... r-part-two

this one is good too accept I cannot get the code running on my localhost :banghead:

http://www.henriquebarroso.com/how-to-c ... rk-in-php/

but what counts the most is this simple illustration of MVC by this coder,
Model: Responsible for the data management like SQL databases and SELECT, UPDATE, DELETE,etc…

View: Shows the UI, html, css, javascript and so on

Controller: The brain and heart of MVC architecture, this is were all comes in, the controller is the middle man between the view and the model.
thanks again :D
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Write your own MVC with PHP

Post by Christopher »

lauthiamkok wrote:but what counts the most is this simple illustration of MVC by this coder,
Model: Responsible for the data management like SQL databases and SELECT, UPDATE, DELETE,etc…

View: Shows the UI, html, css, javascript and so on

Controller: The brain and heart of MVC architecture, this is were all comes in, the controller is the middle man between the view and the model.
This definition will lead you astray as you do more with MVC. There are a couple of important things you should know.

First, the Model is in a separate layer from the View and Controller. The Model is in what I usually call the Domain Layer. In MVC this is equivalent to the Application and Data Tiers in 3-Tier/N-Tier architecture. It is not just persistent data, but also has all the "business logic" that defines the application. The View and Controller are in the Presentation Layer. One way to think about the separation between them is that the Controller mainly deals with Request processing, while the View mainly deals with Response processing.

Remember that because the Model is in a separate layer -- it should not have any dependencies on any View or Controller code.

Also, in PHP there will usually be a Database Access/Connection/Adapter Object that is used by the Model to connect to a datasource. For databases that object is often PDO, something like it.
(#10850)
User avatar
Zyxist
Forum Contributor
Posts: 104
Joined: Sun Jan 14, 2007 10:44 am
Location: Cracow, Poland

Re: Write your own MVC with PHP

Post by Zyxist »

This article does not describe a "true" MVC, but a popular modification based on frameworks. However, it is not a "true" MVC and has quite different properties.
The view (presentation) is responsible to display the data provided by the model in a specific format. It has a similar usage with the template modules present in some popular web applications, like wordpress, joomla, …
View is not created by templates only, but also by the presentation logic (pagination management, etc.)
The controller handles the model and view layers to work together. The controller receives a request from the client, invoke the model to perform the requested operations and send the data to the View. The view format the data to be presented to the user, in a web application as an html output.
This is the primary misunderstanding of MVC. If anyone takes a look at the original definition of this pattern, he can see that the controller does not serve as a proxy between model and view. It only selects them and communicates one with another, then the view retrieves all the necessary data from models directly, using well-defined, generic interfaces. It may seem silly, but this simple issue changes the properties dramatically. The original MVC leads to the development of bettwen interfaces, reduces the dependencies between the three layers and makes the entire application more flexible. In so-called "web framework MVC", most of the code, often including the business and presentation-specific logic, is moved to controllers, and most of the "flexibility" and speed is achieved thanks to code generators, not the design pattern itself. Actually, many web frameworks make things even worse, reducing models to ORM, which makes almost impossible to use other-than-database data sources in a convenient way, and breaks one of the MVC rules.

PS. I have only one point: if anyone finds the "modified pseudo-MVC" convenient, that's all right. But he shouldn't write it is MVC. Design patterns were "invented" to define an unambiguous terminology for useful design techniques, so that anyone who sees i.e. "pattern XYZ", could understand it precisely. For me, calling an MVC modification simply "MVC" is like throwing away observation from Observer pattern and claiming it is still Observer.
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: Write your own MVC with PHP

Post by lauthiamkok »

thanks again Christ! :D
Post Reply