├── README.md
├── assets
└── css
│ └── style.css
├── config
├── baseConstants.php
├── constants.php
└── db.php
├── controllers
├── employeeController.php
└── hobbieController.php
├── index.php
├── models
├── employeeModel.php
├── helper
│ └── dbConnection.php
└── hobbieModel.php
├── resources
└── db.sql
└── views
├── employee
├── employee.php
└── employeeDashboard.php
├── error
└── error.php
├── hobbie
├── hobbie.php
└── hobbieDashboard.php
└── main
└── main.php
/README.md:
--------------------------------------------------------------------------------
1 | `#patterns` `#mvc` `#php` `#master-in-software-engineering`
2 |
3 | # Assembler School: PHP MVC Pattern Basics - Workshop
4 |
5 | Project using MVC Pattern without OOP. In this first approach, you will understand how the pattern works and implement it in a project created from scratch.
6 |
7 | ## Table of Contents
8 |
9 | - [Getting Started](#getting-started)
10 | - [Dependencies](#dependencies)
11 | - [Contents](#contents)
12 | - [1. Prepare the constants](#1-prepare-the-constants)
13 | - [2. Application entry point](#2-application-entry-point)
14 | - [3. Main Views](#3-main-views)
15 | - [4. Controllers](#4-controllers)
16 | - [5. Models](#5-models)
17 | - [6. Views](#6-views)
18 | - [Resources](#resources)
19 |
20 | ## Getting Started
21 |
22 | First of all, you will need to clone this repo:
23 |
24 | ```bash
25 | $ git clone https://github.com/assembler-school/repository-name
26 | ```
27 |
28 | ## Dependencies
29 |
30 | - [Bootstrap](https://getbootstrap.com/)
31 |
32 | ## Contents
33 |
34 | When you're creating a MVC project from scratch it's very important to follow correctly the creation steps in order to understand the execution flow. That is why in this workshop we have detailed the steps to follow for a correct operation of the application:
35 |
36 | ## 1. Prepare the constants
37 |
38 | > In order to call dynamically all the CSS, JS and PHP files, we must configure our project constants that will be accessed during the execution of the application.
39 | > These files will be located in `./config/`
40 |
41 | ### 1.1. `./config/baseConstants.php`
42 |
43 | ```php
44 | FOR REFERENCE FILES
49 | define("BASE_PATH", $documentRoot);
50 |
51 | //BASE URL -> FOR LINK CSS
52 | $uri = $_SERVER['REQUEST_URI'];
53 |
54 | if (isset($uri) && $uri !== null) {
55 | $uri = substr($uri, 1);
56 | $uri = explode('/', $uri);
57 | $uri = "http://$_SERVER[HTTP_HOST]" . "/" . $uri[0];
58 | } else {
59 | $uri = null;
60 | }
61 |
62 | define("BASE_URL", $uri);
63 | ```
64 |
65 | ### 1.2. `./config/constants.php`
66 |
67 | ```php
68 | All the user requests must be done in the entry point, we will use query params in order to indicate which controller and function must be executed.
102 |
103 | ### 2.1. `./index.php`
104 |
105 | First of all, we need to require once the previous created constants files.
106 |
107 | ```php
108 | As you can see in the previous step, we had two default views, now we're gonna create them.
135 |
136 | ### 3.1. `./views/main/main.php`
137 |
138 | This is the main view that is loaded only if the user doesn't specify any controller or it doesn't exists.
139 |
140 | ```html
141 |
142 |
143 |
144 |
154 | Employee Controller
155 |
156 |
157 |
158 | ```
159 |
160 | ### 3.2. `./views/error/error.php`
161 |
162 | This view will be always loaded when the user specifies a bad parameter. As you can see, we print an error message that we will see in the last steps.
163 |
164 | ```html
165 |
166 |
167 |
168 |
169 |
170 | Document
171 |
172 |
173 | ". $errorMsg ."";
175 | ?>
176 |
177 |
178 | ```
179 |
180 | ## 4. Controllers
181 |
182 | > This are the PHP files that are called in the [index.php](./index.php) file. Here we will implement the functionalities of each controller.
183 |
184 | ### `./controllers/employeeController.php`
185 |
186 | Before we start, we need to require the model, that will be responsible of calling the database queries to obtain or modify the information. We will create the model later.
187 |
188 | ```php
189 | As we said before, the models are responsible of calling the database queries to obtain or modify the information.
238 |
239 | ### 5.1. Connection to the database
240 |
241 | In order to send queries to the database, we need to create the connection.
242 |
243 | #### 5.1.1. `./models/helper/dbConnection.php`
244 |
245 | ```php
246 | PDO::ERRMODE_EXCEPTION,
258 | PDO::ATTR_EMULATE_PREPARES => FALSE,
259 | ];
260 |
261 | $pdo = new PDO($connection, USER, PASSWORD, $options);
262 |
263 | return $pdo;
264 | } catch (PDOException $e) {
265 | require_once(VIEWS . "/error/error.php");
266 | }
267 | }
268 | ```
269 |
270 | ### 5.2. `./models/employeeModel.php`
271 |
272 | First, we must require the previous database connection file in order to execute the database queries.
273 |
274 | ```php
275 | require_once("helper/dbConnection.php");
276 | ```
277 |
278 | In this model we will create the `get` function that we are executing in the `employeeController.php`.
279 |
280 | ```php
281 | function get()
282 | {
283 | $query = conn()->prepare("SELECT e.id, e.name, e.email, g.name as 'gender', e.city, e.age, e.phone_number
284 | FROM employees e
285 | INNER JOIN genders g ON e.gender_id = g.id
286 | ORDER BY e.id ASC;");
287 |
288 | try {
289 | $query->execute();
290 | $employees = $query->fetchAll();
291 | return $employees;
292 | } catch (PDOException $e) {
293 | return [];
294 | }
295 | }
296 | ```
297 |
298 | ## 6. Views
299 |
300 | > View are the frontend part of the MVC pattern. Inside them we should create all the necessary code to show the information to the user.
301 |
302 | ### 6.1. `./views/employee/employeeDashboard.php`
303 |
304 | This will be the file that shows all the records of the database.
305 |
306 | ```php
307 |
308 |
309 |
310 |
311 |
312 |
313 | Document
314 |
315 |
316 |
317 |
318 |