PHP获取控制器中的PUT和DELETE数组



我正试图通过DOJO请求向控制器发出请求

        request("/category/", {
            method: "PUT",
              data: {
                    original: event.target.getAttribute("data-original"),
                    edited: event.target.getAttribute("data-edited"),
                    id: event.target.getAttribute("data-id")
                }
        }).then(function(response) {
            response = JSON.parse(response);
            if(response.success) {
                createRow(response.id);
            } else {
                // handle validation errors here
            }
        });

它向我的/category/路由发出PUT请求,该请求被提取并发送到控制器,此时我想访问$_PUT超全局,我认为它的存在方式与访问$_POST超全局不同。

public function putIndex()
{
    try {
        try {
            $this->category->edit(
                new CategoryVO($request['id'], $request['original']),
                new CategoryVO($request['id'], $request['edited'])
            ); // This is where I'd like to access the values sent in the PUT request
            echo json_encode(
                array(
                    "success" => true
                )
            );
        } catch (CategoryValidation $validationException) {
            echo json_encode(
                array(
                    "success" => false,
                    "validation_errors" => $this->service->prepareErrors(
                        $validationException
                    )
                )
            );
        }
    } catch(Exception $e) {
        echo json_encode(
            array(
                "success" => false,
                "unexpected_exception" => true
            )
        );
    }
}

仅供参考:自定义构建的框架,所以没有zf2/codeigniter可能附带的任何漂亮功能。

看看这是否有助于您:https://stackoverflow.com/a/6270140/6097905

基本上,您需要获得请求方法(我想它无论如何都会派上用场),然后检索数据,并以静态方式存储它。

您没有$_PUT超全局,并且在PUT请求中发送的数据不会出现在$_POST中,这是正确的。

您必须手动从php://input读取数据,然后将其存储在某个位置。

在谷歌上搜索"PHP PUT数据"会得到这个最重要的结果,其中有一个很好的例子:http://www.lornajane.net/posts/2008/accessing-incoming-put-data-from-php

最新更新