执行指定URI的控制器
null Core_Core::execute( string $uri )
参数列表
参数 类型 描述 默认值 $uri
string
$uri
public static function execute($uri)
{
$found = Core::find_controller($uri);
if ($found)
{
if (isset($found['route']))
{
$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
{
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)
{
$controller = new $class_name();
Controller::$controllers[] = $controller;
# 是否有必要将action从$arguments中移出
$need_shift_action = false;
$arguments = $found['args'];
if ($arguments)
{
$action = current($arguments);
if (0===strlen($action))
{
$action = 'default';
}
else
{
$need_shift_action = true;
}
}
else
{
$action = 'index';
}
$action_name = 'action_' . $action;
if (!method_exists($controller, $action_name))
{
if ($action_name!='action_default' && method_exists($controller, 'action_default'))
{
$action_name = 'action_default';
}
elseif ($action_name!='' && (!$arguments || $arguments===array('')) && method_exists($controller, 'action_index'))
{
$action_name = 'action_index';
}
elseif (method_exists($controller, '__call'))
{
$controller->__call($action_name, $arguments);
Core::rm_controoler($controller);
return;
}
else
{
Core::rm_controoler($controller);
throw new Exception(__('Page Not Found'), 404);
}
}
elseif ($need_shift_action)
{
array_shift($arguments);
}
$ispublicmethod = new ReflectionMethod($controller, $action_name);
if (!$ispublicmethod->isPublic())
{
Core::rm_controoler($controller);
throw new Exception(__('Request Method Not Allowed.'), 405);
}
unset($ispublicmethod);
# POST 方式,自动CSRF判断
if (HttpIO::METHOD=='POST')
{
$auto_check_post_method_referer = isset($controller->auto_check_post_method_referer)?$controller->auto_check_post_method_referer:Core::config('auto_check_post_method_referer', true);
if ($auto_check_post_method_referer && !HttpIO::csrf_check())
{
throw new Exception(__('Not Acceptable.'), 406);
}
}
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->uri = $uri;
$controller->directory = $found['dir'];
if (IS_SYSTEM_MODE)
{
# 系统内部调用参数
$controller->arguments = @unserialize(HttpIO::POST('data', HttpIO::PARAM_TYPE_OLDDATA));
}
else
{
$controller->arguments = $arguments;
}
# 设置 HttpIO 参数
HttpIO::set_params_controller($controller);
# 前置方法
if (method_exists($controller, 'before'))
{
$controller->before();
}
# 执行方法
$count_arguments = count($arguments);
switch ($count_arguments)
{
case 0:
$controller->$action_name();
break;
case 1:
$controller->$action_name($arguments[0]);
break;
case 2:
$controller->$action_name($arguments[0], $arguments[1]);
break;
case 3:
$controller->$action_name($arguments[0], $arguments[1], $arguments[2]);
break;
case 4:
$controller->$action_name($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
break;
default:
call_user_func_array(array($controller, $action_name), $arguments);
break;
}
# 后置方法
if (method_exists($controller, 'after'))
{
$controller->after();
}
# 移除控制器
Core::rm_controoler($controller);
unset($controller);
}
else
{
throw new Exception(__('Page Not Found'), 404);
}
}
else
{
throw new Exception(__('Page Not Found'), 404);
}
}