Skip to content

Commit 39aca67

Browse files
committed
RestFullController Added
1 parent 6fce356 commit 39aca67

File tree

5 files changed

+114
-12
lines changed

5 files changed

+114
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If you want some [customization](./docs/customization.md), first make it in the
2525

2626
if you like to install with all the default configuration settings you may just run the following command
2727

28-
php composer.phar create-project xiidea/codeigniter-extended path/ 1.1.0
28+
php composer.phar create-project xiidea/codeigniter-extended path/ 1.2.0
2929

3030
Composer will install Codeigniter2 and all the dependencies under the working directory. And ask you for some configuration values.
3131

composer.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@
6363
"src/application/third_party/{$name}": ["type:codeigniter-third-party"]
6464
},
6565
"branch-alias": {
66-
"dev-master": "1.1.x-dev"
66+
"dev-master": "1.2.x-dev"
6767
}
68-
},
69-
"variables": {
70-
"ci.package.name": "ellislab/codeigniter",
71-
"ci.app.dir": "src/application",
72-
"ci.web.dir": "web"
7368
}
7469
}

docs/controller.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,39 @@ If you like to render using Twig you can use the following codes:
7979
For default template system, Creating layout is same task as create a view file. Create a view file into **{APPDIR}/view/_layouts** like other view files. Then You just need to echo the $content variable where you like to display the partial view within the layout.
8080
If you are using the twig template engine, then follow the [twig instruction](./twig.md).
8181

82+
6. RestFullController:
83+
----------------------
84+
A base controller for implementing Rest Api. All controller those serve rest api should extend it instead of CIX_Controller or CI_Controller. A sample
85+
Controller implementing Api may looks like follow:
86+
87+
```php
88+
89+
class Api extends \Xiidea\Base\RestFullController {
90+
91+
public function access_map(){
92+
return array(
93+
'user_get'=>'view',
94+
'user_post'=>'create',
95+
'user_put'=>'edit',
96+
'user_delete'=>'delete',
97+
);
98+
}
99+
100+
public function user_get(){
101+
$this->sendResponse(200, json_encode(array('name'=>'Name of user')));
102+
}
103+
104+
public function dump_post(){
105+
$this->sendResponse(200, json_encode(array('success'=>true, 'msg'=>'user created')));
106+
}
107+
108+
public function user_put(){
109+
$this->sendResponse(200, json_encode(array('success'=>true, 'msg'=>'user updated')));
110+
}
111+
112+
public function user_delete(){
113+
$this->sendResponse(200, json_encode(array('success'=>true, 'msg'=>'user deleted')));
114+
}
115+
}
116+
117+
```

src/libs/Xiidea/Base/Controller.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ class Controller extends \CI_Controller
5353
{
5454

5555
protected $_layout = 'main';
56+
protected $_method = null;
57+
5658
private $_isAjaxRequest = false;
5759
private $_twig = null;
5860
private $_twigPath = null;
59-
private $_method = null;
60-
public $twig;
6161

6262
function __construct()
6363
{
6464
parent::__construct();
6565
$this->_method = $this->router->fetch_method();
66-
$this->_restrictFromRouter();
66+
$this->_restrictFromRouter(__CLASS__);
6767

6868
$this->_isAjaxRequest = ($this->input->get('ajax') || $this->input->is_ajax_request());
6969
}
@@ -237,9 +237,9 @@ public function show_error($msg = null, $code = null, $header = null)
237237
show_error($msg, $code, $header);
238238
}
239239

240-
private function _restrictFromRouter()
240+
protected function _restrictFromRouter($class)
241241
{
242-
if(in_array($this->_method, get_class_methods(__CLASS__))){
242+
if (in_array($this->_method, get_class_methods($class))) {
243243
$this->show_error(null, 404);
244244
}
245245
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Xiidea\Base;
4+
5+
class RestFullController extends Controller
6+
{
7+
8+
function __construct()
9+
{
10+
parent::__construct();
11+
$this->_restrictFromRouter(__CLASS__);
12+
$this->_re_route();
13+
}
14+
15+
public function _remap($method, $arguments)
16+
{
17+
try {
18+
call_user_func_array(array($this, $this->router->fetch_method()), $arguments);
19+
}
20+
catch (\Exception $e) {
21+
$this->sendResponse(404);
22+
}
23+
}
24+
25+
private function _re_route()
26+
{
27+
$requestMethod = strtolower($_SERVER['REQUEST_METHOD']);
28+
$controllerMethod = $this->_method . '_' . $requestMethod;
29+
30+
if (!is_callable(array($this, $controllerMethod))) {
31+
$this->sendResponse(404);
32+
}
33+
34+
$this->router->set_method($controllerMethod);
35+
}
36+
37+
public function sendResponse($status = 200, $body = null, $content_type = 'application/json')
38+
{
39+
set_status_header($status);
40+
41+
if (!empty($body)) {
42+
43+
header('Content-type: ' . $content_type);
44+
die($body);
45+
46+
} else {
47+
48+
switch ($status) {
49+
case 401:
50+
$message = 'You must be authorized to view this page.';
51+
break;
52+
case 404:
53+
$message = 'The requested URL ' . $_SERVER['REQUEST_URI'] . ' was not found.';
54+
break;
55+
case 500:
56+
$message = 'The server encountered an error processing your request.';
57+
break;
58+
case 501:
59+
$message = 'The requested method is not implemented.';
60+
break;
61+
default :
62+
$message = 'An Error Was Encountered!';
63+
}
64+
65+
$this->show_error($message, $status);
66+
}
67+
}
68+
}
69+
70+
/* End of file RestFullController.php */
71+
/* Location: ./Xiidea/Base/RestController.php */

0 commit comments

Comments
 (0)