我想从这样的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();
例如,但当我尝试以随机方式创建路径时 - >错误和..我不知道该怎么做请求帮助!
答案 0 :(得分:1)
正如我在评论中提到的:你不能在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}();
答案 1 :(得分:0)
更多代码会有用,但这会有帮助吗?
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']));