Skip to content

Instantly share code, notes, and snippets.

@KlonD90
Created July 3, 2012 05:24
Show Gist options
  • Save KlonD90/3037869 to your computer and use it in GitHub Desktop.
Save KlonD90/3037869 to your computer and use it in GitHub Desktop.
router version 2
<?php
class service {
public function __get($name) {
if ($this->$name)
return $name;
else {
require_once 'controller/'.$name.'.php';
$this->$name = new $name();
return $this->$name;
}
}
}
$service = new service();
$method = $_SERVER['REQUEST_METHOD'];
$url = $_SERVER['REQUEST_URI'];
$urlMapping = array(
'GET' => array(
'/' => array(
'controller' => 'PostController#list'
),
'/page/:page' => array(
'controller' => 'PostController#list'
),
'/post/:id' => array(
'controller' => 'PostController#post'
),
'/stream/list' => array(
'controller' => 'StreamController#list'
),
'/stream/:id' => array(
'controller' => 'StreamController#channel'
),
'/user/:login' => array(
'controller' => 'UserController#profile'
),
'/post/add' => array(
'controller' => 'PostController#form',
'filter' => $isEditor || $isAdmin
),
'/schedule/add' => array(
'controller' => 'ScheduleController#add',
'params' => array(
'name','date'
),
// 'filter' => $isAdmin || $isStreamer
)
),
'POST' => array(
'/user/edit' => array(
'controller' => 'UserController#edit',
'params' => array('pass'),
'filter' => $isLogin
),
'/user/:id/ban' => array(
'controlller' => 'UserController#ban'
),
'/post/add' => array(
'controller' => 'PostController#add',
'params' => array('title', 'text','publishDate'),
'filter' => $isEditor || $isAdmin
),
'/post/:id/edit' => array(
'controller' => 'PostController#edit',
'params' => array('title','text','publishDate'),
'filter' => $isEditor || $isAdmin
),
'/stream/:id/edit' => array(
'controller' => 'StreamController#edit',
'params' => array('title', 'key','service'),
'filter' => $isStreamer || $isAdmin
),
'/stream/add' => array(
'controller' => 'StreamController#add',
'params' => array('title', 'key','service'),
'filter' => $isStreamer || $isAdmin
),
'/stream/:id/delete' => array(
'controller' => 'StreamController#delete',
'filter' => $isStreamer || $isAdmin
),
'/comment/add' => array(
'controller' => 'CommentController#add',
'params' => array('text','material'),
'filter' => $isLogin
),
'/comment/:id/delete' => array(
'controller' => 'CommentController#delete',
'filter' => $isAdmin || $isEditor
),
'/schedule/add' => array(
'controller' => 'ScheduleController#add',
'params' => array(
'name','date'
),
'filter' => $isAdmin || $isStreamer
),
'/schedule/:id/delete' => array(
'controller' => 'ScheduleController#delete',
'params' => array(
'name','date'
),
'filter' => $isAdmin || $isStreamer
)
)
);
$mapping = $urlMapping[$method];
$routeData = proccessRouting($mapping, $url);
if (!is_array($routeData))
pageNotFound();
if(!$routeData['filter'])
accessDenied();
$class = $routeData['class'];
$obj = $service->$class;
call_user_func_array(array($obj, $routeData['method']), $routeData['params']);
function proccessRouting($mapping, $url) {
$url = explode('/', $url);
$maps = array_keys($mapping);
$urlSize = count($url);
$routing = array();
foreach ($maps as $map) {
$mapArray = explode('/', $map);
$equal = false;
if (count($mapArray) === $urlSize || strpos($map, '/*') !== false) {
$equal = true;
$star = false;
$j=0;
for ($i = 1; $i < count($mapArray); $i++) {
$j++;
$mapElement = $mapArray[$i];
while($star==true && isset($url[$j])){
if ($mapElement == $url[$j]){
$star=false;
continue;
}
else
$j++;
}
if($star == true){
$equal = false;
}
if ($mapElement == '*'){
$star = true;
continue;
}
if (substr($mapElement, 0, 1) == ':')
continue;
if ($mapElement != $url[$j]) {
$equal = false;
break;
}
}
if ($equal)
$routing[] = $map;
}
}
if (count($routing) == 0)
return false;
$route = array_reduce($routing, function($best, $route) {
$stars_route = substr_count($route, '*');
$stars_best = substr_count($best, '*');
if( ($stars_route || $stars_best) && $stars_route != $stars_best){
if($stars_route == 0 && $stars_best)
return $stars_route;
if($stars_best == 0 && $stars_route)
return $stars_best;
if ($stars_best < $stars_route)
return $route;
else
return $best;
}
else {
if (substr_count($route, ':') < substr_count($best, ':'))
return $route;
else
return $best;
}
}, $routing[0]);
$params = array();
$routeArray = explode('/', $route);
$star = false;
$j=1;
for ($i = 1; $i < count($routeArray); $i++) {
if($routeArray[$i] == '*'){
$next= $i + 1;
$star_param=array();
while($routeArray[$next] != $url[$j] )
$star_param[]=$url[$j++];
$params[]=$star_param;
$j++;
$i++;
continue;
}
if (substr($routeArray[$i], 0, 1) == ':')
$params[] = $url[$j];
$j++;
}
$needParams = $mapping[$route]['params'];
if ($needParams)
foreach ($needParams as $postParam) {
$params[] = $_POST[$postParam];
}
$controller = explode('#', $mapping[$route]['controller']);
$class = $controller[0];
$method = $controller[1];
$filter = isset($mapping[$route]['filter']) ? $mapping[$route]['filter'] : true;
return array('class' => $class, 'method' => $method, 'params' => $params, 'filter' => $filter);
}
function pageNotFound() {
echo 'not found';
exit();
}
function accessDenied() {
echo 'access denied';
exit();
}
function redirect($url) {
header('Location: '.$url);
exit();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment