首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在codeigniter中创建Rest API

如何在codeigniter中创建Rest API
EN

Stack Overflow用户
提问于 2018-07-11 14:40:31
回答 5查看 22.4K关注 0票数 1

为了创建简单的Rest API,我按照以下步骤下载了CodeIgniter-restserver,并从下载的文件中将粘贴的REST_Controller复制到我的项目下的库中(src是项目名称)。然后在我的项目的控制器内部创建了Api.php

代码语言:javascript
复制
<?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,但未显示结果。

EN

回答 5

Stack Overflow用户

发布于 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

然后将此代码复制并粘贴到其中:

代码语言:javascript
复制
<?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';

希望这能有所帮助

票数 4
EN

Stack Overflow用户

发布于 2018-07-11 14:46:20

请逐行阅读this article。这是初学者使用CodeIgniter Rest API库的最佳解决方案。

代码语言:javascript
复制
<?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

How to use the Rest API library in CodeIgniter

票数 1
EN

Stack Overflow用户

发布于 2018-07-11 14:49:00

希望这能对你有所帮助:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51278792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档