我想要这样的网址: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();
例如,但是当我尝试以随机方式创建路径时 -> 错误和 .. 我不知道该怎么做,请帮忙!
最佳答案
正如我在评论中提到的:你不能在 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}();
关于Php 从 $_GET[] 元素创建命名空间路径并使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13788612/