ZF3开发的RestAPI:处理Post请求
ZF3 development of RestAPI: handling Post request for
我是 Zf3 的新手,正在开发电子邮件 Restful API.
在处理请求时遇到困难。我无法得到 POST parameters.i 认为 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(路线)
'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,
]
],
],
],
],
if BwsMailController
extends AbstractController
or AbstractRestfulController
你可以简单地: $this->params()->fromRoute()
or ->fromQuery()
如果你使用的是获取字符串。例如:
$routeParamId = $this->params()->fromRoute('id', null);
这会获取路由配置中定义的参数 id
。如果未设置,它将默认为 null
。
如果您有一个 GET url,类似于:site.com/mail?id=123
,那么您可以:
$getParamId = $this->params()->fromQuery('id', null); // sets '123' in var
还有更多选择,好好阅读the routing documentation。
我是 Zf3 的新手,正在开发电子邮件 Restful API.
在处理请求时遇到困难。我无法得到 POST parameters.i 认为 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(路线)
'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,
]
],
],
],
],
if BwsMailController
extends AbstractController
or AbstractRestfulController
你可以简单地: $this->params()->fromRoute()
or ->fromQuery()
如果你使用的是获取字符串。例如:
$routeParamId = $this->params()->fromRoute('id', null);
这会获取路由配置中定义的参数 id
。如果未设置,它将默认为 null
。
如果您有一个 GET url,类似于:site.com/mail?id=123
,那么您可以:
$getParamId = $this->params()->fromQuery('id', null); // sets '123' in var
还有更多选择,好好阅读the routing documentation。