Google App Engine-PHP-未找到类

时间:2018-07-30 05:30:22

标签: php google-app-engine slim

我在使用Google App Engine和PHP时遇到问题。

我将Slim framweork用于示例项目以测试App Engine(当前我使用AWS)。

我创建了一个简单的Slim应用,并将其部署到App引擎和AWS。在AWS上运行正常,但在App Engine上却出现“找不到类错误”:

  

2018/07/29 21:38:15 [错误] 9#9:* 1在stderr中发送的FastCGI:“ PHP消息:PHP致命错误:未捕获的错误:在/中找不到类'projectName \ Middleware \ Logging' app / public / index.php:21

     

在第51行的/app/public/index.php中抛出”,同时读取响应   来自上游的标头,客户端:172.17.0.4,服务器:,请求:“ GET //   HTTP / 1.1”,上游:“ fastcgi://127.0.0.1:9000”,主机:   “ projectName-183713.appspot.com”

/public/index.php

<?php
namespace projectName;
require __DIR__ . '/../vendor/autoload.php';

session_start();

$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);

require __DIR__ . '/../src/dependencies.php';
require __DIR__ . '/../src/middleware.php';
require __DIR__ . '/../src/routes.php';
require __DIR__ . '/../src/projectName/Routes/route_login.php';
require __DIR__ . '/../src/projectName/Routes/route_user.php';
require __DIR__ . '/../app/routes.php';
require __DIR__ . '/../app/database.php';

use \projectName\Middleware\Logging;
use \projectName\Middleware\Authentication;

$logging = new Logging();
$auth = new Authentication($app->getContainer()->get('router'));

$app->add($logging);
$app->add($auth);

$app->get('/hello/{name}', function ($request, $response, $args) {
    echo 'Hello';

    return $response;
});

$app->run();

/src/projectName/Middleware/Logging.php

<?php
namespace projectName\Middleware;

class Logging{
    public function __invoke($request, $response, $next)
    {
    $response = $next($request, $response);
    return $response;
    }
}

app.yaml

runtime: php
env: flex

runtime_config:
  document_root: public

handlers:
  - url: .*
    script: index.php
  - url: /(.+\.php)$
    script: \1

__ DIR__。 '/../ vendor / autoload.php'

"autoload": {
    "psr-4": {
        "\\projectName\\": "src/projectName",
        "\\projectName\\Models\\": "src/projectName/Models",
        "\\projectName\\Middleware\\": "src/projectName/Middleware"
    }
}

1 个答案:

答案 0 :(得分:1)

问题出在composer.json中的psr-4。

App Enginen不能在路由中使用两个反斜线。因此,解决方案是:

"\\projectName\\": "src/projectName",

=>

"projectName\\": "src/projectName",

AWS和本地开发人员(标准php docker)在双反斜杠上可以正常工作,但App引擎不能。