【问题标题】:How to get all post parameters in Symfony2? [duplicate]如何获取 Symfony2 中的所有帖子参数? [复制]
【发布时间】:2012-05-24 13:39:38
【问题描述】:

我想获取symfony表单的所有post参数。

我用过:

$all_parameter = $this->get('request')->getParameterHolder()->getAll();

我得到这个错误

Fatal error: Call to undefined method Symfony\Component\HttpFoundation\Request::getParameterHolder() in /Library/WebServer/Documents/Symfony/src/Uae/PortailBundle/Controller/NoteController.php on line 95

【问题讨论】:

  • 如果有人正在搜索 symfony 3 : $request = Request::createFromGlobals(); $a = $request->request->all()

标签: php symfony


【解决方案1】:
$this->get('request')->request->all()

【讨论】:

  • Request $request 是控制器操作的参数时:$request->request->all()
  • 我认为 $this->get('request') 在 2.8 中已被弃用,并在 3.0 中被删除。没有确认。不管怎样,@jmq 评论成功了。
【解决方案2】:

Symfony Request Objects 有很多公共属性代表请求的不同部分。可能最简单的描述方式就是给你看Request::initialize()的代码

/**
 * Sets the parameters for this request.
 *
 * This method also re-initializes all properties.
 *
 * @param array  $query      The GET parameters
 * @param array  $request    The POST parameters
 * @param array  $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
 * @param array  $cookies    The COOKIE parameters
 * @param array  $files      The FILES parameters
 * @param array  $server     The SERVER parameters
 * @param string $content    The raw body data
 *
 * @api
 */
public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
{
    $this->request = new ParameterBag($request);
    $this->query = new ParameterBag($query);
    $this->attributes = new ParameterBag($attributes);
    $this->cookies = new ParameterBag($cookies);
    $this->files = new FileBag($files);
    $this->server = new ServerBag($server);
    $this->headers = new HeaderBag($this->server->getHeaders());

    $this->content = $content;
    $this->languages = null;
    $this->charsets = null;
    $this->acceptableContentTypes = null;
    $this->pathInfo = null;
    $this->requestUri = null;
    $this->baseUrl = null;
    $this->basePath = null;
    $this->method = null;
    $this->format = null;
}

所以,如您所见,Request::$request 是 POST 参数的 ParameterBag

【讨论】:

  • * ParameterBag,它有all() 方法来获取包含所有参数的数组。
  • * ParameterBag,它有all() 方法来获取包含所有参数的数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
  • 2011-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多