RestAPI的ZF3开发:处理Post请求

问题描述 投票:0回答:1

我是Zf3的新手,正在开发Email Restful API。

处理请求有困难。我无法获得POST参数。我认为在module.config.php>'router'中存在一些问题

问题 1如何从请求中获取POST变量。 2我在申请页面时找到了它(使用Postman) - 如果我传递ID,控制器调用get($ id)方法 - 如果我POST变量控制器调用Create($ data)函数。

- 总是这样吗?在create($ data)方法中编写zendEmail代码是否合适?

bwsmail控制器

(创建功能)

    public function create($data)  
    {
        echo $this->getRequest()->getPost('id', "no value");
              // Output
              // this returns no value

        echo $this->getRequest();
              // Output
              //POST http://localhost:8080/bwsmail/mail?id=123456789 HTTP/1.1
              //Cookie: PHPSESSID=p5i7o8lm2ed0iocdhkc8jvos01
              //Cache-Control: no-cache
              //Postman-Token: ccaf5b37-02a2-4537-bf3f-c1dc419d8ceb
              //User-Agent: PostmanRuntime/7.6.1
              //Accept: */*
              //Host: localhost:8080
              //Accept-Encoding: gzip, deflate
              //Content-Length: 0
              //Connection: keep-alive


         $response = $this->getResponseWithHeader()->setContent( __METHOD__.' create new item of data :<b>'.'</b>');
         return $response;
}

module.config.php(route)

'bwsmail' => [
            'type' => Literal::class,
            'options' => [
                'route' => '/bwsmail',
                'defaults' => [
                    'controller' => Controller\BwsMailController::class,
                ]
            ],
            'may_terminate' => true,
            'child_routes' => [
                                'mail' => [
                                    'type' => 'segment',
                                    'options' => [
                                        'route' => '/mail[/:id]',
                                        'constraints'=>
                                        [
                                            'id' => '[0-9a-zA-Z]+',
                                        ],
                                        'defaults' => [
                                            'controller' => Controller\BwsMailController::class,
                                        ]
                                    ],
                                ],
                            ],
             ],
zend-framework3
1个回答
0
投票

如果BwsMailController扩展AbstractControllerAbstractRestfulController你可以简单地:$this->params()->fromRoute()->fromQuery()如果你使用get字符串。例如:

$routeParamId = $this->params()->fromRoute('id', null);

这将获取路径配置中定义的参数id。如果未设置,则默认为null

如果您有一个GET网址,例如:site.com/mail?id=123,那么您可以:

$getParamId = $this->params()->fromQuery('id', null); // sets '123' in var

有更多的选择,有一个很好的阅读the routing documentation

© www.soinside.com 2019 - 2024. All rights reserved.