我希望url是这样的: example.com/?module=index&controller=index&action=hello通过$_GET[]获取值,然后创建像application\modules\index\controllers\IndexController这样的命名空间路径,然后
use application\modules\index\controllers\IndexController as IndexController;
$IndexController = new IndexController;
$IndexController->hello();
例如,当我尝试以随机的方式创建路径时,-> error和..我不知道该怎么做,请帮帮我!
发布于 2012-12-09 23:30:09
正如我在评论中提到的:你不能在use语句中使用变量,在创建新实例时,你必须在变量中使用完全命名空间的类名。
所以它看起来像这样:
$module = $_GET['module'];
$ctrl = $_GET['controller'];
//Remember to use double backslashes in strings
$class = "application\\modules\\{$module}\\controllers\\{$ctrl}Controller";
$controller = new $class;
$controller->{$action}();
发布于 2012-12-09 23:22:59
更多的代码将是有用的,但这有帮助吗?
namespace Foobar;
class Foo {
static public function test() {
echo "Hello";
}
}
call_user_func(__NAMESPACE__ .'\Foo::'.$_GET['action']);
// or
call_user_func(array(__NAMESPACE__ .'\Foo', $_GET['action']));
https://stackoverflow.com/questions/13788612
复制相似问题