我正在尝试将RESTful服务集成到我的Codeigniter应用程序中。我正在使用这个图书馆https://github.com/chriskacerguis/codeigniter-restserver还有来自https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814.
然而,对于如何实现路由,我有点困惑。本教程提到使用完整的url,但我想做如下操作:
我的控制器
类AdminLogin\u WS扩展REST\u控制器{
public function __construct() {
parent::__construct();
$this->load->model('AccountModel');
}
public function login_get(){
$this->response(json_encode(null));
}
public function login_post(){
$username = $this->post('username');
$this->response(json_encode($username));
}
}
我的路线
$route['AdminLogin\u WS/Login']['post']='AdminLogin\u WS/Login\u post';
$route['AdminLogin\u WS/Login']='AdminLogin\u WS/Login';
REST请求
public function ws_login(){
$this->curl->create('https://url.com/AdminLogin_WS/Login');
$this->curl->http_login('login','password');
$this->curl->post(array(
'username' => 'auser'
));
$result = $this->curl->execute();
var_dump(json_decode($result));
}
如何指定post或get的函数?
可以分别使用_post()
和_get()
来指定函数是post还是get。
对于您的路由,我认为您的路由错误。主路线和备用路线之间应存在差异。你应该吃点类似的东西
$route['method/param1'] = 'controller/method/param1';
用聊天信息更新
使用login\u get()。
原始答案
我会将此作为评论发布,但没有代表这样做。
您所说的“仅当我创建一个名为login\u get()
的控制器函数时,它才起作用”是什么意思?在我看来,你是在给你的路线发送GET而不是POST。您能否提供一些有关如何测试的信息,以查看是否可以发布并访问您的登录\u POST()
?你试过下载邮递员之类的工具吗(https://www.getpostman.com/)并且发送一个帖子来帮助消除你没有正确发送帖子的可能性?