Skip to content

HttpFoundation: Make request->get() behave more like $_REQUEST #40974

Closed
@flack

Description

@flack

The get method of Request currently looks at

  • custom routing params
  • GET
  • POST

in that order. It's a bit unfortunate, because in PHP's default config, the order in $_REQUEST is the opposite of that, i.e. a parameter in $_POST overrides the same parameter from $_GET in the $_REQUEST superglobal. Which seems more logical: Let's say a user opens some search URL, clicks through a few pages of results (typically the search params are in GET in that scenario), then changes a value in the form and submits (let's say the form POSTs by default for the sake of argument), the POST data gets ignored.

So I was wondering: Would you be open to make the order in the get function depend on the request_order (https://www.php.net/manual/en/ini.core.php#ini.request-order) ini setting? That would make $request->get('something') a drop-in replacement for $_REQUEST['something'] (well, almost at least).

Basically, it would look something like this:

    public function get(string $key, $default = null)
    {
        if ($this !== $result = $this->attributes->get($key, $this)) {
            return $result;
        }

        $order = explode('', ini_get('request_order'));

        foreach (array_reverse($order) as $item) {
            switch ($item) {
                case 'g':
                    if ($this->query->has($key)) {
                        return $this->query->all()[$key];
                    }
                    break;

                case 'p':
                    if ($this->request->has($key)) {
                        return $this->request->all()[$key];
                    }
                    break;

                case 'c':
                    if ($this->cookies->has($key)) {
                        return $this->cookies->all()[$key];
                    }
                    break;
            }
        }

        return $default;
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions