Search code examples
phpcakephpimmutabilitycakephp-3.4

cakephp-3.4 immutable http\request api API


In cakephp 3.3 I could use statement like this in controller:

$this->request->data = array_merge($this->request->query,$this->request->data);

How can I achieve the same effect using new immutable http\request api API i n cake 3.4/3.5?


Solution

  • So it's quite bad practice to overwrite (or even append to) the request, since this is what the client has sent - if you really still want to go that way, one could use reflection to set the value... Did I mention this is bad practice?

    Like.. really bad practice :)

    $reflectionClass = new ReflectionObject($this->request);
    $reflectionProperty = $reflectionClass->getProperty('data');
    $reflectionProperty->setAccessible(true);
    $reflectionProperty->setValue($this->request, -YourNewArray-);
    

    I guess I don't have to repeat that what already has been said, but if it saves you the problem of upgrading between versions.. this might fix it.