Tutorial PHP / MVC
The goal of this tutorial is to start from a one-page website and gradually
transform its structure into a model view controller pattern (MVC)
1 Question 1
• Look at the index.php file that is in the stage0 directory.
• Add the corresponding database.
• Run the index.php file by copying it to your WWW or htdocs directory on
your server (WAMP, MAMP, Xampp, etc.).
• What does it do?
• How do you find the code in terms of readability and ease of modification?
2 Question 2: The model
• Find the code snippets in the previous file that allow you to write SQL
queries.
• Copy the database access code into a file called connexion.php and
include it in your index.php.
• Encapsulate this code in c-functions and use function calls.
• Move these functions to a file called functions.php, which you will call in the
index.php file through a request. This file should be in a directory model.
• Test the code
3 Question 3: The views
• Find the code snippets in the previous file that allow you to write HTML
code.
• Move the HTML content into two files: edit.php for adding or modifying a
user and list.php for displaying the list of users. These two files should be in a
directory called views.
• Edit the index file to include these files
• Test the code
4 Question 4: Going even further with MVC
• Examine the code in step3
• Test the code