-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi_router.php
66 lines (55 loc) · 1.66 KB
/
api_router.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
<?php
class API_Router extends CI_Controller
{
public function index()
{
$args = func_get_args();
$version = $args[0];
$i = ($version) ? 1 : 0;
$controller = $args[$i + 1];
$action = $args[$i + 2];
$this->router->directory = $version . '/';
$this->router->class = $controller;
$this->router->method = $action;
$parameters = array_slice($this->uri->rsegments, $i + 5);
if ($parameters && strpos($parameters[count($parameters) - 1], '.'))
{
$arr = explode('.', $parameters[count($parameters) - 1], 2);
$parameters[count($parameters) - 1] = $arr[0];
}
if (!$version && strpos($_SERVER['HTTP_ACCEPT'], 'version=') !== FALSE)
{
$arr = explode(';', $_SERVER['HTTP_ACCEPT']);
$arr = explode('=', trim($arr[1]));
$version = $arr[1];
}
$controller_path = APPPATH . 'controllers/' . $version . '/' . $controller . '.php';
if (file_exists($controller_path))
{
require_once $controller_path;
$controller = new $controller();
}
else
{
header('HTTP/1.1 406 Not Acceptable');
header('Status: 406 Not Acceptable');
exit;
}
define('API_VERSION', $version);
if (method_exists($controller, '_remap'))
{
$controller->_remap($action, $parameters);
}
else
{
if (method_exists($controller, $action))
{
call_user_func_array(array($controller, $action), $parameters);
}
else
{
show_404();
}
}
}
}