Skip to content

Commit fd04c3b

Browse files
author
Chris Kacerguis
committed
latest changes for 3.1
1 parent 67eaafb commit fd04c3b

File tree

102 files changed

+768
-16054
lines changed

Some content is hidden

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

102 files changed

+768
-16054
lines changed

.codeclimate.yml

-3
This file was deleted.

AUTHORS.md

-10
This file was deleted.

CHANGELOG.md

-66
This file was deleted.

README.md

+59-212
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1+
# CodeIgniter RestServer
12

2-
# CodeIgniter Rest Server
3+
[![StyleCI](https://github.styleci.io/repos/219224674/shield?branch=master)](https://github.styleci.io/repos/219224674)
34

4-
[![Gitter chat](https://badges.gitter.im/chriskacerguis/codeigniter-restserver.png)](https://gitter.im/codeigniter-restserver/Lobby)
5-
6-
A fully RESTful server implementation for CodeIgniter using one library, one
7-
config file and one controller.
5+
A fully RESTful server implementation for CodeIgniter using one library, one config file and one controller.
86

97
## Requirements
108

11-
1. PHP 7.2 or greater
12-
2. CodeIgniter 3.1.10+
13-
14-
_Note: for 1.7.x support download v2.2 from Downloads tab_
15-
16-
## Important Update on 4.0.0
9+
- PHP 7.2 or greater
10+
- CodeIgniter 3.1.11+
1711

18-
Please note that version 4.0.0 is in the works, and is considered a breaking change (per SemVer). As CI 3.1.0 now has native support for Composer, this library will be moving to be composer based.
12+
## Installation
1913

20-
Take a look at the "development" branch to see what's up.
14+
```sh
15+
composer require chriskacerguis/ci-restserver
16+
```
2117

22-
## Installation & loading
18+
## Usage
2319

2420
CodeIgniter Rest Server is available on [Packagist](https://packagist.org/packages/chriskacerguis/codeigniter-restserver) (using semantic versioning), and installation via composer is the recommended way to install Codeigniter Rest Server. Just add this line to your `composer.json` file:
2521

2622
```json
27-
"chriskacerguis/codeigniter-restserver": "^3.0"
23+
"chriskacerguis/codeigniter-restserver": "^3.1"
2824
```
2925

3026
or run
@@ -33,212 +29,63 @@ or run
3329
composer require chriskacerguis/codeigniter-restserver
3430
```
3531

36-
## Handling Requests
37-
38-
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.
39-
40-
This allows you to implement a RESTful interface easily:
41-
42-
```php
43-
use Restserver\Libraries\REST_Controller;
44-
45-
class Books extends CI_Controller
46-
{
47-
use REST_Controller {
48-
REST_Controller::__construct as private __resTraitConstruct;
49-
}
50-
public function index_get()
51-
{
52-
// Display all books
53-
}
54-
55-
public function index_post()
56-
{
57-
// Create a new book
58-
}
59-
}
60-
```
61-
62-
`REST_Controller` also supports `PUT` and `DELETE` methods, allowing you to support a truly RESTful interface.
63-
64-
65-
Accessing parameters is also easy. Simply use the name of the HTTP verb as a method:
66-
67-
```php
68-
$this->get('blah'); // GET param
69-
$this->post('blah'); // POST param
70-
$this->put('blah'); // PUT param
71-
```
72-
73-
The HTTP spec for DELETE requests precludes the use of parameters. For delete requests, you can add items to the URL
74-
75-
```php
76-
public function index_delete($id)
77-
{
78-
$this->response([
79-
'returned from delete:' => $id,
80-
]);
81-
}
82-
```
83-
84-
If query parameters are passed via the URL, regardless of whether it's a GET request, can be obtained by the query method:
85-
86-
```php
87-
$this->query('blah'); // Query param
88-
```
89-
90-
## Content Types
91-
92-
`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.
93-
94-
This means your URLs can look like this:
95-
```
96-
http://example.com/books.json
97-
http://example.com/books?format=json
98-
```
99-
100-
This can be flaky with URI segments, so the recommend approach is using the HTTP `Accept` header:
101-
102-
```bash
103-
$ curl -H "Accept: application/json" http://example.com
104-
```
105-
106-
Any responses you make from the class (see [responses](#responses) for more on this) will be serialised in the designated format.
107-
108-
## Responses
109-
110-
The class provides a `response()` method that allows you to return data in the user's requested response format.
111-
112-
Returning any object / array / string / whatever is easy:
113-
114-
```php
115-
public function index_get()
116-
{
117-
$this->response($this->db->get('books')->result());
118-
}
119-
```
120-
121-
This will automatically return an `HTTP 200 OK` response. You can specify the status code in the second parameter:
32+
Step 1: Add this to your controller (should be before any of your code)
12233

12334
```php
124-
public function index_post()
125-
{
126-
// ...create new book
127-
$this->response($book, 201); // Send an HTTP 201 Created
128-
}
35+
use chriskacerguis\RestServer\RestController;
12936
```
13037

131-
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`:
38+
Step 2: Extend your controller
13239

13340
```php
134-
$this->response([]); // HTTP 404 Not Found
41+
class Example extends RestController
13542
```
13643

137-
## Configuration
138-
139-
You can overwrite all default configurations by creating a rest.php file in your config folder with your configs.
140-
All given configurations will overwrite the default ones.
141-
142-
## Language
143-
144-
You can overwrite all default language files. Just add a rest_controller_lang.php to your language and overwrite the what you want.
145-
146-
147-
## Multilingual Support
44+
## Basic GET example
14845

149-
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:
46+
Here is a basic example of
15047

15148
```php
152-
public function __construct()
153-
{
154-
parent::__construct();
155-
$this->__resTraitConstruct();
156-
157-
if (is_array($this->response->lang))
158-
{
159-
$this->load->language('application', $this->response->lang[0]);
160-
}
161-
else
162-
{
163-
$this->load->language('application', $this->response->lang);
164-
}
49+
<?php
50+
defined('BASEPATH') OR exit('No direct script access allowed');
51+
52+
use chriskacerguis\RestServer\RestController;
53+
54+
class Example extends REST_Controller {
55+
56+
function __construct()
57+
{
58+
// Construct the parent class
59+
parent::__construct();
60+
}
61+
62+
public function users_get()
63+
{
64+
// Users from a data store e.g. database
65+
$users = [
66+
['id' => 1, 'name' => 'John', 'email' => '[email protected]'],
67+
['id' => 2, 'name' => 'Jim', 'email' => '[email protected]'],
68+
];
69+
70+
$id = $this->get('id');
71+
72+
if ($id === null)
73+
{
74+
// Check if the users data store contains users
75+
if ($users)
76+
{
77+
// Set the response and exit
78+
$this->response($users, 200);
79+
}
80+
else
81+
{
82+
// Set the response and exit
83+
$this->response([
84+
'status' => false,
85+
'message' => 'No users were found'
86+
], 404);
87+
}
88+
}
89+
}
16590
}
166-
```
167-
168-
## Authentication
169-
170-
This class also provides rudimentary support for HTTP basic authentication and/or the securer HTTP digest access authentication.
171-
172-
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:
173-
174-
```php
175-
$config['rest_valid_logins'] = ['username' => 'password', 'other_person' => 'secure123'];
176-
```
177-
178-
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.
179-
180-
If you're tying this library into an AJAX endpoint where clients authenticate using PHP sessions then you may not like either of the digest nor basic authentication methods. In that case, you can tell the REST Library what PHP session variable to check for. If the variable exists, then the user is authorized. It will be up to your application to set that variable. You can define the variable in ``$config['auth_source']``. Then tell the library to use a php session variable by setting ``$config['rest_auth']`` to ``session``.
181-
182-
All three methods of authentication can be secured further by using an IP white-list. If you enable `$config['rest_ip_whitelist_enabled']` in your config file, you can then set a list of allowed IPs.
183-
184-
Any client connecting to your API will be checked against the white-listed 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:
185-
186-
```php
187-
$config['rest_ip_whitelist'] = '123.456.789.0, 987.654.32.1';
188-
```
189-
190-
Your localhost IPs (`127.0.0.1` and `0.0.0.0`) are allowed by default.
191-
192-
## API Keys
193-
194-
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:
195-
196-
```php
197-
$config['rest_enable_keys'] = TRUE;
198-
```
199-
200-
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:
201-
202-
```sql
203-
CREATE TABLE `keys` (
204-
`id` INT(11) NOT NULL AUTO_INCREMENT,
205-
`key` VARCHAR(40) NOT NULL,
206-
`level` INT(2) NOT NULL,
207-
`ignore_limits` TINYINT(1) NOT NULL DEFAULT '0',
208-
`date_created` INT(11) NOT NULL,
209-
PRIMARY KEY (`id`)
210-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
211-
```
212-
213-
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`.
214-
215-
By default, the HTTP will be `X-API-KEY`. This can be configured in **config/rest.php**.
216-
217-
```bash
218-
$ curl -X POST -H "X-API-KEY: some_key_here" http://example.com/books
219-
```
220-
## Profiling
221-
Codeigniter Profiler feature has been added to the library, so that you can use the power of CI profiler in your project just by setting config parameter to enable profile through out your application
222-
Turn it on in your **config/config.php** file:
223-
224-
```php
225-
TRUE to turn profile ON, FALSE to turn it off
226-
$config['enable_profiling'] = FALSE;
227-
```
228-
Also you need to enable `hooks` in your config.php that looks like this
229-
```php
230-
$config['enable_hooks'] = TRUE;
231-
```
232-
Also you can refer to **config/config.php.sample**
233-
234-
## Other Documentation / Tutorials
235-
236-
* [NetTuts: Working with RESTful Services in CodeIgniter](http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/)
237-
238-
## Contributions
239-
240-
This project was originally written by Phil Sturgeon, however his involvement has shifted
241-
as he is no longer using it. As of 2013/11/20 further development and support will be done by Chris Kacerguis.
242-
243-
Pull Requests are the best way to fix bugs or add features. I know loads of you use this, so please
244-
contribute if you have improvements to be made and I'll keep releasing versions over time.
91+
```

0 commit comments

Comments
 (0)