Example #1
0
 /**
  * Route
  * Takes the request and calls the appropriate controller/action
  * Request data is now taken directly from $_SERVER instead of being
  * passed into the router
  */
 static function route()
 {
     // check to see whether the request was a redirect
     if (empty($_SERVER['REDIRECT_URL'])) {
         // index.php was called directly instead of being rewritten
         // so we call pagesController->home
         $controllerName = 'pagesController';
         $actionName = 'home';
     } else {
         $requestURI = $_SERVER['REDIRECT_URL'];
         $request = Router::parseRequest($requestURI);
     }
     // get the controller name from the request array (if not set)
     if (empty($controllerName)) {
         $controllerName = array_shift($request) . 'Controller';
     }
     // get the action name from the request array (if not set)
     if (empty($actionName)) {
         $actionName = array_shift($request);
     }
     // throw an exception if the controller doesn't exist
     if (!class_exists($controllerName)) {
         throw new Exception('Class ' . $controllerName . ' does not exist.');
     }
     // create an instance of the required controller class
     // (passing in the name of the action we're about to call)
     $controller = new $controllerName($actionName);
     // pagesController is a special case because the methods are
     // created dynamically based on the action name passed to the
     // class constructor. The anonymous function assigned to the
     // dynamic property fails method_exists() and is_callable(),
     // so we have to call it separately using call_user_func().
     // This also makes it more difficult to throw exceptions that
     // trap bad page names.
     if ($controllerName == 'pagesController') {
         call_user_func($controller->{$actionName});
     } else {
         // throw an exception if the action doesn't exist
         if (!method_exists($controllerName, $actionName)) {
             throw new Exception('Method ' . $controllerName . '->' . $actionName . ' does not exist.');
         }
         // call the controller action method and pass in request params
         $controller->{$actionName}($request);
     }
 }
Example #2
0
 static function route($requestURI)
 {
     $request = Router::parseRequest($requestURI);
     // get the controller name from the request array
     $controllerName = array_shift($request) . 'Controller';
     // get the action name from the request array
     $actionName = array_shift($request);
     // throw an exception if the controller doesn't exist
     if (!class_exists($controllerName)) {
         throw new Exception('Class ' . $controllerName . ' does not exist.');
     }
     // create an instance of the required controller class
     // (passing in the name of the action we're about to call)
     $controller = new $controllerName($actionName);
     // throw an exception if the controller doesn't exist
     if (!method_exists($controllerName, $actionName)) {
         throw new Exception('Method ' . $controllerName . '->' . $actionName . ' does not exist.');
     }
     // call the controller action method and pass in request params
     $controller->{$actionName}($request);
 }