【问题标题】:How to create Rest API in codeigniter如何在 codeigniter 中创建 Rest API
【发布时间】:2018-12-19 02:24:07
【问题描述】:

为了创建简单的 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 但没有显示结果。

【问题讨论】:

  • 我建议你在php.ini文件中启用错误报告,即error_reporting = E_ALLdisplay_errors = On

标签: php api codeigniter-3


【解决方案1】:

您需要点击以下链接 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';

希望对你有帮助

【讨论】:

    【解决方案2】:

    希望对您有所帮助:

    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

    【讨论】:

    • 你的类名应该是Api,从API改成这样的网址http://localhost/src/index.php/Api/test或者像这样使用http://localhost/src/index.php/api/test
    • 你这边出了点问题:看这个:imgur.com/a/TlEna3u
    • 你能告诉我你遵循了什么步骤吗?我按照上面的步骤没有用,所以我再问一次……你能告诉我吗
    【解决方案3】:

    请逐行阅读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

    How to use the Rest API library in CodeIgniter

    【讨论】:

    • 我尝试在邮递员中没有显示相同的内容,并且只遵循相同的文档
    • 你现在遇到了什么问题?
    • 是的,我也没有打开任何它在浏览器中显示的内容:(
    • 你的意思是,它正在显示空白页。如果是则启用错误报告为stackoverflow.com/questions/9587413/…
    • 在 Api.php 中将控制器名称 API 更改为 Api
    【解决方案4】:

    试试

    <?php
        require(APPPATH'.libraries/REST_Controller.php');
    
        class ApiController extends REST_Controller {
    
            function test()
            {
                echo "RESTfull API";
                die;
            }
        }
    ?>
    

    我建议使用库中的内置方法response

    【讨论】:

    • @GonnaHack:考虑将你的类名重命名为ApiController,文件名也应该是ApiController.php
    【解决方案5】:

    尝试小写:api/test 而不是Api/test

    更新。添加到 config/routes.php

    $route['api/test'] = 'api/test';
    

    另一件事,如果您有路线,请尝试弄清楚,他们是否使用复数形式,如果它是敌人,那么您可以尝试检查/apis/test 而不是/api/test 因为复数变换控制器名称从一到多形式(对不起原始英语)

    【讨论】:

    • 你有路由器吗?我应该问,你在 80 端口的 apache/nginx 下运行它?
    • 是否将路由添加到 config/routes.php 文件?
    • 我改变了但没有任何效果..即使我也改变了路线
    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2021-11-02
    • 2019-02-17
    • 2014-09-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多