【问题标题】:Kohana Restful PutKohana Restful Put
【发布时间】:2017-01-19 16:13:14
【问题描述】:

我在放入 kohana restfull api 时遇到问题。 看起来我的 $post 不起作用,因为在我使用此功能后,我只能看到“找不到数据!”。任何想法?感谢帮助。 这是我的代码。

$trg_id = $this->request->param('id');
        if ($trg_id) {
            $post = $this->request->post();
            if ($post) {
                $objTrackingGroup = ORM::factory('Orm_trackingGroup', $trg_id);
                if ($objTrackingGroup->loaded()) {
                    $objTrackingGroup->values($post)
                                      ->save();
                    $this->rest_output($data, 200);
                        } else {
                            $data = array(
                                'code' => 500,
                                'error' => 'Unknown error'
                            );
                            $this->rest_output($data, 500);
                        }

                } else {
                    $data = array(
                        'code' => 404,
                        'error' => 'Data not found!'
                    );
                    $this->rest_output($data, 404);
                }

        }else {
            $data = array(
                'code' => 404,
                'error' => 'Data not found'
            );
            $this->rest_output($data, 404);
        }

【问题讨论】:

  • 更新功能好像看不到帖子。
  • 您是使用 PUT 还是 POST 方法提交数据?因为 $this->request->post();仅适用于 POST。

标签: rest kohana kohana-3.3


【解决方案1】:

您可以使用此 Kohana 模块来使用 Restfull API。它有处理 PUT、UPDATE 方法的方法 https://github.com/SupersonicAds/kohana-restful-api

你可以使用

$this->request->query (); 
$this->request->body ();

如果您在 query() 中收到参数,那么它是一个数组,您可以使用它,或者如果您请求的是 body() 中的 RAW 请求,则可以使用 parse_str 函数将其转换为数组。

这是我的例子

class Controller_RestTest extends Controller_REST {
    public function action_index() {
    }
    public function action_update() {
        echo Debug::vars ( $this->request->query () );
        echo Debug::vars ( $this->request->body () );
    }
}

对于这个 PUT 请求,我得到了这个响应

PUT http://localhost/RestTest?a=1234&b=4567
Content-Type: application/x-www-form-urlencoded
?body-contents=this is boy contents
 -- response --
200 OK
Server:  nginx
Date:  Tue, 11 Oct 2016 11:43:44 GMT
Content-Type:  text/html; charset=utf-8
Content-Length:  158
Connection:  keep-alive
Cache-Control:  no-cache, no-store, max-age=0, must-revalidate
Vary:  Accept-Encoding
Content-Encoding:  gzip
X-Powered-By:  PHP/5.6.26




array(2) (
    "a" => string(4) "1234"
    "b" => string(4) "4567"
)

string(35) "?body-contents=this is boy contents"

【讨论】:

  • 我已经知道了,但我仍然不知道如何创建 put 示例。
  • 非常感谢。我在找这个。 :)
猜你喜欢
  • 2016-12-05
  • 2013-04-18
  • 2013-01-23
  • 1970-01-01
  • 2017-06-19
  • 2021-01-29
  • 1970-01-01
  • 2014-12-14
  • 2011-04-29
相关资源
最近更新 更多