AkosL
AkosL

Reputation: 11

Google App Engine - PHP - Class not found

i've problem with Google App Engine and PHP.

I use Slim framweork for a sample project to test App Engine (currently I use AWS).

I created a simple Slim app and deployed it to App engine and to AWS. On AWS it runs fine, but on App Engine i get "class not found error":

2018/07/29 21:38:15 [error] 9#9: *1 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Class 'projectName\Middleware\Logging' not found in /app/public/index.php:21

thrown in /app/public/index.php on line 51" while reading response header from upstream, client: 172.17.0.4, server: , request: "GET // HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "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"
    }
}

Upvotes: 0

Views: 548

Answers (1)

AkosL
AkosL

Reputation: 11

The problem was with psr-4 in composer.json.

App Enginen doesn't work with two backslash in the route. So, the solution is:

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

=>

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

AWS and local dev (standard php docker) works fine with double backslash, but App engine not.

Upvotes: 1

Related Questions