1
1
Practical Work #1
2
2
=================
3
3
4
- The goal of this practical is to learn how to work with a VM and Vagrant first,
5
- by playing with the PHP language. You should focus on understanding how things
6
- work rather than trying to do things by yourself, especially because most of the
7
- work is already done. Be sure to understand everything before starting the
8
- exercises!
4
+ The goal of this practical is to play with the PHP language. You should focus on
5
+ understanding how things work rather than trying to do things by yourself,
6
+ especially because most of the work is already done for you. Be sure to
7
+ understand everything before starting the exercises!
9
8
10
9
* [ Interactive Command Line] ( #interactive-command-line )
11
10
* [ Cli Programs] ( #cli-programs )
12
11
* [ Client and Server] ( #client-and-server )
13
12
14
- Once you have a working VM, SSH into it:
15
-
16
- $ vagrant ssh
17
-
18
13
Interactive Command Line
19
14
------------------------
20
15
@@ -51,7 +46,7 @@ Hit `enter`:
51
46
52
47
Hello Votre prenom
53
48
54
- To quit the shell, just type ` quit ` or ` exit ` at the php prompt.
49
+ To quit the shell, type ` quit ` or ` exit ` at the php prompt.
55
50
56
51
The PHP command line is also able to execute PHP code directly from the command
57
52
line thanks to the ` -r ` option:
@@ -117,14 +112,14 @@ The result should be:
117
112
118
113
### 3. Include
119
114
120
- In host machine, create ` DOCROOT/projects /tp1/shell.php` and insert:
115
+ Create the ` ~/php /tp1/shell.php` file, and insert:
121
116
122
117
``` php
123
118
<?php
124
119
$foo = 1;
125
120
```
126
121
127
- Then, from interactive shell in virtual machine :
122
+ Then, from the interactive shell:
128
123
129
124
php > var_dump($foo); // foo is not defined
130
125
@@ -141,9 +136,10 @@ You should get the following result:
141
136
142
137
NULL
143
138
144
- Now, include your file:
139
+ Now, include your file (you might have to write the absolute path to your
140
+ ` shell.php ` ):
145
141
146
- php > include "/var/www /tp1/shell.php";
142
+ php > include "~/php /tp1/shell.php";
147
143
php > var_dump($foo); // all code in file is executed
148
144
149
145
The result should be:
@@ -161,16 +157,12 @@ It is possible to execute PHP code directly from the command line by giving
161
157
162
158
$ php file.php
163
159
164
- You can also use the ` #!/usr/bin/env php ` shebang and make the file a script à
165
- la shell script:
160
+ You can also use the ` #!/usr/bin/env php ` shebang and make the file a script _ à
161
+ la _ shell script:
166
162
167
163
$ echo '#!/usr/bin/env php' > /path/to/your/php/file.php
168
164
$ chmod a+x !$
169
165
170
- ** Note:** changing file permissions will NOT work here because of VirtualBox.
171
- An alternative would be to enable NFS between the host and the virtual machines,
172
- but it might cause issues.
173
-
174
166
Executing such a script would be doable by running:
175
167
176
168
$ /path/to/your/php/file.php
@@ -204,12 +196,12 @@ Client and Server
204
196
205
197
Let's create a few directories and files:
206
198
207
- $ mkdir -p $DOCROOT/projects /tp1/{public,model,view}
208
- $ touch $DOCROOT/projects /tp1/public/cities.php $DOCROOT/projects /tp1/model/cities.php $DOCROOT/projects /tp1/view/cities.php
199
+ $ mkdir -p ~/php /tp1/{public,model,view}
200
+ $ touch ~/php /tp1/public/cities.php ~/php /tp1/model/cities.php $~/php /tp1/view/cities.php
209
201
210
202
``` php
211
203
<?php
212
- // DOCROOT/projects /tp1/public/cities.php
204
+ // ~/php /tp1/public/cities.php
213
205
214
206
// include model
215
207
include __DIR__ . '/../model/cities.php';
@@ -220,7 +212,7 @@ include __DIR__ . '/../view/cities.php';
220
212
221
213
``` php
222
214
<?php
223
- // DOCROOT/projects /tp1/model/cities.php
215
+ // ~/php /tp1/model/cities.php
224
216
225
217
$cities = array(
226
218
[ "name" => "San Francisco", "country" => "USA" ],
@@ -236,7 +228,7 @@ $cities = array(
236
228
```
237
229
238
230
``` php
239
- <!-- DOCROOT/projects /tp1/view/cities.php -->
231
+ <!-- ~/php /tp1/view/cities.php -->
240
232
<!DOCTYPE HTML>
241
233
<html >
242
234
<head >
@@ -257,10 +249,14 @@ $cities = array(
257
249
</html >
258
250
```
259
251
260
- Open
261
- [ http://localhost:8080/tp1/cities.php ] ( http://localhost:8080/tp1/cities.php ) .
252
+ Run:
262
253
263
- ### Add A Dynamic Controller
254
+ php -S localhost:8080 -t ~/php/tp1/public/
255
+
256
+ Then, open:
257
+ [ http://localhost:8080/cities.php ] ( http://localhost:8080/cities.php ) .
258
+
259
+ ### Add a Dynamic Controller
264
260
265
261
Let's define a page to present a city. You need:
266
262
@@ -270,7 +266,7 @@ Let's define a page to present a city. You need:
270
266
271
267
``` php
272
268
<?php
273
- // DOCROOT/projects /tp1/public/city.php
269
+ // ~/php /tp1/public/city.php
274
270
275
271
include __DIR__ . '/../model/cities.php';
276
272
@@ -285,8 +281,9 @@ function page_not_found()
285
281
}
286
282
287
283
// retrieve id from url parameter
288
- $cityId = $_GET["id"];
289
- if (!isset($cityId) || !is_numeric($cityId) || !isset($cities[$cityId])) {
284
+ $cityId = $_GET["id"] ?? null;
285
+
286
+ if (null === $cityId || !is_numeric($cityId)) {
290
287
// No id given or invalid id
291
288
page_not_found();
292
289
}
@@ -301,7 +298,7 @@ include __DIR__ . '/../view/city.php';
301
298
```
302
299
303
300
``` php
304
- <!-- DOCROOT/projects /tp1/view/city.php -->
301
+ <!-- ~/php /tp1/view/city.php -->
305
302
<!DOCTYPE HTML>
306
303
<html >
307
304
<head >
@@ -318,7 +315,7 @@ include __DIR__ . '/../view/city.php';
318
315
```
319
316
320
317
``` html
321
- <!-- DOCROOT/projects /tp1/view/404.html -->
318
+ <!-- ~/php /tp1/view/404.html -->
322
319
<!DOCTYPE HTML>
323
320
<html >
324
321
<head >
@@ -337,12 +334,12 @@ include __DIR__ . '/../view/city.php';
337
334
</html >
338
335
```
339
336
340
- Open
337
+ Run PHP's built-in server again if you stopped it, then open:
341
338
[ http://localhost:8080/tp1/cities.php ] ( http://localhost:8080/tp1/cities.php ) ,
342
339
and click on some cities.
343
340
344
- Open
345
- [ http://localhost:8080/tp1/city.php?id=-1 ] ( http://localhost:8080/tp1/city.php?id=-1 )
341
+ Open:
342
+ [ http://localhost:8080/tp1/city.php?id=-1 ] ( http://localhost:8080/tp1/city.php?id=-1 ) ,
346
343
and see the result.
347
344
348
345
### Exercises!
0 commit comments