Last Updated: February 25, 2016
·
4.27K
· 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');

    }

}

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside