-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
72 lines (56 loc) · 1.65 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
include __DIR__.'/vendor/autoload.php';
use \Express\Express;
use \Express\Router;
$express = new Express();
$router = new Router();
// The complete request info can be var_dumped calling getInfo():
$express->getInfo();
/**
* Here you have a few common usages for ExpressPHP
*/
// Handle $_POST variables
$router->post('/', function($req, $res) {
$res->json(array(
'name' => $req->body->name
));
});
// Handle $_GET variables in /page path, for example /path?name=Alan
$router->get('/path', function($req, $res) {
$res->send('Hi, '.$req->query->name);
});
// Handle dynamic URL
$router->get('/:page', function($req, $res) {
$res->send('You are visiting '.$req->params->page.'!');
});
// You can handle nested dynamic paths:
$router->get('/:author/:id', function($req, $res) {
$res->send('You are visiting the post id: '.$req->params->id.' by '.$req->params->author);
});
// You can even use regex
$router->get('/([0-9]{4})-word', function($req, $res) {
// For example, here we want 4 numbers and then "-word"
});
// You have a few useful helpers for cookies
$router->get('/cookie', function($req, $res) {
// Get a cookie named "username"
$name = $req->cookies->username;
// Set a cookie
$res->cookie('cookieName', 'cookieContent');
// Remove a cookie
$res->clearCookie('username');
});
// And for redirections, too
$router->get('/redirect', function($req, $res) {
// Using Location header
$res->location('/other-page');
// Or using a 302 redirect:
$res->redirect('/other-page');
// Or a 301 permanent redirect
$res->redirect('/other-page', true);
});
/**
* listen() must receive an instance of Router to work.
*/
$express->listen($router);
?>