concrete5 way for $_FILES, $_POST, $_SERVER

134 Views Asked by At

What's the concrete5 equivalent to

if (empty($_FILES) && 
    empty($_POST) && 
    isset($_SERVER['REQUEST_METHOD']) && 
    strtolower($_SERVER['REQUEST_METHOD']) == 'post') {

in way of requests?

[UPDATE] Looks like this is working:

if (empty($this->request->files->all()) && 
    empty($this->request->request->all()) && 
    null !== $this->request->server->get('REQUEST_METHOD') && 
    strtolower($this->request->server->get('REQUEST_METHOD')) == 'post') {
1

There are 1 best solutions below

2
Michele Locati On BEST ANSWER

You should get the current Request instance. In a controller method, it's as simple as writing

$request = $this->request;

If you are not using a controller but a custom class, you can mark the Request as a dependency of your class:

use Concrete\Core\Http\Request;

class YourClass
{
    /**
     * @var \Concrete\Core\Http\Request
     */
    private $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    public function TheMethodWhereYouNeedRequest()
    {
        $request = $this->request;
        // ...
    }
}

You can also get the Request instance by writing

$request = \Core::make(\Concrete\Core\Http\Request::class);

Once you have the Request instance, you can write:

if (
    // Same as empty($_FILES)
    $this->request->files->count() === 0
    &&
    // Same as empty($_POST)
    $this->request->request->count() === 0
    &&
    // Same as isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post'
    $this->request->getMethod() === 'POST'
) {
    ...
}

Please remark that the concrete5 Request extends the Symfony Request (version 3.4), so you may want to take a look at https://symfony.com/doc/3.4/components/http_foundation.html#accessing-request-data