组织自定义 类

Organise custom classes

我正在尝试学习 Slim 3 and I have difficulties trying to figure out the proper way to organise my custom code, esp. custom classes. For instance, I want to create a custom error handler 的基础知识:

<?php
namespace App\Handlers;
// [...]
final class Error extends \Slim\Handlers\Error
{
    // [...]
}

...但是我检查过的文档没有透露在哪个路径下保存 class 定义或如何配置框架以便可以在我的 index.php 条目中找到它点数:

<?php
require __DIR__ . '/../vendor/autoload.php';
// [...]
$app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);
$container = $app->getContainer();
$container['errorHandler'] = function ($c) {
    return new App\Handlers\Error($c['Logger']);
};

Fatal error: Class 'App\Handlers\Error' not found

如有任何提示,我将不胜感激。

你的问题与框架无关

Slim 不会告诉您将自定义代码保存在何处,因为这是您自由选择的问题。

你的错误:

Fatal error: Class 'App\Handlers\Error' not found

不是Slim生成的,而是PHP自己生成的。您需要为您的代码添加一个自动加载器,让 PHP 知道在哪里可以找到合适的 类.

我看到您使用 Composer,因此它是 configure composer.json to create autoloader 代码的最佳选择。