patternphpMinor
Handling HTTP requests
Viewed 0 times
handlinghttprequests
Problem
This class handles HTTP requests. It's a singleton class that parses URI to get the controller, method and parameters and then executes the controller's method.
Maybe
I know that it can be improved. The execute method could be better because there are some repeated code.
```
_host = $_SERVER['HTTP_HOST'];
$this->_request_uri = $_SERVER['REQUEST_URI'];
$this->_script_name = $_SERVER['SCRIPT_NAME'];
$this->_headers = array();
$this->_parseUri();
}
private function _parseUri()
{
/ In 'http://www.example.com/foo/bar/' get the '/foo/bar/' part /
$part = str_replace(
dirname($this->_script_name),
'',
$this->_request_uri
);
/ break it into chunks and clean up empty positions /
$chunks = explode('/', $part);
$chunks = array_filter($chunks);
if (count($chunks))
{
if ($this->_controller = array_shift($chunks))
{
$this->_controller = ucfirst($this->_controller) . '_Controller';
}
if ($this->_method = array_shift($chunks))
{
$this->_method = ucfirst($this->_method) . 'Action';
}
$this->_parameters = $chunks ? $chunks : null;
}
}
private function _send_headers()
{
foreach($this->_headers as $header)
{
header($header);
}
}
public static function instance()
{
if (!self::$_instance)
{
self::$_instance = new Request();
}
return self::$_instance;
}
public function execute()
{
/ There is a controller ... /
if (!empty($this->_controller))
{
if (!class_exists($this->_controller, true))
{
array_push($this->_headers, 'HTTP/1.1 404 Not F
Maybe
parseUri should be in another class and maybe there is too much responsibility for this class.I know that it can be improved. The execute method could be better because there are some repeated code.
```
_host = $_SERVER['HTTP_HOST'];
$this->_request_uri = $_SERVER['REQUEST_URI'];
$this->_script_name = $_SERVER['SCRIPT_NAME'];
$this->_headers = array();
$this->_parseUri();
}
private function _parseUri()
{
/ In 'http://www.example.com/foo/bar/' get the '/foo/bar/' part /
$part = str_replace(
dirname($this->_script_name),
'',
$this->_request_uri
);
/ break it into chunks and clean up empty positions /
$chunks = explode('/', $part);
$chunks = array_filter($chunks);
if (count($chunks))
{
if ($this->_controller = array_shift($chunks))
{
$this->_controller = ucfirst($this->_controller) . '_Controller';
}
if ($this->_method = array_shift($chunks))
{
$this->_method = ucfirst($this->_method) . 'Action';
}
$this->_parameters = $chunks ? $chunks : null;
}
}
private function _send_headers()
{
foreach($this->_headers as $header)
{
header($header);
}
}
public static function instance()
{
if (!self::$_instance)
{
self::$_instance = new Request();
}
return self::$_instance;
}
public function execute()
{
/ There is a controller ... /
if (!empty($this->_controller))
{
if (!class_exists($this->_controller, true))
{
array_push($this->_headers, 'HTTP/1.1 404 Not F
Solution
I think there is too much responsibility for this class. I also think that it's name is a misnomer. A more appropriate name would be a
If you want to reduce/divide the responsibilities of this class, have a look at how other PHP frameworks have divided these, for example, Symfony2. Symfony2 has a
Internally, the
FrontController or RequestHandler rather than Request. When I see an object named Request I expect something that wraps around a HTTP request, providing easy access to the underlying request data and state. I.e. I expect something that is a request rather than something that processes requests.If you want to reduce/divide the responsibilities of this class, have a look at how other PHP frameworks have divided these, for example, Symfony2. Symfony2 has a
FrontController object (they call it a Kernel) that has two parts: a Router and a Resolver. You put Request objects into the Kernel and you get Response objects back.Internally, the
Kernel passes the Request to the Router. The Router figures out which Controller and Action are being requested. This information is then passed to the Resolver which tries to load the actual PHP class that holds the Controller and Action. The Kernel then executes the Controller/Action which returns a Response object, which is (usually) displayed by the Kernel.Context
StackExchange Code Review Q#990, answer score: 6
Revisions (0)
No revisions yet.