-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexample.php
107 lines (103 loc) · 3.35 KB
/
example.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
require ('crouter.php');
class Handler{
public function hello($name){
echo "Hello $name !!!";
}
public static function hello_again($name){
echo "Hello $name again !!!";
}
}
// product
//$router = include("router.inc.php");
//$router->execute();
/**
* using CRouter to compile callback handlers in plan array.
* no need to create the tree node by split the pathinfo.
* will compile the router to source code into "router.inc.php".
*
* in product model, just need include the compiled source.
* <pre>
* $router = include("router.inc.php");
* $router->execute();
* </pre>
*/
// dev
(new CRouter('router.inc.php', true))
->error(401, function($message){
header('Location: /login', true, 401);
die($message);
})
->error(405, function($message){
header('Location: /hello/world', true, 405);
die($message);
})
->error(406, function($message){
die($message);
})
->hook('auth', function($router){
if ('lloyd' == $router->params['name'])
return true;
$router->error(401, 'Forbiden');
})
->hook('after', function($result, $router){
if ($result) {
header('Content-type: application/'. (isset($_GET['jsoncallback'])?'javascript':'json'));
if (isset($_GET['jsoncallback']))
print $_GET['jsoncallback']. '('. json_encode($result). ')';
else print json_encode($result);
}
})
->hook('before', function($router){
//$params['name'] = 'lloydzhou';
return true;
})
->get('/', function(){
echo "Hello world !!!";
})
->match(array('get', 'post'), array('/index.html', '/index.php'), function(){
echo "Good Lucky!";
})
->post('/hello', array(new Handler, 'hello'), 'auth')
// using group API to set prefix of the pathinfo
->group('/hello')
->get('/:name', array(new Handler(), 'hello'))
->get('/:name/again', array('Handler', 'hello_again'), 'auth')
// reset the prefix, or you can just set to another prefix
->group()
->get('/hello/:name:a.:ext', function($name, $ext){
if ('js' == $ext || 'json' == $ext) return array('name'=>$name);
return array('code'=>1, 'msg'=>'error message...');
}, 'auth')
->execute(array(), php_sapi_name() == 'cli' ? 'GET' : null, php_sapi_name() == 'cli' ? '/hello/lloyd.json': null);
/**
* curl -vvv 127.0.0.1:8888/hello/
* will trigger 405 error handler, should redirect to URL: "/hello/world"
*
* curl -vvv 127.0.0.1:8888/index.php
* will get 200 status code, and get body "Good Lucky!"
*
* curl -vvv -XPOST 127.0.0.1:8888/index.html
* will get 200 status code, and get body "Good Lucky!"
*
* curl -vvv -XPOST -d "name=lloyd" 127.0.0.1:8888/hello
* will get 200 status code, and get body "Hello lloyd !!!"
*
* curl -vvv 127.0.0.1:8888/hello/lloyd
* will get 200 status code, and get body "Hello lloyd !!!"
*
* curl -vvv 127.0.0.1:8888/hello/lloyd/again
* will get 200 status code, and get body "Hello lloyd again !!!"
*
* curl -vvv 127.0.0.1:8888/hello/world/again
* will trigger 406 error handler, should redirect to URL: "/login"
*
* curl -vvv 127.0.0.1:8888/hello/lloyd.json
* will get 200 status code, and get body: {"name": "lloyd"}
*
* curl -vvv 127.0.0.1:8888/hello/lloyd.js?jsoncallback=test
* will get 200 status code, and get body: test({"name": "lloyd"})
*
* curl -vvv 127.0.0.1:8888/hello/lloyd.jsx?jsoncallback=test
* will get 200 status code, and get body: test({"code":1,"msg":"error message..."})
*/