Skip to content

Commit b471a60

Browse files
salmasianchriskacerguis
authored andcommitted
Making the example in README.md work
Also expanding the example to demonstrate how the id parameter is used
1 parent 7bbf32d commit b471a60

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

README.md

+30-11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ or run
2929
composer require chriskacerguis/codeigniter-restserver
3030
```
3131

32+
Note that you will need to copy `rest.php` to your `config` directory (e.g. `application/config`)
33+
3234
Step 1: Add this to your controller (should be before any of your code)
3335

3436
```php
@@ -43,15 +45,18 @@ class Example extends RestController
4345

4446
## Basic GET example
4547

46-
Here is a basic example of
48+
Here is a basic example. This controller, which should be saved as `Api.php`, can be called in two ways:
49+
50+
* `http://domain/api/users/` will return the list of all users
51+
* `http://domain/api/users/id/1` will only return information about the user with id = 1
4752

4853
```php
4954
<?php
5055
defined('BASEPATH') OR exit('No direct script access allowed');
5156

5257
use chriskacerguis\RestServer\RestController;
5358

54-
class Example extends REST_Controller {
59+
class Api extends RestController {
5560

5661
function __construct()
5762
{
@@ -63,29 +68,43 @@ class Example extends REST_Controller {
6368
{
6469
// Users from a data store e.g. database
6570
$users = [
66-
['id' => 1, 'name' => 'John', 'email' => '[email protected]'],
67-
['id' => 2, 'name' => 'Jim', 'email' => '[email protected]'],
71+
['id' => 0, 'name' => 'John', 'email' => '[email protected]'],
72+
['id' => 1, 'name' => 'Jim', 'email' => '[email protected]'],
6873
];
6974

70-
$id = $this->get('id');
75+
$id = $this->get( 'id' );
7176

72-
if ($id === null)
77+
if ( $id === null )
7378
{
7479
// Check if the users data store contains users
75-
if ($users)
80+
if ( $users )
7681
{
7782
// Set the response and exit
78-
$this->response($users, 200);
83+
$this->response( $users, 200 );
7984
}
8085
else
8186
{
8287
// Set the response and exit
83-
$this->response([
88+
$this->response( [
8489
'status' => false,
8590
'message' => 'No users were found'
86-
], 404);
91+
], 404 );
92+
}
93+
}
94+
else
95+
{
96+
if ( array_key_exists( $id, $users ) )
97+
{
98+
$this->response( $users[$id], 200 );
99+
}
100+
else
101+
{
102+
$this->response( [
103+
'status' => false,
104+
'message' => 'No such user found'
105+
], 404 );
87106
}
88107
}
89108
}
90109
}
91-
```
110+
```

0 commit comments

Comments
 (0)