0

我正在修改我的调度程序以利用命名空间。

我的自动加载器非常标准,看起来像这样:

$paths = array(
get_include_path(),
ROOT . DS . 'library'  . DS . 'intranet'  . DS  . 'classes',
ROOT . DS . 'application',
);

set_include_path(join(PATH_SEPARATOR, $paths));
spl_autoload_register();

我的路由器有点乱,但到目前为止,

$url = explode('/', trim($_SERVER['REQUEST_URI'], '/'));

    $controller = !empty($url[0])  ? 'controller\\'.$url[0]  : 'controller\\home';
    $method = !empty($url[1]) ? $url[1] : 'index';
    $params = !empty($url[2]) ? $url[2] : $_POST;


    if (class_exists($controller)){

        $dispatchedController = "new $controller()";

        if (! method_exists($controller, $method)){
            trigger_error("{$controller}::{$method}() Method does not exist!");
            $error = new ErrorHandler();
            $error->invalidDispatch(debug_backtrace(),$controller, $method);
            return;
        }

        return $dispatchedController::$method;

    } else{
        trigger_error("{$controller}::{$method}({$params}) Method does not exist!");
        $error = new ErrorHandler();
        $error->invalidDispatch(debug_backtrace(), $controller, $method);
        return;
    }
}

但是,它一直失败return $dispatchedController::$method;

让我们以类文件为例home

命名空间控制器;

class home
{
    public function index(){

    echo "cool!";
    }
}

我遇到了错误:Fatal error: Class 'new controller\home()' not found in /public/library/intranet/classes/router.php on line 30

如何修改调度程序?

4

1 回答 1

0

您需要通过将代码更改为来创建控制器对象

$dispatchedController = new $controller;

然后您可以将该方法作为可调用返回,也许与

return array($dispatchedController, $method);

然后可以调用

call_user_func($returnedValue); // and also with call_user_func_array
于 2012-11-21T00:03:06.023 回答