0

我有以下目录结构 在此处输入图像描述

这是我在 composer.json 文件中使用 psr-4 的方式

"autoload": {
    "psr-4": {"MyMVC\\": "app/"}
},

现在在我的初始化文件中,我试图动态加载控制器

<?php namespace MyMVC;

use MyMVC\Core\Config;
use MyMVC\Controllers;

class Application
{
    /**
     * Takes the arguments and execute the requested route
     *
     * @param  [type] $controller name of the controller
     * @param  [type] $method     name of method
     * @param  array  $arguments  an array of arguments to be passed into method
     * @access private
     */
    private function dispatch($controller, $method, array $arguments)
    {
        $controller = 'Controllers\\' . ucfirst($controller) . 'Controller';
        $controllerObject = new $controller;
    }

我的HomeController长相是这样的

<?php namespace MyMVC\Controllers;

class HomeController
{

    function __construct()
    {
        echo 'Hello World';
    }
}

现在,如果我访问 url ` http://localhost/mymvc/home/bla/bla ' 我收到错误

Fatal error: Class 'Controllers\HomeController' not found in /var/www/html/mymvc/app/init.php on line 136
4

1 回答 1

1

在应用程序应该是

$controller = 'MyMVC\\Controllers\\' . ucfirst($controller) . 'Controller';
于 2015-08-08T00:30:14.100 回答