├── .gitignore
├── .htaccess
├── composer.json
├── view
└── helloView.php
├── model
├── Person.php
└── AbstractModel.php
├── controller
└── HelloController.php
├── index.php
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | vendor
3 |
--------------------------------------------------------------------------------
/.htaccess:
--------------------------------------------------------------------------------
1 | RewriteEngine On
2 | RewriteBase /
3 | RewriteCond %{REQUEST_FILENAME} !-f
4 | RewriteRule ^ index.php [QSA]
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "themainframe/php-mvc-sample",
3 | "authors": [
4 | {
5 | "name": "Damien Walsh",
6 | "email": "me@damow.net"
7 | }
8 | ],
9 | "require": {
10 |
11 | },
12 | "autoload": {
13 | "psr-0": {
14 | "": ["controller/", "model/", "./"]
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/view/helloView.php:
--------------------------------------------------------------------------------
1 |
9 |
10 |
11 | Hello name; ?>
12 |
13 |
14 |
15 | You like everything to be fav_colour; ?> ;)
16 |
17 |
--------------------------------------------------------------------------------
/model/Person.php:
--------------------------------------------------------------------------------
1 |
7 | */
8 | class Person extends AbstractModel
9 | {
10 | /**
11 | * Three columns,
12 | *
13 | * - The ID
14 | * - The person name
15 | * - The person's favourite colour.
16 | */
17 | public static $data = array(
18 | array(
19 | 'id' => 1,
20 | 'name' => 'Johnny Greensmith',
21 | 'fav_colour' => 'Green'
22 | ),
23 |
24 | array(
25 | 'id' => 2,
26 | 'name' => 'Mary Blueton',
27 | 'fav_colour' => 'Blue'
28 | )
29 | );
30 | }
31 |
--------------------------------------------------------------------------------
/controller/HelloController.php:
--------------------------------------------------------------------------------
1 |
7 | */
8 | class HelloController
9 | {
10 | /**
11 | * Say hello to a person.
12 | * $params will be an array of parameters passed by the Front Controller.
13 | *
14 | * For this example, the /hello/ route will pass:
15 | *
16 | * $params[0] - The whole route, E.g. /hello/1
17 | * $params[1] - Just the ID, E.g. 1
18 | *
19 | * @param array $params The parameters from the URL.
20 | */
21 | public function helloAction($params)
22 | {
23 | // Find the requested person in the database
24 | $person = Person::findByColumn('id', $params[1]);
25 |
26 | // Render the helloView view
27 | include 'view/helloView.php';
28 | }
29 | }
30 |
31 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | require_once 'vendor/autoload.php';
12 |
13 | // Define the routes table
14 | $routes = array(
15 | '/\/hello\/(.+)/' => array('HelloController', 'helloAction')
16 | );
17 |
18 | // Decide which route to run
19 | foreach ($routes as $url => $action) {
20 |
21 | // See if the route matches the current request
22 | $matches = preg_match($url, $_SERVER['REQUEST_URI'], $params);
23 |
24 | // If it matches...
25 | if ($matches > 0) {
26 |
27 | // Run this action, passing the parameters.
28 | $controller = new $action[0];
29 | $controller->{$action[1]}($params);
30 |
31 | break;
32 | }
33 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PHP MVC Sample
2 |
3 | This is a little demonstration of how MVC applications work in PHP. I wrote this code during a live demo so it's not 100% and some shortcuts were taken to speed up the development.
4 |
5 | The idea is to give an understanding of how MVC works, not just specifically in PHP, but in general.
6 |
7 | ## Installing
8 |
9 | Installation is super-simple. Grab [Composer](http://getcomposer.org/) and run `composer install` in the Git repository directory.
10 |
11 | ## Explanation
12 |
13 | The idea behind this is that reading, modifying and breaking it will teach a person how to work with MVC in PHP. The comments included in it are also helpful.
14 |
15 | As a 3-bullet-point primer, I like to think of MVC in this way:
16 |
17 | * **The Model** - Concerned with data. Creating, Reading, Updating and Deleting.
18 | * **The View** - Concerned with appearance. How the application looks.
19 | * **The Controller** - Concerned with behaviour. How the application acts and flows.
--------------------------------------------------------------------------------
/model/AbstractModel.php:
--------------------------------------------------------------------------------
1 |
14 | */
15 | abstract class AbstractModel
16 | {
17 | /**
18 | * This method looks through the associated database table
19 | * for a row that has a certain value in a certain column.
20 | *
21 | * For example, a call to:
22 | *
23 | * Person::findByColumn('name', 'Johnny Greensmith');
24 | *
25 | * ...would return a new instance of Person, hydrated with
26 | * Johnny Greensmith's details.
27 | *
28 | * @param string $column The column to search in.
29 | * @param mixed $value The value to search for.
30 | * @return bool|object
31 | */
32 | public static function findByColumn($column, $value)
33 | {
34 | // Find the row in the database
35 | foreach (static::$data as $row) {
36 |
37 | // If this is the row...
38 | if ($row[$column] == $value) {
39 |
40 | // Create a new instance of the actual class
41 | $instance = new static;
42 |
43 | // Hydrate the instance with the row contents
44 | foreach ($row as $key => $value) {
45 | $instance->{$key} = $value;
46 | }
47 |
48 | // Return the new instance
49 | return $instance;
50 | }
51 | }
52 | return false;
53 | }
54 |
55 | /**
56 | * This method persists the current instance to the
57 | * database table it came from.
58 | *
59 | * For example, you could modify Johnny Greensmith's favourite
60 | * colour with the following code:
61 | *
62 | * $jonny = Person::findByColumn('id', 1);
63 | * $johnny->fav_colour = 'Orange';
64 | * $johnny->persist();
65 | *
66 | * This would update the data stored in the Person class.
67 | * Normally, this would generate an SQL query to persist
68 | * a row back to the database table it came from.
69 | */
70 | public function persist()
71 | {
72 | foreach (static::$data as & $row) {
73 | if ($row['id'] == $this->id) {
74 | foreach ($this as $key => $value) {
75 | $row[$key] = $value;
76 | }
77 | }
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------