forked from selevit/PHPRest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.php
58 lines (47 loc) · 1.49 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
<?php
require_once "HttpRouter.class.php";
require_once "AjaxHandler.class.php";
/**
* Declare the URL handlers.
* Handler must be an array: array("/url-pattern/", "\\Name\\Of\\Class")
* first element is string (for string URL path match) or regexp
*/
$handlers = array(
array("/ajax/register/", '\\AjaxRegisterHandler'),
array("#^/product/([0-9]+)/reviews/$#", '\\ProductReviewHandler'),
array("#^/Example.php/news/(.*)$#", '\\NewsHandler'),
);
/**
* All handlers must be subclass of PHPRest\HttpHandler
*/
class AjaxRegisterHandler extends PHPRest\AjaxHandler
{
/**
* handler POST-requests
*/
public function post()
{
// Get POST params
$login = $this->getBodyParam("login");
$pass = $this->getBodyParam("pass");
// Get URL-query params
$redirect_url = $this->getQueryParam("next");
// Check form fields
if (!$login) $this->setError("login", "Required field");
if (!$pass) $this->setError("pass", "Required field");
// If errors found, show it
if ($this->getErrors()) {
$this->writeErrors();
return;
}
// Do other stuff...
$response = array("user_id" => 100);
$this->write($response);
}
}
class ProductReviewHandler extends PHPRest\AjaxHandler {};
class NewsHandler extends PHPRest\AjaxHandler {};
// Initialize HTTP router
$router = new PHPRest\HttpRouter($handlers);
// Initialize HTTP requests handler
$router->initHandler();