absens
absens

Reputation: 23

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, e.g. 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:

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, e.g.: $app->get('/program', '\App\Controller\ProgramController:list')->setName('program');

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

Upvotes: 0

Views: 229

Answers (1)

absens
absens

Reputation: 23

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".

Upvotes: 1

Related Questions