patternphpMinor
HTTP Request/Response class
Viewed 0 times
responserequestclasshttp
Problem
I would like to get feedback on this, which are just simple
I tried to keep it simple, so I made two interfaces and two classes:
The
RequestInterface.php
ResponseInterface.php
Request.php
Response.php
```
na
Response/Request classes.I tried to keep it simple, so I made two interfaces and two classes:
- RequestInterface.php
- Request.php
- ResponseInterface.php
- Response.php
The
use Foo\Storage\ArrayHandler as access; is just a class to access and set arrays, so I wouldn't have to use functions like array_key_exists all over the place to check for keys, set, and unset arrays. RequestInterface.php
namespace Foo\Http;
interface RequestInterface
{
public function get($key, $default = null);
public function post($key, $default = null);
public function server($key, $default = null);
public function getMethod();
}ResponseInterface.php
namespace Foo\Http;
interface ResponseInterface
{
public function setStatusCode($statusCode);
public function addHeader($name, $value);
public function setContentType($contentType);
public function redirect($url);
}Request.php
namespace Foo\Http;
use Foo\Storage\ArrayHandler as access;
class Request implements RequestInterface
{
protected $get;
protected $post;
protected $files;
protected $server;
protected $cookies;
public function __construct(
access $get,
access $post,
access $files,
access $server,
access $cookies
) {
$this->get = $get;
$this->post = $post;
$this->files = $files;
$this->server = $server;
$this->cookies = $cookies;
}
public function get($key, $defaultValue = null){
return $this->get->get($key, $defaultValue);
}
public function post($key, $defaultValue = null){
return $this->post->get($key, $defaultValue);
}
public function server($key, $defaultValue = null){
return $this->server->get($key, $defaultValue);
}
public function getMethod(){
return $this->server->get('REQUEST_METHOD');
}
}Response.php
```
na
Solution
You should take a look at Symfony's HttpFoundation component.
They've written various Request/Response objects similar to what you're attempting to do and they are extremely well done. Either I'd use them directly(save your own time). Or if this is an exploratory exercise then take a look at their code base to see how you can improve on your own solution.
It's not as sexy as writing your own objects, but it'll save you a lot of time and effort in the long run.
They've written various Request/Response objects similar to what you're attempting to do and they are extremely well done. Either I'd use them directly(save your own time). Or if this is an exploratory exercise then take a look at their code base to see how you can improve on your own solution.
It's not as sexy as writing your own objects, but it'll save you a lot of time and effort in the long run.
Context
StackExchange Code Review Q#73515, answer score: 3
Revisions (0)
No revisions yet.