当前位置: 技术文章>> 如何在Magento 2中获取POST和GET请求

文章标题:如何在Magento 2中获取POST和GET请求
  • 文章分类: Magento
  • 10461 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


如何在Magento 2中获取POST和GET请求

对于扩展Magento\Framework\App\Action\Action的控制器,可以在$this->getRequest()->getPost()的帮助下获取请求。

对于自定义类,请在构造函数中注入请求:

<?php 
namespace Namespace\Module\Something;
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(); 
    }
}


推荐文章