Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- *
- *
- * //test URLs
- * $urlCnt[0] = "/information/location";
- * controller => information, action => location
- * $urlCnt[1] = "/webdesign/show/1";
- * $urlCnt[2] = "/information/show/3/why-you-should-choose-us";
- * $urlCnt[3] = "/information/delete/3/becauseItsOld";
- * $urlCnt[4] = "/information";
- * controller => information, action => $standartAction
- * @author bs
- *
- */
- class Router
- {
- static private $_instance;
- private $routes = array();
- private $url;
- private $standardAction = "index";
- private $standardController = "index";
- public function __construct($url)
- {
- $this->url = "/" . $url;
- }
- public static function getInstance($url = "")
- {
- if ( !(self::$_instance instanceof self) )
- {
- self::$_instance = new self($url);
- }
- return self::$_instance;
- }
- /**
- *
- * returns all routes in the router
- */
- public function getRoutes()
- {
- return $this->routes;
- }
- /*
- * returns the request
- */
- public function getRequest()
- {
- return $this->generateOutcome();
- }
- /**
- *
- * sets the standart controller
- * //standard = index
- * @param unknown_type $controller
- */
- public function setStandardController($controller)
- {
- $this->standardController = $controller;
- }
- /**
- *
- * sets the standart action
- * //standard = index
- * @param unknown_type $action
- */
- public function setStandardAction($action)
- {
- $this->standardAction = $action;
- }
- /**
- *
- * adds routes to the router
- * @param unknown_type $route
- */
- public function addRoute($route = array() )
- {
- $this->routes[] = $route;
- }
- /**
- *
- * creates an array that will output something like:
- */
- private function generateOutcome()
- {
- //check if the domain is just the index...
- if ($this->url != "/")
- {
- //some empty stuff for later
- $params = array();
- $regex = "";
- $routesCNT = array();
- //prepare the regex to parse the urls
- foreach($this->routes as $route)
- {
- $ga = preg_split("/\//", $route[0] );
- foreach($ga as $expr)
- {
- //check if the name starts with a : - if so than it will be a variable that is needed as an array index
- //important for later stuff, as controller, action, params always be there as an index
- if( strpos($expr, ":") === 0 )
- {
- //:controller or :action found or whatever -> a regex need to be used
- $regex .= "\/[a-zA-Z-0-9]+";
- }
- else
- {
- //not found therefore what is written is the requirement
- $regex .= "\/" . $expr;
- }
- }
- //put the route back together but this time with the regular expression
- $rPattern[0] = $route[0];
- $rPattern[1] = $route[1];
- $rPattern[2] = $regex;
- $routesCNT[] = $rPattern;
- $regex = null;
- }
- //build the final array for the url to usefull stuff
- $processor = array();
- $finalArray = array();
- $i = 1;
- foreach($routesCNT as $route)
- {
- if ( preg_match("/^" . $route[2] . "\z/i", $this->url ) == 1 )
- {
- //split the url into pieces for the final array
- $urlValues = explode("/", $this->url);
- $routeLength = count($route[1]);
- foreach( $route[1] as $name => $val )
- {
- if ( $routeLength > 2 && $routeLength == (count($urlValues) - 1))
- {
- //make sure not to override the index
- if( $val == "")
- {
- $processor[$name] = $urlValues[$i];
- }
- else
- {
- $processor[$name] = $val;
- }
- }
- else
- {
- $processor['controller'] = $urlValues[1];
- $processor['action'] = $urlValues[2];
- }
- $finalArray = $processor;
- $i++;
- }
- }
- else
- {
- //here is the $route[1]['params'] regquired!!!
- //split the url into pieces for the final array
- $urlValues = explode("/", $this->url);
- $processor['controller'] = $urlValues[1];
- //check if there is no action set
- if ( count($urlValues) <= 2)
- {
- $processor['action'] = $this->standardAction;
- }
- else if(empty($urlValues))
- {
- $processor['controller'] = $this->standardController;
- $processor['action'] = $this->standardAction;
- }
- else
- {
- $processor['action'] = $urlValues[2];
- $baseLength = strlen($urlValues[1] . "/" . $urlValues[2] );
- $newUrl = substr($this->url, $baseLength + 1);
- $params = explode("/", $newUrl);
- array_shift( $params );
- $processor['params'] = $params;
- }
- $finalArray = $processor;
- }
- }
- }
- else
- {
- //if the requested path is just / - it means basically its the index file therefore show the standard
- $processor['controller'] = $this->standardController;
- $processor['action'] = $this->standardAction;
- $finalArray = $processor;
- }
- return new ArrayObject( $finalArray, 2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement