Is this an efficient way to handle page requests in MVC?index.php:require 'Classes/Autoloader.php';Autoloader::start();Session::start();new Bootstrap();Bootstrap.phpclass 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 =  $query < $hash ? $hash : $query;            $uri = substr($uri, 0, $idx);        }    }    private function page_exist(){        $controller = class_exists($this->controller);        $method = method_exists($this->controller, $this->action);        if(!$controller||!$method){            return FALSE;        }        return TRUE;    }}Util istruthy_or():public static function istruthy_or(&$var, $val = NULL){    return isset($val) ? ($var ? $var : $val) : ($var ? $var : NULL);}
Handling page requests in MVC
php;mvc
Your approach is great as long as you are the only one working with this code and all your uri ideally resolve to /controller/action/args. Problems will start when your client asks for a friendly uri like /my-section/my-page or someone else will have to maintain your code.To address the problem with uri you might need custom routing (rules for non-standard uri routing that apply before the standard /controller/action/args). For examples of such rules you should check how it's done in different frameworks and find the one that fits your liking. Here's how it's done in CodeIgniter.Second issue is a bit less clear. By looking at your code:Autoloader::start();Session::start();new Bootstrap();One will never realise where exactly is the job gets done. You could do it a lot clearer by chaning, for example, to this:$bootstrap = new Bootstrap();$bootstrap->process_request($_SERVER['REQUEST_URI']);But that's not the only issue. Next you call manage_url(), which does not get any paramteter and therefore is untestable. You could make it testable by calling it with a parameter manage_url($uri) - this way you can call it in a test with different uris and see how it handles it's job. Then again, what is it's job? It does multiple things, thats why such a non-telling name.You could make your code a lot more clear for future developers (including yourself) by making each function do one task. For example:public function process_request($uri){    // first you want a function to clear uri from unwanted parts    $uri = $this->prepare_uri($uri);     // then you want to apply custom routing I described earlier    if (!$this->apply_custom_routing($uri)) {        // if none matched, apply default routing of your manage_url()'s last part        if (!$this->apply_default_routing($uri)) {            // none valid routing found - 404            $this->apply_error_routing();        }    }    // now you know request details and can proceed    $this->call_controller();}Note that it's now pretty clear what each function does and each of them is at least somewhat testable, because their dependencies are clear.Also note, that you don't need that ugly switch statement in request(). There is a function for that type of situations (when you don't know amount of arguments beforehand):call_user_func_array(array($this->controller, $this->action), $this->args);