【问题标题】:What is the method name for restful api in codeigniter?codeigniter中restful api的方法名称是什么?
【发布时间】:2015-12-21 11:05:22
【问题描述】:

假设我的控制器名称是 Api_example,并且我扩展了 REST_Controller。 现在我的困惑是

public function user_get(){
//Some code..
}
public function user_post(){
    //Some code..
}

现在,我无法理解该方法中的“用户”是什么。如果我访问user_get() 方法如localhost/api_example/user/getlocalhost/api_example/user/post 只是为了以json 格式显示一些array data。它不工作。 请帮帮我。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    您只能通过浏览器访问GET方法:

    localhost/index.php/api_example/user
    

    如果你想使用 POST 方法,你必须发送一个 post 请愿,你可以阅读更多关于 POST、GET、PUT 和 DELETE,这里What is difference between HTTP methods GET, POST, PUT and DELETE

    前缀是函数的名称,可以任意命名,重要的是后缀GET、POST、PUT或DELETE。 index 是默认函数的名称,url 是[server]/index.php/[controller_name]/[function_name]

    例如:

    localhost/index.php/api_example/user
    

    localhost 是服务器名称。

    index.php 是用于访问控制器文件夹的 codeigniter url 段。

    api_example 是控制器的名称。

    user 是函数的名称 (function user_get(){ ... })

    【讨论】:

    • 感谢您的回答。但我想知道'user_get()'中的'user'之类的前缀词到底是什么。我见过很多例子,但有人用过'index_get()/index_post()'。
    • 我明白了,直到你解释了,现在如果我按照这个程序,我会得到一个 user_post() 的“未知方法”异常。我是否必须更改 routes.PHP 中的任何内容?现在我已经提到了['api_example/user/(:any)']=['api_example/user_post']。但我不确定。
    • 你不能从浏览器访问POST方法,只有GET方法,你可以通过ajax或者一些chrome扩展访问POST方法。 url是一样的,只是改变类型,如果类型是POST,自动访问函数user_post(){...},如果你类型是GET访问函数user_get(){...}
    • 非常感谢。你拯救了我的一天。我用的是firefox扩展-addons.mozilla.org/en-US/firefox/addon/restclient
    【解决方案2】:

    您可以使用以下网址 [用于 GET 请求]:

    YOUR_BASE_URL/api/example/users
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 2016-12-14
      • 1970-01-01
      • 2023-01-12
      • 2015-06-05
      • 2018-07-10
      相关资源
      最近更新 更多