我正试图为我的网站开发REST api。我正在使用CodeIgniter的PHP框架。我已经按照http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814上提到的教程创建了restful api。本教程基于在https://github.com/chriskacerguis/codeigniter-restserver
上开发的代码。我已经把rest.php放在application/config文件夹和REST_Controller.php放在application/libraries/
我已经在application/controllers/example.php中创建了一个apirequire 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);
}
}
}
我有一个。htaccess文件如下:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
然而,当我发送一个GET请求mywebsite.com/index.php/example/id/1它重定向到主页。谁能指点我一下
希望它能起作用
试试这个
mywebsite.com/index.php/user_get?id=1