Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require("config.php");
- /**
- * Router Class - Framework
- * @author Erwin Okken <erwtje@gmail.com>
- * @version 1.0
- */
- class Router
- {
- /**
- * @var string path
- */
- var $path;
- /**
- * @var string page
- */
- var $page;
- /**
- * @var string action
- */
- var $action;
- /**
- * @var array optional parameters
- */
- var $parameters = array();
- /**
- * Constructor gets the current page, action and optional parameters.
- * Redirects to dispatch function.
- */
- public function __construct()
- {
- $requestURI = explode('/', $_SERVER['REQUEST_URI']);
- $scriptName = explode('/',$_SERVER['SCRIPT_NAME']);
- for($i=0;$i < sizeof($scriptName);$i++)
- {
- if(strtolower($requestURI[$i]) == strtolower($scriptName[$i]))
- {
- $this->path .= $requestURI[$i] . "/";
- unset($requestURI[$i]);
- }
- }
- // dit kan later weg:
- define("PATH",$this->path);
- $url = array_values($requestURI);
- for($i=0;$i < sizeof($url);$i++){
- if($i==0){
- $this->page = $url[$i];
- }
- elseif($i==1){
- $this->action = $url[$i];
- }
- else{
- $this->parameters[] = $url[$i];
- }
- }
- $this->page = (empty($this->page)) ? "home" : $this->page;
- $this->action = (empty($this->action)) ? "news" : $this->action;
- switch($this->page){
- case "home":
- case "guestbook":
- try
- {
- $this->dispatch();
- }
- catch(Exception $e)
- {
- echo $e->getMessage();
- }
- break;
- default:
- $this->page = "home";
- $this->action = "404";
- $this->dispatch();
- break;
- }
- }
- /**
- * Dispatch method checks the called controller and method and redirects it.
- * @throw Exception When controller doesn't exist
- */
- public function dispatch()
- {
- if(file_exists("controllers/" . $this->page . ".class.php"))
- {
- require_once("controllers/" . $this->page . ".class.php");
- }
- else
- {
- throw new Exception("Cannot find the controller file: " . $this->page);
- }
- //$controllername = ucfirst($this->page) . "_Controller";
- //$controller = new $controllername();
- if(class_exists(ucfirst($this->page) . "_Controller"))
- {
- // Onderstaande methode werkt, maar STATIC.
- // call_user_func_array(array(ucfirst($this->page) . '_Controller', $this->action),array());
- $controllername = $this->page . "_Controller";
- $action = $this->action;
- $curcontroller = new $controllername;
- if(method_exists($controllername, $this->action))
- {
- $curcontroller->$action();
- }
- else
- {
- throw new Exception("Methode niet gevonden: " . $action);
- }
- }
- else
- {
- throw new Exception("Cannot find controller class: " . $this->page);
- }
- }
- }
- $Router = new Router();
- ?>
Add Comment
Please, Sign In to add comment