mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-03 16:51:40 +09:00
Router 기능 추가
This commit is contained in:
parent
5c9d88901f
commit
623ef0e36b
4 changed files with 255 additions and 34 deletions
36
.htaccess
36
.htaccess
|
|
@ -15,37 +15,7 @@ RewriteRule ^(.+)/files/(member_extra_info|attach|cache|faceOff)/(.*) ./files/$2
|
|||
RewriteCond %{SCRIPT_FILENAME} !-f
|
||||
RewriteRule ^(.+)/(files|modules|widgets|widgetstyles|layouts|m.layouts|addons)/(.*) ./$2/$3 [L]
|
||||
|
||||
# rss , blogAPI
|
||||
RewriteRule ^(rss|atom)$ ./index.php?module=rss&act=$1 [L]
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/(rss|atom|api)$ ./index.php?mid=$1&act=$2 [L]
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/(rss|atom|api)$ ./index.php?vid=$1&mid=$2&act=$3 [L]
|
||||
|
||||
# trackback
|
||||
RewriteRule ^([0-9]+)/(.+)/trackback$ ./index.php?document_srl=$1&key=$2&act=trackback [L]
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$ ./index.php?mid=$1&document_srl=$2&key=$3&act=trackback [L]
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$ ./index.php?vid=$1&document_srl=$2&key=$3&act=trackback [L]
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback$ ./index.php?vid=$1&mid=$2&document_srl=$3&key=$4&act=trackback [L]
|
||||
|
||||
# document permanent link
|
||||
RewriteRule ^([0-9]+)$ ./index.php?document_srl=$1 [L,QSA]
|
||||
|
||||
# mid link
|
||||
RewriteCond %{SCRIPT_FILENAME} !-d
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/?$ ./index.php?mid=$1 [L,QSA]
|
||||
# mid + document link
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/([0-9]+)$ ./index.php?mid=$1&document_srl=$2 [L,QSA]
|
||||
|
||||
# vid + mid link
|
||||
RewriteCond %{SCRIPT_FILENAME} !-d
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/?$ ./index.php?vid=$1&mid=$2 [L,QSA]
|
||||
# vid + mid + document link
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)$ ./index.php?vid=$1&mid=$2&document_srl=$3 [L,QSA]
|
||||
|
||||
# mid + entry title
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/entry/(.+)$ ./index.php?mid=$1&entry=$2 [L,QSA]
|
||||
# vid + mid + entry title
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/entry/(.+)$ ./index.php?vid=$1&mid=$2&entry=$3 [L,QSA]
|
||||
|
||||
#shop / vid / [category|product] / identifier
|
||||
# router
|
||||
RewriteCond %{SCRIPT_FILENAME} !-f
|
||||
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_\.-]+)$ ./index.php?act=route&vid=$1&type=$2&identifier=$3 [L,QSA]
|
||||
RewriteCond %{SCRIPT_FILENAME} !-d
|
||||
RewriteRule ^(.*)$ ./index.php [L]
|
||||
|
|
|
|||
|
|
@ -366,6 +366,13 @@ class Context
|
|||
// check if using rewrite module
|
||||
$this->allow_rewrite = ($this->db_info->use_rewrite == 'Y' ? TRUE : FALSE);
|
||||
|
||||
// If using rewrite module, initializes router
|
||||
if($this->allow_rewrite)
|
||||
{
|
||||
$oRouter = Router::getInstance();
|
||||
$oRouter->proc();
|
||||
}
|
||||
|
||||
// set locations for javascript use
|
||||
if($_SERVER['REQUEST_METHOD'] == 'GET')
|
||||
{
|
||||
|
|
@ -1568,7 +1575,10 @@ class Context
|
|||
'act.document_srl.key.mid.vid' => ($act == 'trackback') ? "$vid/$mid/$srl/$key/$act" : ''
|
||||
);
|
||||
|
||||
$query = $target_map[$target];
|
||||
$oRouter = Router::getInstance();
|
||||
$oRouter->setMap($target_map);
|
||||
|
||||
$query = $oRouter->makePrettyUrl($target);
|
||||
}
|
||||
|
||||
if(!$query)
|
||||
|
|
|
|||
240
classes/router/Router.class.php
Normal file
240
classes/router/Router.class.php
Normal file
|
|
@ -0,0 +1,240 @@
|
|||
<?php
|
||||
/**
|
||||
* @file Router.class.php
|
||||
* @brief Parses URIs and determines routing
|
||||
* @author FunnyXE (admin@funnyxe.com)
|
||||
*/
|
||||
class Router
|
||||
{
|
||||
/**
|
||||
* Singleton
|
||||
* @var object
|
||||
*/
|
||||
private static $theInstance = null;
|
||||
|
||||
/**
|
||||
* URI Segments
|
||||
* @var array
|
||||
*/
|
||||
private static $segments = array();
|
||||
|
||||
/**
|
||||
* Routes
|
||||
* @var array
|
||||
*/
|
||||
private $routes = array();
|
||||
|
||||
/**
|
||||
* Rewrite map
|
||||
* @var array
|
||||
*/
|
||||
private $rewrite_map = array();
|
||||
|
||||
/**
|
||||
* @brief returns static context object (Singleton). It's to use Router without declaration of an object
|
||||
* @return object Instance
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if(!isset(self::$theInstance))
|
||||
{
|
||||
self::$theInstance = new Router();
|
||||
}
|
||||
|
||||
return self::$theInstance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Applys routes.
|
||||
* @see This function should be called only once
|
||||
* @return void
|
||||
*/
|
||||
public function proc()
|
||||
{
|
||||
$uri = $_SERVER['REQUEST_URI'];
|
||||
|
||||
if (stripos($uri, $_SERVER['SCRIPT_NAME']) === 0)
|
||||
{
|
||||
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
|
||||
}
|
||||
elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
|
||||
{
|
||||
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
|
||||
}
|
||||
|
||||
if ($uri == '/' || empty($uri))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$path = parse_url($uri, PHP_URL_PATH);
|
||||
|
||||
// Do some final cleaning of the URI and return it
|
||||
$path = str_replace(array('//', '../'), '/', trim($path, '/'));
|
||||
|
||||
if(strlen($path) > 0)
|
||||
{
|
||||
self::$segments = explode('/', $path);
|
||||
|
||||
// Remove the meanless segment
|
||||
unset(self::$segments[0]);
|
||||
}
|
||||
|
||||
$self = Router::getInstance();
|
||||
|
||||
// Set default routes
|
||||
$self->routes = array(
|
||||
// rss , blogAPI
|
||||
'(rss|atom)' => array('module' => 'rss', 'act' => '$1'),
|
||||
'([a-zA-Z0-9_]+)/(rss|atom|api)' => array('mid' => '$1', 'act' => '$2'),
|
||||
'([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/(rss|atom|api)' => array('vid' => '$1', 'mid' => '$2', 'act' => '$3'),
|
||||
// trackback
|
||||
'([0-9]+)/(.+)/trackback' => array('document_srl' => '$1', 'key' => '$2', 'act' => 'trackback'),
|
||||
'([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback' => array('mid' => '$1', 'document_srl' => '$2', 'key' => '$3', 'act' => 'trackback'),
|
||||
'([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)/(.+)/trackback' => array('vid' => '$1', 'mid' => '$2', 'document_srl' => '$3' , 'key' => '$4', 'act' => 'trackback'),
|
||||
// mid
|
||||
'([a-zA-Z0-9_]+)/?' => array('mid' => '$1'),
|
||||
// mid + document_srl
|
||||
'([a-zA-Z0-9_]+)/([0-9]+)' => array('mid' => '$1', 'document_srl' => '$2'),
|
||||
// vid + mid
|
||||
'([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/' => array('vid' => '$1', 'mid' => '$2'),
|
||||
// vid + mid + document_srl
|
||||
'([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)?' => array('vid' => '$1', 'mid' => '$2', 'document_srl' => '$3'),
|
||||
// document_srl
|
||||
'([0-9]+)' => array('document_srl' => '$1'),
|
||||
// mid + entry title
|
||||
'([a-zA-Z0-9_]+)/entry/(.+)' => array('mid' => '$1', 'entry' => '$2'),
|
||||
// vid + mid + entry title
|
||||
'([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/entry/(.+)' => array('vid' => '$1', 'mid' => '$2', 'entry' => '$3'),
|
||||
// shop / vid / [category|product] / identifier
|
||||
'([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_\.-]+)' => array('act' => 'route', 'vid' => '$1', 'type' => '$2', 'identifier'=> '$3'),
|
||||
);
|
||||
|
||||
if(isset($self->routes[$path]))
|
||||
{
|
||||
foreach($self->routes[$path] as $key => $val)
|
||||
{
|
||||
$val = preg_replace('#^\$([0-9]+)$#e', '\$matches[$1]', $val);
|
||||
|
||||
Context::set($key, $val, TRUE);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Apply routes
|
||||
foreach($self->routes as $regex => $query)
|
||||
{
|
||||
if(preg_match('#^' . $regex . '$#', $path, $matches))
|
||||
{
|
||||
foreach($query as $key => $val)
|
||||
{
|
||||
$val = preg_replace('#^\$([0-9]+)$#e', '\$matches[$1]', $val);
|
||||
|
||||
Context::set($key, $val, TRUE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Add a rewrite map(s)
|
||||
* @param array $map
|
||||
* @return void
|
||||
*/
|
||||
public function setMap($map)
|
||||
{
|
||||
$self = Router::getInstance();
|
||||
$self->rewrite_map = array_merge($self->rewrite_map, $map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Add a route
|
||||
* @param string $target
|
||||
* @param array $query
|
||||
* @return void
|
||||
*/
|
||||
public function add($target, $query)
|
||||
{
|
||||
$self = Router::getInstance();
|
||||
$self->routes[$target] = $query;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Add multiple routes
|
||||
* @param array $routes
|
||||
* @return void
|
||||
*/
|
||||
public function adds($routes)
|
||||
{
|
||||
$self = Router::getInstance();
|
||||
$self->routes = array_merge($self->routes, $routes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get segment from request uri
|
||||
* @param int $index
|
||||
* @return string
|
||||
*/
|
||||
public function getSegment($index)
|
||||
{
|
||||
$self = Router::getInstance();
|
||||
return $self->segments[$index];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get segment from request uri
|
||||
* @param int $index
|
||||
* @return string
|
||||
*/
|
||||
public function getSegments()
|
||||
{
|
||||
$self = Router::getInstance();
|
||||
return $self->segments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get route info
|
||||
* @param string $regex
|
||||
* @return array
|
||||
*/
|
||||
public function getRoute($regex)
|
||||
{
|
||||
$self = Router::getInstance();
|
||||
return $self->routes[$regex];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get routes list
|
||||
* @return array
|
||||
*/
|
||||
public function getRoutes()
|
||||
{
|
||||
$self = Router::getInstance();
|
||||
return $self->routes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get routes list
|
||||
* @param string $regex
|
||||
* @return boolean
|
||||
*/
|
||||
public function isExistsRoute($regex)
|
||||
{
|
||||
$self = Router::getInstance();
|
||||
return isset($self->routes[$regex]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Makes shortten url
|
||||
* @param string $regex
|
||||
* @return string
|
||||
*/
|
||||
public function makePrettyUrl($regex)
|
||||
{
|
||||
$self = Router::getInstance();
|
||||
return $self->rewrite_map[$regex];
|
||||
}
|
||||
}
|
||||
|
|
@ -299,6 +299,7 @@ if(!defined('__XE_LOADED_CLASS__'))
|
|||
require(_XE_PATH_ . 'classes/xml/XmlJsFilter.class.php');
|
||||
require(_XE_PATH_ . 'classes/xml/XmlLangParser.class.php');
|
||||
require(_XE_PATH_ . 'classes/cache/CacheHandler.class.php');
|
||||
require(_XE_PATH_ . 'classes/router/Router.class.php');
|
||||
require(_XE_PATH_ . 'classes/context/Context.class.php');
|
||||
require(_XE_PATH_ . 'classes/db/DB.class.php');
|
||||
require(_XE_PATH_ . 'classes/file/FileHandler.class.php');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue