首页 > 解决方案 > 在 mvc 中正确使用命名空间

问题描述

我有一个 MVC 引擎。我需要根据 URI 创建类的实例,即如果用户输入mysite.ru/main - 创建 MainController 的实例。所有控制器都位于相应的目录中并使用自动加载(psr-4)。这里出现了一个问题:我无法提前连接这个类,我避免这个问题的方法失败了:

use App\Controllers as Controller;
namespace Core;

class Route {
    static function start() {
        $name_of_controller = .. // Find this name (It will be MainController for example)
        $instance = new Controller."\\{}" //  <--- this code don't working
    }

}

如何解决这个问题呢?

标签: phpmodel-view-controller

解决方案


推荐阅读