在这篇博客中,我们将了解magento2请求和响应的不同操作。首先让我们看一下GET/POST数据。在许多情况下,我们需要获
得GET或POST方式传递的数据,例如从表单传递的数据。要处理这些数据,我们需要先读取数据,该方法类似于magento 1,
要获取GET方法传递的数据,我们只需使用以下两种方法:
$this->getRequest()->getParams()
这将获取get传递的所有数据,但如果要获取特定数据需要使用
$this->getRequest()->getParam('data');
要获取POST方法传递的数据,我们使用以下方法:
$this->request->getPost()
这将获取post传递的所有数据,但是如果要获取特定数据需要使用
$this->getRequest()->getPost('data');
但是,如果你看magento2核心文件,那么你会发现使用的是getPostvalue()方法而不是getPost()。它在lib\internal\Magento\Framework\HTTP\PhpEnvironment\Request.php中的定义是:
public function getPostValue($name = null, $default = null) { $post = $this->getPost($name, $default); if ($post instanceof ParametersInterface) { return $post->toArray(); } return $post; }
您可以通过以下方式获取POST数据:
$post = $this->getRequest()->getPostValue();
尽管两种方法具有几乎相同的功能,但是它们返回数据的格式略有不同。在magento2中,使用getPostValue()应该是更好的做法。
下面的例子会让你更好地理解:
当我们使用getPost()时,它返回内容如Zend\Stdlib\Parameters Object ( [storage:ArrayObject:private] => Array ( [data] => Array ( [title] => abc [status] => 1 [submit] => Save ) ) )
但是当我们使用getPostValue()时,我们得到一个结构良好的数组,比如:Array ( [data] => Array ( [title] => abc [status] => 1 [submit] => Save ) ) ,这就是getPostValue优于getPost()的原因。
然后从vendor\zendframework\zend-http\src\Request.php:获得getPost的值:
public function getPost($name = null, $default = null)
{ if ($this->postParams === null) { $this->postParams = new Parameters(); } if ($name === null) { return $this->postParams; } return $this->postParams->get($name, $default); }
如果在扩展Magento\Framework\App\Action\Action的控制器尝中试此方法,则上述方法可行。在其他情况下,需要在构造函数中注入请求:
class ClassName { protected $request; public function __construct( \Magento\Framework\App\Request\Http $request, ....//rest of parameters here ) { $this->request = $request; ...//rest of constructor here } public function getPost()
{ return $this->request->getPost(); } }
从控制器发送自定义Header/Response:
在magento2中,可以从控制器执行方法中操纵请求来发送自定义header和错误页。
首先,为了符合动作控制器接口\Magento\Framework\App\ActionInterface::execute(),您的控制方法必须返回\Magento\Framework\Controller\ResultInterface(\Magento\Framework\App\ResponseInterface 也是支持的,但它是遗留的,它将在M2的未来版本中删除,当所有核心方法都被重构时)。
因此,请从\Magento\Framework\Controller\ResultInterface中进行选择。最适合自定义REST API(假设它使用JSON)似乎是\Magento\Framework\Controller\Result\Json。但是,如果您需要更多自定义内容,请考虑\Magento\Framework\Controller\Result\Raw。
下方代码片段,它将帮助您更好地理解:
namespace VendorName\ModuleName\Controller; /** * Demo of authorization error for custom REST API */ class RestAuthorizationDemo extends \Magento\Framework\App\Action\Action { /** @var \Magento\Framework\Controller\Result\JsonFactory */ protected $jsonResultFactory; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory ) { parent::__construct($context); $this->jsonResultFactory = $jsonResultFactory; } public function execute() { /** @var \Magento\Framework\Controller\Result\Json $result */ $result = $this->jsonResultFactory->create(); /** You may introduce your own constants for this custom REST API */ $result->setHttpResponseCode(\Magento\Framework\Webapi\Exception::HTTP_FORBIDDEN); $result->setData(['error_message' => __('What are you doing here?')]); return $result; } }
上面的代码将导致HTTP状态代码403和正文的响应
{“error_message”:“授权失败”}