Skip to content

Commit

Permalink
寻找控制器支持路由功能
Browse files Browse the repository at this point in the history
  • Loading branch information
breath-co2 committed Feb 5, 2013
1 parent 2794357 commit a5f1375
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 13 deletions.
74 changes: 64 additions & 10 deletions core/classes/core.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,33 @@ public static function execute($uri)

if ($found)
{
require $found['file'];

if ($found['ns']=='team_library'||$found['ns']=='project')
if (isset($found['route']))
{
$class_name = $found['class'];
$class_name = $found['class'];
$class_exists = class_exists($class_name, true);
$arguments = array();
if (isset($found['route']['action']) && $found['route']['action'])
{
$arguments[] = $found['route']['action'];
}
}
else
{
$class_name = str_replace('.', '_', $found['ns']).'_'.$found['class'];
require $found['file'];

if ($found['ns']=='team_library' || $found['ns']=='project')
{
$class_name = $found['class'];
}
else
{
$class_name = str_replace('.', '_', $found['ns']) . '_' . $found['class'];
}

$class_exists = class_exists($class_name, false);
}

if (class_exists($class_name,false))
if ($class_exists)
{

$controller = new $class_name();
Expand All @@ -472,13 +487,13 @@ public static function execute($uri)
$action = 'index';
}

$action_name = 'action_'.$action;
$action_name = 'action_' . $action;

if (!method_exists($controller, $action_name))
{
if ($action_name!='action_default' && method_exists($controller, 'action_default'))
{
$action_name='action_default';
$action_name = 'action_default';
}
elseif (method_exists($controller, '__call'))
{
Expand All @@ -494,12 +509,13 @@ public static function execute($uri)
throw new Exception(__('Page Not Found'), 404);
}
}
else
elseif ($arguments)
{
array_shift($arguments);
}

$ispublicmethod = new ReflectionMethod($controller, $action_name);

if (!$ispublicmethod->isPublic())
{
Core::rm_controoler($controller);
Expand All @@ -508,10 +524,23 @@ public static function execute($uri)
}
unset($ispublicmethod);

if (isset($found['route']))
{
# 设置Route参数
foreach ($found['route'] as $k => $v)
{
$controller->$k = $v;
}
}
else
{
$controller->ids = $found['ids'];
}

# 将参数传递给控制器
$controller->action = $action_name;
$controller->controller = $found['class'];
$controller->ids = $found['ids'];


if (IS_SYSTEM_MODE)
{
Expand Down Expand Up @@ -599,6 +628,31 @@ protected function find_controller($uri)
$uri = substr($uri, 0, -strlen(Core::$core_config['url_suffix']));
}

if (!IS_SYSTEM_MODE && isset(Core::$config['route']) && Core::$config['route'])
{
# 有路由配置,首先根据路由配置查询控制器
$found_route = Route::get($uri);

if ($found_route)
{
if (!isset($found_route['controller']) || !$found_route['controller'])
{
if (IS_DEBUG)Core::debug()->error('The route not match controller');
Core::show_404();
}

return array
(
'class' => 'Controller_' . $found_route['controller'],
'route' => $found_route,
);
}
else
{
unset($found_route);
}
}

if ($uri!='/')
{
$uri_arr = explode('/', strtolower($uri));
Expand Down
5 changes: 2 additions & 3 deletions core/classes/route.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class Core_Route
{

// Defines the pattern of a <segment>
const REGEX_KEY = '<([a-zA-Z0-9_]++)>';

Expand Down Expand Up @@ -51,7 +50,7 @@ public function get($pathinfo)
if (!isset(Route::$regex[Core::$project]))
{
# 构造路由正则
$this->init_regex();
Route::init_regex();
}

# 当前Route
Expand Down Expand Up @@ -91,7 +90,7 @@ public static function uri(array $params = null)
if (!isset(Route::$regex[Core::$project]))
{
# 构造路由正则
$this->init_regex();
Route::init_regex();
}

$current_route = Core::config('route.'.Route::$current_route);
Expand Down

0 comments on commit a5f1375

Please sign in to comment.