php - 在 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
}
}
如何解决这个问题呢?
解决方案
推荐阅读
- android - 如何将应用程序安装到 Android 模拟器的 SD 卡而不是其内部存储器?
- javascript - 如何在Javascript上为计时器制作暂停/播放按钮?
- mysql - 在插入错误语法#1064 后选择 count(*) 并选择 sum(my_data)
- python - 如何使用多处理添加大量 pandas df
- c# - 无法使用 WorkItemTrackingHttpClient 更新 WorkItem 状态
- python - Python双重列表到文本文件
- rabbitmq - RabbitMQ Pika - 如何检查消费者是否处于活动状态
- c# - 使用格式说明符货币 C# 的有效方法
- jquery - 添加“包装器” div 标签来包装其他 html div 标签?
- swift - 类型检查失败,Swift 中的可选值