试图在Doctrine 2中使用Slim 3的问题

时间:2017-04-08 23:23:35

标签: php symfony doctrine-orm slim

我收到以下消息:

//client.js
$.ajax({
    method: "GET",
    async: true,
    url: "/events",
    success: function(data) {
        callback(data);
    },
    complete: function(request, status, err) {
        if (status == "timeout" || status == "success") {
            console.log("LOG: Normal long-polling timeout or successful poll, continuing.");
            longPoll();
        } else {
            console.warn("WARN: Server probably offline, retrying in 2 sec.");
            setTimeout(function() {
                longPoll();
            }, 2000);
        }
    },
    timeout: 30000
});

我的索引文件是这样的:

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}

和InterventionsAction是这样的:

<?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();

文件树:

<?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);
    }

}

所以我的问题可能是我设置了一些错误的配置值,或者是否有什么遗漏我应该寻找?

1 个答案:

答案 0 :(得分:1)

我认为您的自动加载器无法找到这些文件。您应该在composer.json中搜索"autoload"部分,并在其中添加如下内容:

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

这假定您的目录src /仅包含App-namespace中的类。所以src / Action / InterventionAction.php期望包含一个名为App\Action\InterventionAction的类。对于您的测试,您可能希望在“autoload-dev”-section中执行等效操作。

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

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