简体   繁体   中英

Slim4 controller autoloaded correctly in localhost but not on remote server ("callable controller does not exist")

I am developing an app with Slim 4 PHP framework for the first time and it works great in localhost, but I have an issue with running it in remote server. I am getting following error when running all routes, eg example.com/myapp/app.php/program :

Uncaught RuntimeException: Callable \App\Controller\ProgramController::list() does not exist in /.../vendor/slim/slim/Slim/CallableResolver.php

Below is basic config of both environments and main php files:

  • localhost: Windows 10, Apache 2.4.53, PHP 8.1.12 (xampp)

  • remote server: Debian, Apache 2.4.54, PHP 8.1.12

composer.json

{
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
}

app.php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

require __DIR__ . '/vendor/autoload.php';
require_once './app/config-dev.php'; // contains all CONFIG_ vars

$app = AppFactory::create();
# set app's base path from config
$app->setBasePath(CONFIG_BASE_PATH);

$twig = Twig::create(CONFIG_TWIG_DIR, ['cache' => CONFIG_TWIG_CACHE]);
$app->add(TwigMiddleware::create($app, $twig));

if (CONFIG_DEBUG) {
    $app->addRoutingMiddleware();
    $app->addErrorMiddleware(true, true, true);
}

# routing
require_once './app/routing.php';

$app->run();

app/routing.php

use App\Controller\ProgramController;

$app->get('/program', [ProgramController::class, 'list'])
    ->setName('program');
// ...

app/controller/ProgramController.php

namespace App\Controller;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class ProgramController
{
    public function __construct()
    {
        // ...
    }
    
    public function list(Request $request, Response $response, array $args): Response
    {
        // ...
    }
}

I have tried different forms of namespaces and different strategies for registering controller, eg: $app->get('/program', '\App\Controller\ProgramController:list')->setName('program');

I have also tried running composer dump-autoload but nothing works.

That was dumb issue. The folder holding controllers was named "controller", in small caps, but the namespace is \App\Controller, thus it could not find it. This is crucial on UNIX systems which are case sensitive, but it worked fine on Windows which is case insensitive with file paths. Works fine after changing folder name to "Controller".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM