尝试将 Slim 3 与 Doctrine 2 一起使用的问题

Problems trying to use Slim 3 with Doctrine 2

我收到以下消息:

Slim Application Error
The application could not run because of the following error:

Details

Type: Error
Message: Class 'App\Action\InterventionsAction' not found
File: /var/www/html/ws_slim/public/index.php
Line: 53
Trace

#0 [internal function]: Closure->{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#1 /var/www/html/ws_slim/vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(41): call_user_func(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#2 /var/www/html/ws_slim/vendor/slim/slim/Slim/Route.php(344): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 /var/www/html/ws_slim/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#4 /var/www/html/ws_slim/vendor/slim/slim/Slim/Route.php(316): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 /var/www/html/ws_slim/vendor/slim/slim/Slim/App.php(476): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 /var/www/html/ws_slim/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(122): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 /var/www/html/ws_slim/vendor/slim/slim/Slim/App.php(370): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#8 /var/www/html/ws_slim/vendor/slim/slim/Slim/App.php(295): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#9 /var/www/html/ws_slim/public/index.php(59): Slim\App->run()
#10 {main}

我的索引文件是这样的:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use App\Action\InterventionsAction;

require 'vendor/autoload.php';


$app = new \Slim\App([
    'settings' => [
        'displayErrorDetails' => true
    ]
]);
$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello,, $name");

    return $response;
});
$app->get('/api/interventions', function (Request $request, Response $response) {
    $int = new InterventionsAction($em);

    $response->getBody()->write("Hello,, ");

    return $response;
});
$app->run();

而 InterventionsAction 是这样的:

<?php
namespace App\Action;

use Doctrine\ORM\EntityManager;

final class InterventionsAction
{
    private $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function fetch($request, $response, $args)
    {
        $interventions = $this->em->getRepository('App\Entity\Interventions')->findAll();
        $interventions = array_map(
            function ($interventions) {
                return $interventions->getArrayCopy();
            },
            $interventions
        );
        return $response->withJSON($interventions);
    }

}

文件树:

├── cli-config.php
├── composer.json
├── composer.lock
├── CONTRIBUTING.md
├── EXPORT
│   ├── Companies.php
│   ├── CompaniesPlansPayments.php
│   ├── CompaniesPlans.php
│   ├── DepartmentsCoordinators.php
│   ├── Departments.php
│   ├── InterventionclasificationDetails.php
│   ├── Interventionclasifications.php
│   ├── Interventionrelations.php
│   ├── Interventions.php
│   ├── InterventiontypesCompanies.php
│   ├── Interventiontypes.php
│   ├── Locations.php
│   ├── LocationTypes.php
│   ├── Managementreview.php
│   ├── Managementreviewrelations.php
│   ├── Plans.php
│   ├── ServicelinesManagers.php
│   ├── Servicelines.php
│   ├── Users.php
│   └── Usertypes.php
├── logs
│   ├── app.log
│   └── README.md
├── nbproject
│   ├── private
│   │   └── private.properties
│   ├── project.properties
│   └── project.xml
├── phpunit.xml
├── public
│   └── index.php
├── README.md
├── src
│   ├── Action
│   │   └── InterventionsAction.php
│   ├── dependencies.php
│   ├── Entity
│   │   ├── Companies.php
│   │   ├── CompaniesPlansPayments.php
│   │   ├── CompaniesPlans.php
│   │   ├── DepartmentsCoordinators.php
│   │   ├── Departments.php
│   │   ├── InterventionclasificationDetails.php
│   │   ├── Interventionclasifications.php
│   │   ├── Interventionrelations.php
│   │   ├── Interventions.php
│   │   ├── InterventiontypesCompanies.php
│   │   ├── Interventiontypes.php
│   │   ├── Locations.php
│   │   ├── LocationTypes.php
│   │   ├── Managementreview.php
│   │   ├── Managementreviewrelations.php
│   │   ├── Plans.php
│   │   ├── ServicelinesManagers.php
│   │   ├── Servicelines.php
│   │   ├── Users.php
│   │   └── Usertypes.php
│   ├── middleware.php
│   ├── routes.php
│   └── settings.php
├── templates
│   └── index.phtml
├── tests
│   └── Functional
│       ├── BaseTestCase.php
│       └── HomepageTest.php
└── vendor
    ├── autoload.php
    ├── bin
    │   ├── doctrine -> ../doctrine/orm/bin/doctrine
    │   ├── doctrine-dbal -> ../doctrine/dbal/bin/doctrine-dbal
    │   ├── doctrine.php -> ../doctrine/orm/bin/doctrine.php
    │   └── phpunit -> ../phpunit/phpunit/phpunit
    ├── composer
    │   ├── autoload_classmap.php
    │   ├── autoload_files.php
    │   ├── autoload_namespaces.php
    │   ├── autoload_psr4.php
    │   ├── autoload_real.php
    │   ├── ClassLoader.php
    │   ├── installed.json
    │   └── LICENSE
    ├── container-interop
    │   └── container-interop
    ├── doctrine
    │   ├── annotations
    │   ├── cache
    │   ├── collections
    │   ├── common
    │   ├── dbal
    │   ├── inflector
    │   ├── instantiator
    │   ├── lexer
    │   └── orm
    ├── monolog
    │   └── monolog
    ├── myclabs
    │   └── deep-copy
    ├── nikic
    │   └── fast-route
    ├── phpdocumentor
    │   ├── reflection-common
    │   ├── reflection-docblock
    │   └── type-resolver
    ├── phpspec
    │   └── prophecy
    ├── phpunit
    │   ├── php-code-coverage
    │   ├── php-file-iterator
    │   ├── php-text-template
    │   ├── php-timer
    │   ├── php-token-stream
    │   ├── phpunit
    │   └── phpunit-mock-objects
    ├── pimple
    │   └── pimple
    ├── psr
    │   ├── container
    │   ├── http-message
    │   └── log
    ├── sebastian
    │   ├── code-unit-reverse-lookup
    │   ├── comparator
    │   ├── diff
    │   ├── environment
    │   ├── exporter
    │   ├── global-state
    │   ├── object-enumerator
    │   ├── recursion-context
    │   ├── resource-operations
    │   └── version
    ├── slim
    │   ├── php-view
    │   └── slim
    ├── symfony
    │   ├── console
    │   ├── debug
    │   ├── polyfill-mbstring
    │   └── yaml
    └── webmozart
        └── assert

所以我的问题是我可能设置了一些错误的配置值,或者是否缺少我应该寻找的东西?

我假设您的自动加载器找不到文件。您应该在 composer.json 中搜索 "autoload" 部分,然后在其中放置类似以下内容的内容:

"autoload": {
    "psr-4: {
        "App\": "src/",
    }
}

这假定您的目录 src/ 仅包含来自 App 命名空间的 classes。所以 src/Action/InterventionAction.php 期望包含一个带有命名空间 App\Action\InterventionAction 的 class。对于您的测试,您可能希望在 "autoload-dev" 部分中执行等效操作。

您还必须 运行 composer installcomposer update --lock 以确保使用新信息更新自动加载器。

您可能还想阅读自动加载 PSR 以了解如何将文件名解析为 classes 和作曲家文档: