Generating Igniter REST API Code
I am trying to develop a REST API for my site. I am using CodeIgniter PHP Framework. I followed the tutorial mentioned at http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814 to create a calm apis. The tutorial is based on code developed at https://github.com/chriskacerguis/codeigniter-restserver
I put rest.php in application / config folder and REST_Controller.php in application / libraries /
I created an api in app / controllers / example.php
require APPPATH.'/libraries/REST_Controller.php';
class example extends REST_Controller
{
function __construct()
{
// Construct our parent class
parent::__construct();
}
function user_get()
{
if(!$this->get('id'))
{
$this->response(NULL, 400);
}
// $user = $this->some_model->getSomething( $this->get('id') );
$users = array(
1 => array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com', 'fact' => 'Loves swimming'),
2 => array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com', 'fact' => 'Has a huge face'),
3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => 'Is a Scott!', array('hobbies' => array('fartings', 'bikes'))),
);
$user = @$users[$this->get('id')];
if($user)
{
$this->response($user, 200); // 200 being the HTTP response code
}
else
{
$this->response(array('error' => 'User could not be found'), 404);
}
}
}
I have a .htaccess file as shown below:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
However, when I send a GET request to mywebsite.com/index.php/example/id/1, it is redirected to the main page. Can anyone guide me.
+3
source to share
1 answer