Skip to content

Commit 85b63f0

Browse files
committed
init
0 parents  commit 85b63f0

File tree

867 files changed

+228718
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

867 files changed

+228718
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.project
2+
.settings/

README.md

+240
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
# CodeIgniter Rest Server
2+
3+
A fully RESTful server implementation for CodeIgniter using one library, one
4+
config file and one controller.
5+
6+
## Requirements
7+
8+
1. PHP 5.2 or greater
9+
2. CodeIgniter 2.1.0 to 3.0-dev
10+
11+
_Note: for 1.7.x support download v2.2 from Downloads tab_
12+
13+
## Installation
14+
15+
Drag and drop the **application/libraries/Format.php** and **application/libraries/REST_Controller.php** files into your application's directories. Either autoload the `REST_Controller` class or `require_once` it at the top of your controllers to load it into the scope. Additionally, copy the **rest.php** file from **application/config** in your application's configuration directory.
16+
17+
## Handling Requests
18+
19+
When your controller extends from `REST_Controller`, the method names will be appended with the HTTP method used to access the request. If you're making an HTTP `GET` call to `/books`, for instance, it would call a `Books#index_get()` method.
20+
21+
This allows you to implement a RESTful interface easily:
22+
23+
class Books extends REST_Controller
24+
{
25+
public function index_get()
26+
{
27+
// Display all books
28+
}
29+
30+
public function index_post()
31+
{
32+
// Create a new book
33+
}
34+
}
35+
36+
`REST_Controller` also supports `PUT` and `DELETE` methods, allowing you to support a truly RESTful interface.
37+
38+
39+
Accessing parameters is also easy. Simply use the name of the HTTP verb as a method:
40+
41+
$this->get('blah'); // GET param
42+
$this->post('blah'); // POST param
43+
$this->put('blah'); // PUT param
44+
45+
The HTTP spec for DELETE requests precludes the use of parameters. For delete requests, you can add items to the URL
46+
47+
public function index_delete($id)
48+
{
49+
$this->response(array(
50+
'returned from delete:' => $id,
51+
));
52+
}
53+
54+
## Content Types
55+
56+
`REST_Controller` supports a bunch of different request/response formats, including XML, JSON and serialised PHP. By default, the class will check the URL and look for a format either as an extension or as a separate segment.
57+
58+
This means your URLs can look like this:
59+
60+
http://example.com/books.json
61+
http://example.com/books?format=json
62+
63+
This can be flaky with URI segments, so the recommend approach is using the HTTP `Accept` header:
64+
65+
$ curl -H "Accept: application/json" http://example.com
66+
67+
Any responses you make from the class (see [responses](#responses) for more on this) will be serialised in the designated format.
68+
69+
## Responses
70+
71+
The class provides a `response()` method that allows you to return data in the user's requested response format.
72+
73+
Returning any object / array / string / whatever is easy:
74+
75+
public function index_get()
76+
{
77+
$this->response($this->db->get('books')->result());
78+
}
79+
80+
This will automatically return an `HTTP 200 OK` response. You can specify the status code in the second parameter:
81+
82+
public function index_post()
83+
{
84+
// ...create new book
85+
86+
$this->response($book, 201); // Send an HTTP 201 Created
87+
}
88+
89+
If you don't specify a response code, and the data you respond with `== FALSE` (an empty array or string, for instance), the response code will automatically be set to `404 Not Found`:
90+
91+
$this->response(array()); // HTTP 404 Not Found
92+
93+
## Multilingual Support
94+
95+
If your application uses language files to support multiple locales, `REST_Controller` will automatically parse the HTTP `Accept-Language` header and provide the language(s) in your actions. This information can be found in the `$this->response->lang` object:
96+
97+
public function __construct()
98+
{
99+
parent::__construct();
100+
101+
if (is_array($this->response->lang))
102+
{
103+
$this->load->language('application', $this->response->lang[0]);
104+
}
105+
else
106+
{
107+
$this->load->language('application', $this->response->lang);
108+
}
109+
}
110+
111+
## Authentication
112+
113+
This class also provides rudimentary support for HTTP basic authentication and/or the securer HTTP digest access authentication.
114+
115+
You can enable basic authentication by setting the `$config['rest_auth']` to `'basic'`. The `$config['rest_valid_logins']` directive can then be used to set the usernames and passwords able to log in to your system. The class will automatically send all the correct headers to trigger the authentication dialogue:
116+
117+
$config['rest_valid_logins'] = array( 'username' => 'password', 'other_person' => 'secure123' );
118+
119+
Enabling digest auth is similarly easy. Configure your desired logins in the config file like above, and set `$config['rest_auth']` to `'digest'`. The class will automatically send out the headers to enable digest auth.
120+
121+
Both methods of authentication can be secured further by using an IP whitelist. If you enable `$config['rest_ip_whitelist_enabled']` in your config file, you can then set a list of allowed IPs.
122+
123+
Any client connecting to your API will be checked against the whitelisted IP array. If they're on the list, they'll be allowed access. If not, sorry, no can do hombre. The whitelist is a comma-separated string:
124+
125+
$config['rest_ip_whitelist'] = '123.456.789.0, 987.654.32.1';
126+
127+
Your localhost IPs (`127.0.0.1` and `0.0.0.0`) are allowed by default.
128+
129+
## API Keys
130+
131+
In addition to the authentication methods above, the `REST_Controller` class also supports the use of API keys. Enabling API keys is easy. Turn it on in your **config/rest.php** file:
132+
133+
$config['rest_enable_keys'] = TRUE;
134+
135+
You'll need to create a new database table to store and access the keys. `REST_Controller` will automatically assume you have a table that looks like this:
136+
137+
CREATE TABLE `keys` (
138+
`id` int(11) NOT NULL AUTO_INCREMENT,
139+
`key` varchar(40) NOT NULL,
140+
`level` int(2) NOT NULL,
141+
`ignore_limits` tinyint(1) NOT NULL DEFAULT '0',
142+
`date_created` int(11) NOT NULL,
143+
PRIMARY KEY (`id`)
144+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
145+
146+
The class will look for an HTTP header with the API key on each request. An invalid or missing API key will result in an `HTTP 403 Forbidden`.
147+
148+
By default, the HTTP will be `X-API-KEY`. This can be configured in **config/rest.php**.
149+
150+
$ curl -X POST -H "X-API-KEY: some_key_here" http://example.com/books
151+
152+
## Other Documentation / Tutorials
153+
154+
* [NetTuts: Working with RESTful Services in CodeIgniter](http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/)
155+
156+
## Change Log
157+
158+
### 3.0.0 (NOT YET RELEASED)
159+
160+
* Added Blacklist IP option
161+
* Added controller based access controls
162+
* Added support for OPTIONS, PATCH, and HEAD (from boh1996)
163+
* Added logging of the time it takes for a request (rtime column in DB)
164+
* Changed DB schemas to use InnoDB, not MyISAM
165+
* Updated Readme to reflect new developer (Chris Kacerguis)
166+
167+
### 2.6.2
168+
169+
* Update CodeIgniter files to 2.1.3
170+
* Fixed issue #165
171+
172+
### 2.6.1
173+
174+
* Update CodeIgniter files to 2.1.2
175+
* Log Table support for IPv6 & NULL parameters
176+
* Abstract out the processes of firing a controller method within _remap() to an separate method
177+
* Moved GET, POST, PUT, and DELETE parsing to separate methods, allowing them to be overridden as needed
178+
* Small bugfix for a PHP 5.3 strlen error
179+
* Fixed some PHP 5.4 warnings
180+
* Fix for bug in Format.php's to_html() which failed to detect if $data was really a multidimensional array.
181+
* Fix for empty node on XML output format, for false = 0, true = 1.
182+
183+
### 2.6.0
184+
185+
* Added loads of PHPDoc comments.
186+
* Response where method doesn't exist is now "HTTP 405 Method Not Allowed", not "HTTP 404 Not Found".
187+
* Compatible with PHP 5.4.
188+
* Added support for gzip compression.
189+
* Fix the apache\_request\_header function with CGI.
190+
* Fixed up correctly .foo extensions to work when get arguments provided.
191+
* Allows method emulation via X-HTTP-Method-Override
192+
* Support for Backbone.emulateHTTP improved.
193+
* Combine both URI segment and GET params instead of using one or the other
194+
* Separate each piece of the WWW-Authenticate header for digest requests with a comma.
195+
* Added IP whitelist option.
196+
197+
### 2.5
198+
199+
* Instead of just seeing item, item, item, the singular version of the basenode will be used if possible. [Example](http://d.pr/RS46).
200+
* Re-factored to use the Format library, which will soon be merged with CodeIgniter.
201+
* Fixed Limit bug (limit of 5 would allow 6 requests).
202+
* Added logging for invalid API key requests.
203+
* Changed serialize to serialized.
204+
* Changed all visibility 'private' to 'protected'.
205+
* MIME's with character encodings on the end will now work.
206+
* Fixed PUT arguments. Once again just sending a body query string works. [Example](http://d.pr/cY0b)
207+
* Fixed up all .foo extensions to work when no get arguments provided, and moved .html to Format library.
208+
* Updated key.php example to use config_item('rest_keys_table') instead of hardcoded 'keys' table name.
209+
* Updated REST_Controller to use config_item('rest_limits_table') instead of hardcoded 'limits'.
210+
211+
### 2.4
212+
213+
* Added support for UTF-8 characters in XML.
214+
* Added JSONP as a return type.
215+
* Loaded the Security lib before use in case it is not loaded in the application.
216+
* Emulate the Request method for MooTools support.
217+
* Upgraded everything to use CodeIgniter Reactor 2.0.0.
218+
* Added the ability to set or override the Auth type per controller / method.
219+
* Adding ability to only accept AJAX requests.
220+
221+
### 2.3
222+
223+
* Upgraded to CodeIgniter 2.0 and stopped supporting CodeIgniter 1.7.2.
224+
* After $this->response() is called the controller will stop processing.
225+
226+
### 2.2
227+
228+
* Added config options to set table names for keys, limits and logs.
229+
* FALSE values were coming out as empty strings in xml or rawxml mode, now they will be 0/1.
230+
* key => FALSE can now be used to override the keys_enabled option for a specific method, and level
231+
is now optional. If no level is set it will assume the method has a level of 0.
232+
* Fixed issue where calls to ->get('foo') would error is foo was not set. Reported by Paul Barto.
233+
234+
## Contributions
235+
236+
This project was originally written by the awesome Phil Sturgeon, however his involvment has shifted
237+
as he is no longer using it. As of 11/20/2013 further developement and support will be done by Chris Kacerguis.
238+
239+
Pull Requests are the best way to fix bugs or add features. I know loads of you use this, so please
240+
contribute if you have improvements to be made and I'll keep releasing versions over time.

angular-test

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 94c415ed25c0f2d11bbbd7fd5e6aafa1ac4528f7

application/cache/.htaccess

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
deny from all

application/cache/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<html>
2+
<head>
3+
<title>403 Forbidden</title>
4+
</head>
5+
<body>
6+
7+
<p>Directory access is forbidden.</p>
8+
9+
</body>
10+
</html>

application/config/autoload.php

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
/*
3+
| -------------------------------------------------------------------
4+
| AUTO-LOADER
5+
| -------------------------------------------------------------------
6+
| This file specifies which systems should be loaded by default.
7+
|
8+
| In order to keep the framework as light-weight as possible only the
9+
| absolute minimal resources are loaded by default. For example,
10+
| the database is not connected to automatically since no assumption
11+
| is made regarding whether you intend to use it. This file lets
12+
| you globally define which systems you would like loaded with every
13+
| request.
14+
|
15+
| -------------------------------------------------------------------
16+
| Instructions
17+
| -------------------------------------------------------------------
18+
|
19+
| These are the things you can load automatically:
20+
|
21+
| 1. Packages
22+
| 2. Libraries
23+
| 3. Helper files
24+
| 4. Custom config files
25+
| 5. Language files
26+
| 6. Models
27+
|
28+
*/
29+
30+
/*
31+
| -------------------------------------------------------------------
32+
| Auto-load Packges
33+
| -------------------------------------------------------------------
34+
| Prototype:
35+
|
36+
| $autoload['packages'] = array(APPPATH.'third_party', '/usr/local/shared');
37+
|
38+
*/
39+
40+
$autoload['packages'] = array(APPPATH.'third_party');
41+
42+
43+
/*
44+
| -------------------------------------------------------------------
45+
| Auto-load Libraries
46+
| -------------------------------------------------------------------
47+
| These are the classes located in the system/libraries folder
48+
| or in your application/libraries folder.
49+
|
50+
| Prototype:
51+
|
52+
| $autoload['libraries'] = array('database', 'session', 'xmlrpc');
53+
*/
54+
55+
$autoload['libraries'] = array('database', 'session');
56+
57+
58+
/*
59+
| -------------------------------------------------------------------
60+
| Auto-load Helper Files
61+
| -------------------------------------------------------------------
62+
| Prototype:
63+
|
64+
| $autoload['helper'] = array('url', 'file');
65+
*/
66+
67+
$autoload['helper'] = array();
68+
69+
70+
/*
71+
| -------------------------------------------------------------------
72+
| Auto-load Config files
73+
| -------------------------------------------------------------------
74+
| Prototype:
75+
|
76+
| $autoload['config'] = array('config1', 'config2');
77+
|
78+
| NOTE: This item is intended for use ONLY if you have created custom
79+
| config files. Otherwise, leave it blank.
80+
|
81+
*/
82+
83+
$autoload['config'] = array();
84+
85+
86+
/*
87+
| -------------------------------------------------------------------
88+
| Auto-load Language files
89+
| -------------------------------------------------------------------
90+
| Prototype:
91+
|
92+
| $autoload['language'] = array('lang1', 'lang2');
93+
|
94+
| NOTE: Do not include the "_lang" part of your file. For example
95+
| "codeigniter_lang.php" would be referenced as array('codeigniter');
96+
|
97+
*/
98+
99+
$autoload['language'] = array();
100+
101+
102+
/*
103+
| -------------------------------------------------------------------
104+
| Auto-load Models
105+
| -------------------------------------------------------------------
106+
| Prototype:
107+
|
108+
| $autoload['model'] = array('model1', 'model2');
109+
|
110+
*/
111+
112+
$autoload['model'] = array();
113+
114+
115+
/* End of file autoload.php */
116+
/* Location: ./application/config/autoload.php */

0 commit comments

Comments
 (0)