patternphpMinor
Handling page requests in MVC
Viewed 0 times
mvchandlingpagerequests
Problem
Is this an efficient way to handle page requests in MVC?
index.php:
Bootstrap.php
```
class Bootstrap {
private $controller = null,
$action = null,
$args = null;
public function __construct() {
$this->manage_url();
if($this->page_exist()){
$this->request();
} else {
$error = new ErrorController();
$error->error404();
}
}
private function request(){
$controller = new $this->controller();
if(!$this->args){ $controller->{$this->action}(); }
else{
$count_args = count($this->args);
switch ($count_args) {
case 1:
$controller->{$this->action}($this->args[0]);
break;
case 2:
$controller->{$this->action}($this->args[0], $this->args[1]);
break;
case 3:
$controller->{$this->action}($this->args[0], $this->args[1], $this->args[2]);
break;
case 4:
$controller->{$this->action}($this->args[0], $this->args[1], $this->args[2], $this->args[3]);
break;
}
}
}
/**
* Get controller, action and parametes
*/
private function manage_url(){
$uri = $_SERVER['REQUEST_URI'];
$uri = trim($uri, '/');
$this->remove_query_or_hash($uri);
$exploded_uri = explode('/', $uri);
$this->controller = Util::istruthy_or($exploded_uri[0], 'Main').'Controller';
$this->action = Util::istruthy_or($exploded_uri[1], 'index');
$this->args = array_slice($exploded_uri, 2);
}
private function remove_query_or_hash(&$uri){
$query = strpos($uri, '?');
$hash = strpos($uri, '#');
if($query!==FALSE||$hash!==FALSE){
$idx =
index.php:
require 'Classes/Autoloader.php';
Autoloader::start();
Session::start();
new Bootstrap();Bootstrap.php
```
class Bootstrap {
private $controller = null,
$action = null,
$args = null;
public function __construct() {
$this->manage_url();
if($this->page_exist()){
$this->request();
} else {
$error = new ErrorController();
$error->error404();
}
}
private function request(){
$controller = new $this->controller();
if(!$this->args){ $controller->{$this->action}(); }
else{
$count_args = count($this->args);
switch ($count_args) {
case 1:
$controller->{$this->action}($this->args[0]);
break;
case 2:
$controller->{$this->action}($this->args[0], $this->args[1]);
break;
case 3:
$controller->{$this->action}($this->args[0], $this->args[1], $this->args[2]);
break;
case 4:
$controller->{$this->action}($this->args[0], $this->args[1], $this->args[2], $this->args[3]);
break;
}
}
}
/**
* Get controller, action and parametes
*/
private function manage_url(){
$uri = $_SERVER['REQUEST_URI'];
$uri = trim($uri, '/');
$this->remove_query_or_hash($uri);
$exploded_uri = explode('/', $uri);
$this->controller = Util::istruthy_or($exploded_uri[0], 'Main').'Controller';
$this->action = Util::istruthy_or($exploded_uri[1], 'index');
$this->args = array_slice($exploded_uri, 2);
}
private function remove_query_or_hash(&$uri){
$query = strpos($uri, '?');
$hash = strpos($uri, '#');
if($query!==FALSE||$hash!==FALSE){
$idx =
Solution
MVC is a concept that stems from good OOP practices. It's about seperating your application to three different parts: InputControllers, ProcessingModel and OutputView.
What you are describing is the bootstrap page, which, at least from my perspective should not be a class.
Here's my approach to this:
index.php
bootstrap.php
What you are describing is the bootstrap page, which, at least from my perspective should not be a class.
Here's my approach to this:
index.php
<?php
require "../bootstrap.php";bootstrap.php
route($uri); //$route is a Route object which tells us
//what controller to use, what are the parameters, etc.
$controllerClass = $route->getControllerClass();
$controllerAction = $route->getControllerAction();
$controller = new $controllerClass($request);
//Where $request is the Request object, containing the URI, GET, POST, COOKIES, etc.
$viewParams = call_user_func_array([$controller, $controllerAction], $route->getParameters());
//$route->getParameters() is an array of parameters from the route.
//call_user_func_array will transform that array into actual arguments to pass in.
$viewClass = $viewParams["class"];
$viewAction = $viewParams["action"];
$view = new $viewClass($request);
echo $view->render($viewParams);Code Snippets
<?php
require "../bootstrap.php";<?php
//Pseudo-code ahead:
require autoloader;
start router;
add router rules (either from file, or actually in the code)
$route = $router->route($uri); //$route is a Route object which tells us
//what controller to use, what are the parameters, etc.
$controllerClass = $route->getControllerClass();
$controllerAction = $route->getControllerAction();
$controller = new $controllerClass($request);
//Where $request is the Request object, containing the URI, GET, POST, COOKIES, etc.
$viewParams = call_user_func_array([$controller, $controllerAction], $route->getParameters());
//$route->getParameters() is an array of parameters from the route.
//call_user_func_array will transform that array into actual arguments to pass in.
$viewClass = $viewParams["class"];
$viewAction = $viewParams["action"];
$view = new $viewClass($request);
echo $view->render($viewParams);Context
StackExchange Code Review Q#47706, answer score: 3
Revisions (0)
No revisions yet.