Skip to content

Commit 0a4d414

Browse files
committed
rewrite PW #1
1 parent 5d09dbb commit 0a4d414

File tree

1 file changed

+33
-36
lines changed

1 file changed

+33
-36
lines changed

src/iut/1.md

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
Practical Work #1
22
=================
33

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!
98

109
* [Interactive Command Line](#interactive-command-line)
1110
* [Cli Programs](#cli-programs)
1211
* [Client and Server](#client-and-server)
1312

14-
Once you have a working VM, SSH into it:
15-
16-
$ vagrant ssh
17-
1813
Interactive Command Line
1914
------------------------
2015

@@ -51,7 +46,7 @@ Hit `enter`:
5146

5247
Hello Votre prenom
5348

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.
5550

5651
The PHP command line is also able to execute PHP code directly from the command
5752
line thanks to the `-r` option:
@@ -117,14 +112,14 @@ The result should be:
117112

118113
### 3. Include
119114

120-
In host machine, create `DOCROOT/projects/tp1/shell.php` and insert:
115+
Create the `~/php/tp1/shell.php` file, and insert:
121116

122117
``` php
123118
<?php
124119
$foo = 1;
125120
```
126121

127-
Then, from interactive shell in virtual machine:
122+
Then, from the interactive shell:
128123

129124
php > var_dump($foo); // foo is not defined
130125

@@ -141,9 +136,10 @@ You should get the following result:
141136

142137
NULL
143138

144-
Now, include your file:
139+
Now, include your file (you might have to write the absolute path to your
140+
`shell.php`):
145141

146-
php > include "/var/www/tp1/shell.php";
142+
php > include "~/php/tp1/shell.php";
147143
php > var_dump($foo); // all code in file is executed
148144

149145
The result should be:
@@ -161,16 +157,12 @@ It is possible to execute PHP code directly from the command line by giving
161157

162158
$ php file.php
163159

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:
166162

167163
$ echo '#!/usr/bin/env php' > /path/to/your/php/file.php
168164
$ chmod a+x !$
169165

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-
174166
Executing such a script would be doable by running:
175167

176168
$ /path/to/your/php/file.php
@@ -204,12 +196,12 @@ Client and Server
204196

205197
Let's create a few directories and files:
206198

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
209201

210202
``` php
211203
<?php
212-
// DOCROOT/projects/tp1/public/cities.php
204+
// ~/php/tp1/public/cities.php
213205

214206
// include model
215207
include __DIR__ . '/../model/cities.php';
@@ -220,7 +212,7 @@ include __DIR__ . '/../view/cities.php';
220212

221213
``` php
222214
<?php
223-
// DOCROOT/projects/tp1/model/cities.php
215+
// ~/php/tp1/model/cities.php
224216

225217
$cities = array(
226218
[ "name" => "San Francisco", "country" => "USA" ],
@@ -236,7 +228,7 @@ $cities = array(
236228
```
237229

238230
``` php
239-
<!-- DOCROOT/projects/tp1/view/cities.php -->
231+
<!-- ~/php/tp1/view/cities.php -->
240232
<!DOCTYPE HTML>
241233
<html>
242234
<head>
@@ -257,10 +249,14 @@ $cities = array(
257249
</html>
258250
```
259251

260-
Open
261-
[http://localhost:8080/tp1/cities.php](http://localhost:8080/tp1/cities.php).
252+
Run:
262253

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
264260

265261
Let's define a page to present a city. You need:
266262

@@ -270,7 +266,7 @@ Let's define a page to present a city. You need:
270266

271267
``` php
272268
<?php
273-
// DOCROOT/projects/tp1/public/city.php
269+
// ~/php/tp1/public/city.php
274270

275271
include __DIR__ . '/../model/cities.php';
276272

@@ -285,8 +281,9 @@ function page_not_found()
285281
}
286282

287283
// 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)) {
290287
// No id given or invalid id
291288
page_not_found();
292289
}
@@ -301,7 +298,7 @@ include __DIR__ . '/../view/city.php';
301298
```
302299

303300
``` php
304-
<!-- DOCROOT/projects/tp1/view/city.php -->
301+
<!-- ~/php/tp1/view/city.php -->
305302
<!DOCTYPE HTML>
306303
<html>
307304
<head>
@@ -318,7 +315,7 @@ include __DIR__ . '/../view/city.php';
318315
```
319316

320317
``` html
321-
<!-- DOCROOT/projects/tp1/view/404.html -->
318+
<!-- ~/php/tp1/view/404.html -->
322319
<!DOCTYPE HTML>
323320
<html>
324321
<head>
@@ -337,12 +334,12 @@ include __DIR__ . '/../view/city.php';
337334
</html>
338335
```
339336

340-
Open
337+
Run PHP's built-in server again if you stopped it, then open:
341338
[http://localhost:8080/tp1/cities.php](http://localhost:8080/tp1/cities.php),
342339
and click on some cities.
343340

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),
346343
and see the result.
347344

348345
### Exercises!

0 commit comments

Comments
 (0)