-
Notifications
You must be signed in to change notification settings - Fork 5
/
slim.php
182 lines (174 loc) · 5.47 KB
/
slim.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Step 1: Require the Slim Framework
*
* If you are not using Composer, you need to require the
* Slim Framework and register its PSR-0 autoloader.
*
* If you are using Composer, you can skip this step.
*/
require 'Slim/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
/**
* Step 2: Instantiate a Slim application
*
* This example instantiates a Slim application using
* its default settings. However, you will usually configure
* your Slim application now by passing an associative array
* of setting names and values into the application constructor.
*/
$app = new \Slim\Slim(['debug' => true]);
/**
* Step 3: Define the Slim application routes
*
* Here we define several Slim application routes that respond
* to appropriate HTTP request methods. In this example, the second
* argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete`
* is an anonymous function.
*/
// echo $settingValue = $app->config('templates.path');
// GET route
$app->get(
'/',
function () {
echo 'Hello Slim';
}
);
$app->get('/hello/:name', function ($name=null) {
echo "Hello, $name";
});
// POST route
$app->post(
'/post',
function () {
echo 'This is a POST route';
}
);
// PUT route
$app->put(
'/put',
function () {
echo 'This is a PUT route';
}
);
// PATCH route
$app->patch('/patch', function () {
echo 'This is a PATCH route';
});
// DELETE route
$app->delete(
'/delete',
function () {
echo 'This is a DELETE route';
}
);
/*
$ curl http://www.vhallapp.com/slim.php/books
[{"id":"1","title":"Three Musketeers","author":"Alexander Dumas","summary":"Thre
e Musketeers"},{"id":"2","title":"Meditations","author":"Marcus","summary":"Medi
tations"}]
vhalllsp@VHALLLSP-PC /d/soft/wamp/www/laravel_web (appapi)
$ curl -X POST -d '{"title":"php","author":"phper","summary":"php7"}' http://ww
w.vhallapp.com/slim.php/book
{"id":null}
vhalllsp@VHALLLSP-PC /d/soft/wamp/www/laravel_web (appapi)
$ curl -X POST -d 'title=php&author=phper&summary=php7' http://www.vhallapp.com
/slim.php/book
{"id":"3"}
vhalllsp@VHALLLSP-PC /d/soft/wamp/www/laravel_web (appapi)
$ curl -X PUT -d 'title=php7' http://www.vhallapp.com/slim.php/book/3
{"status":true,"message":"Book updated successfully"}
vhalllsp@VHALLLSP-PC /d/soft/wamp/www/laravel_web (appapi)
$ curl -X DELETE http://www.vhallapp.com/slim.php/book/3
{"status":true,"message":"Book deleted successfully"}
*/
// https://github.com/phpmasterdotcom/WritingARESTfulWebServiceWithSlim/blob/master/public/index.php
include "notorm/NotORM.php";
$pdo = new PDO("mysql:dbname=software",'root','');
$db = new NotORM($pdo);
$app->get("/books", function () use ($app, $db) {
$books = array();
foreach ($db->books() as $book) {
$books[] = array(
"id" => $book["id"],
"title" => $book["title"],
"author" => $book["author"],
"summary" => $book["summary"]
);
}
$app->response()->header("Content-Type", "application/json");
echo json_encode($books);
});
$app->get("/book/:id", function ($id) use ($app, $db) {
$app->response()->header("Content-Type", "application/json");
$book = $db->books()->where("id", $id);
if ($data = $book->fetch()) {
echo json_encode(array(
"id" => $data["id"],
"title" => $data["title"],
"author" => $data["author"],
"summary" => $data["summary"]
));
}
else{
echo json_encode(array(
"status" => false,
"message" => "Book ID $id does not exist"
));
}
});
$app->post("/book", function () use($app, $db) {
$app->response()->header("Content-Type", "application/json");
$book = $app->request()->post();
$result = $db->books->insert($book);
echo json_encode(array("id" => $result["id"]));
});
$app->put("/book/:id", function ($id) use ($app, $db) {
$app->response()->header("Content-Type", "application/json");
$book = $db->books()->where("id", $id);
if ($book->fetch()) {
$post = $app->request()->put();
$result = $book->update($post);
echo json_encode(array(
"status" => (bool)$result,
"message" => "Book updated successfully"
));
}
else{
echo json_encode(array(
"status" => false,
"message" => "Book id $id does not exist"
));
}
});
$app->delete("/book/:id", function ($id) use($app, $db) {
$app->response()->header("Content-Type", "application/json");
$book = $db->books()->where("id", $id);
if ($book->fetch()) {
$result = $book->delete();
echo json_encode(array(
"status" => true,
"message" => "Book deleted successfully"
));
}
else{
echo json_encode(array(
"status" => false,
"message" => "Book id $id does not exist"
));
}
});
/**
* Step 4: Run the Slim application
*
* This method should be called last. This executes the Slim application
* and returns the HTTP response to the HTTP client.
*/
$app->run();
// 此时再打开浏览器输入localhost将只能看到以下内容,其实浏览器使用get方法,在slim的Get路由中输出了Hello Slim。
$app->post(
'/post',
function () {
echo 'This is a POST route';
}
);