Last Updated: February 25, 2016
·
3.977K
· chris15001900

MVC Router in php

class Router {

    public static function route(Request $request) {

        $controllerName = $request->getController() . 'Controller';
        $methodName = $request->getMethod();    
        $args = $request->getArgs();    

        if(is_readable(CONTROLLERS . $controllerName . '.php')) {

            $controller = new $controllerName;  

            $method = (is_callable(array($controller, $methodName))) ? $methodName : 'main';        

            if(!empty($args)) {

                call_user_func_array(array($controller, $method), $args);                   

            } else {

                call_user_func(array($controller, $method));                    

            }

            return;

        }

        throw new Exception('wrong_route');

    }

}