Request object improvements - markstory/cakephp GitHub Wiki
For 3.0 there are a few issues with the Request object that should be addressed. Even though the request object is quite new, I made some pretty clear errors when designing it initially.
- The constructor does too much. The constructor fishes through all the globals and processes them. It probably shouldn't do this.
- The constructor has a strange set of parameters. While there are two constructor arguments, it also reads all the super globals. This creates a bit of overhead when testing, and makes mocking out request data marginally harder.
- The $data property has wonky merging rules. In order to maintain some BC, I made the request object do some overly fancy merging. Instead I should have removed the
data[
prefix from all generated inputs.
Possible solutions
I think a few changes to the request object could make working with it much easier, and improve ease of testing.
Change constructor
The constructor should take an array of all properties in the request, these could come from globals or from user defined data. The option to accept user defined data simplifies a few testing scenarios.
<?php
$request = new Cake\Network\Request(array(
'get' => array(...),
'post' => array(...),
'files' => array(...),
'cookies' => array(...),
'url' => 'posts/index',
'base' => '',
'webroot' => '/',
'params' => array(...)
));
This would still process files
to restructure it into the correct form. To keep things simple in the front dispatcher, a static method would be added:
<?php
$request = Cake\Network\Request::createFromGlobals();
// Normal constructor + chaining
$request = new (Cake\Network\Request())->addParams(array(...));
Creating a request from globals would basically act the same as the current new Request()
works.
Status
The changes described here have been implemented in 3.0