为了创建简单的Rest API,我按照以下步骤下载了CodeIgniter-restserver,并从下载的文件中将粘贴的REST_Controller复制到我的项目下的库中(src是项目名称)。然后在我的项目的控制器内部创建了Api.php
<?php
require(APPPATH'.libraries/REST_Controller.php');
class API extends REST_Controller {
function test()
{
echo "RESTfull API";
}
}
?>And I run the URLhttp://localhost/src/index.php/Api/test in postman,但未显示结果。
发布于 2019-09-09 20:22:52
你需要点击下面的链接https://itsolutionstuff.com/post/codeigniter-3-restful-api-tutorialexample.html
然后,在运行代码之后,您将得到一个无法加载所请求的语言文件的小错误: language/english/rest_controller_lang.php
问题是codeigniter找不到rest_controller翻译。您只需要创建这个文件/application/languages/english/rest_controller_lang.php
然后将此代码复制并粘贴到其中:
<?php
/*
* English language
*/
$lang['text_rest_invalid_api_key'] = 'Invalid API key %s'; // %s is the REST API key
$lang['text_rest_invalid_credentials'] = 'Invalid credentials';
$lang['text_rest_ip_denied'] = 'IP denied';
$lang['text_rest_ip_unauthorized'] = 'IP unauthorized';
$lang['text_rest_unauthorized'] = 'Unauthorized';
$lang['text_rest_ajax_only'] = 'Only AJAX requests are allowed';
$lang['text_rest_api_key_unauthorized'] = 'This API key does not have access to the requested controller';
$lang['text_rest_api_key_permissions'] = 'This API key does not have enough permissions';
$lang['text_rest_api_key_time_limit'] = 'This API key has reached the time limit for this method';
$lang['text_rest_ip_address_time_limit'] = 'This IP Address has reached the time limit for this method';
$lang['text_rest_unknown_method'] = 'Unknown method';
$lang['text_rest_unsupported'] = 'Unsupported protocol';希望这能有所帮助
发布于 2018-07-11 14:46:20
请逐行阅读this article。这是初学者使用CodeIgniter Rest API库的最佳解决方案。
<?php
require(APPPATH.'/libraries/REST_Controller.php');
class API extends REST_Controller {
function test_get()
{
$data = array("message"=>"RESTfull API");
$this->response($data);
}
}
?>联系方式:http://localhost/src/index.php/Api/test
注意:在rest API中,您应该将方法类型定义为GET, POST, PUT, and DELETE
发布于 2018-07-11 14:49:00
希望这能对你有所帮助:
require APPPATH . '/libraries/REST_Controller.php';
class Api extends REST_Controller {
function test_get()
{
$data = array('response' => 'RESTfull API');
if(count($data ) > 0)
{
$this->response($data ,REST_Controller::HTTP_OK);
}
else
{
$error = array('message' => 'No record found');
$this->response($error,REST_Controller::HTTP_OK);
}
} 有关更多信息,请阅读:https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814
https://stackoverflow.com/questions/51278792
复制相似问题