If I do a post, I can get content by doing $payload = json_decode($app->request->getBody());
But I cannot understand how request->getBody
works in slim.
First, there's a magic method :
public function __get($name)
{
return $this->container->get($name);
}
This will return a Slim\Http\Request object. That's fine for now.
$this->container
is the Slim\Helper\Set, and this is the get function :
public function get($key, $default = null)
{
if ($this->has($key)) {
$isInvokable = is_object($this->data[$this->normalizeKey($key)]) && method_exists($this->data[$this->normalizeKey($key)], '__invoke');
return $isInvokable ? $this->data[$this->normalizeKey($key)]($this) : $this->data[$this->normalizeKey($key)];
}
return $default;
}
$this->data[$this->normalizeKey($key)]
is the same as $this->data['request']
, which is something of type "Closure" (not sure to understand this).
$isInvokable
is true, so this is called :
$this->data[$this->normalizeKey($key)]($this)
What is this line doing ? Why the ($this)
(Slim\Helper\Set) at the end ?
Especially, why the next function to be called is this :
public function singleton($key, $value)
{
$this->set($key, function ($c) use ($value) {
static $object;
if (null === $object) {
$object = $value($c);
}
return $object;
});
}
Why singleton($key, $value)
?
It has never been called !
$key is not defined at the start of the function. Also what makes $c a Slim\Helper\Set, and $value a closure ?
And why the execution of only static $object
makes the $object goes from unitialized to one of type Slim\Http\Request ?