【发布时间】:2021-02-26 07:31:30
【问题描述】:
因此,如果我使用 $loader->registerDirs() 函数,一切正常,但如果我使用 registerNamespaces 函数,我会收到调度程序错误,即:“无法加载 IndexController 处理程序类”。这是 index.php 文件的代码
<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Url;
use Phalcon\Db\Adapter\Pdo\Mysql;
// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
// Register an autoloader
$loader = new Loader();
//$loader->registerDirs(
// [
// APP_PATH . '/controllers/',
// APP_PATH . '/models/',
// APP_PATH . '/modules/'
// ]
//);
$loader->registerNamespaces(
[
'App\Controllers' => APP_PATH . '/controllers/'
]
);
$loader->register();
$container = new FactoryDefault();
$container->set(
'view',
function () {
$view = new View();
$view->setViewsDir(APP_PATH . '/views/');
return $view;
}
);
$container->set(
'db',
function () {
return new Mysql(
[
'host' => 'localhost',
'username' => 'sammy',
'password' => 'password',
'dbname' => 'learning',
]
);
}
);
$container->set(
'url',
function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
}
);
$application = new Application($container);
// Handle the request
$response = $application->handle(
$_SERVER["REQUEST_URI"]
);
$response->send();
我的 IndexController.php 文件。
<?php
declare(strict_types=1);
namespace App\Controllers;
use Phalcon\Http\Request;
use \Phalcon\Mvc\Controller;
use Phalcon\Http\Message\Uri;
class IndexController extends Controller
{
public function indexAction()
{
return 'hi';
}
}
所以你可以看到 registerDirs 和 registerNamespaces 函数的路径是相同的,但它似乎不起作用。因为它,我尝试了很多改变路径的方法,但没有帮助。
【问题讨论】: