Silex can't find Controller Class

1.2k views Asked by At

Project directory structure

/composer.json
/web/index.php
/app/controller/IndexController.php 

composer.json

{
  "require" : {
    "silex/silex": "*"
  },
  "autoload": {
    "psr-0": {
            "": "./"
        }
  }
}

index.php

<?php

require_once __DIR__.'/../vendor/autoload.php';

use Silex\Application;

$app = new Application();
$app['debug'] = true;

$app->get('/', 'App\\Controller\\IndexController::getIndex');

$app->run();

IndexController.php

<?php

namespace App\Controller;

use Silex\Application;

class IndexController
{
    public function getIndex(Application $app)
    {
        return "You got the index, congrats!";
    }
}

Whenever I

cd web/
php -S localhost:8080
curl http://localhost:8080

from the project root I get the the following exception

InvalidArgumentException in ControllerResolver.php line 153:
Class "App\Controller\IndexController" does not exist.
in ControllerResolver.php line 153
at ControllerResolver->createController('App\Controller\IndexController::getIndex') in ControllerResolver.php line 80
at ControllerResolver->getController(object(Request)) in HttpKernel.php line 135
at HttpKernel->handleRaw(object(Request), '1') in HttpKernel.php line 68
at HttpKernel->handle(object(Request), '1', true) in Application.php line 581
at Application->handle(object(Request)) in Application.php line 558
at Application->run() in index.php line 12

Which makes no sense to me. The program actually runs just fine locally on my MacBook but when I deploy it to my Ubuntu VPS I get the above exception even though the code being run in both environments is identical. Why can't Silex find my controller?

3

There are 3 answers

0
pretzelhammer On BEST ANSWER

It was a capitalization issue. My macbook has a case-insensitive filesystem and my Linux VPS has a case-sensitive filesystem so when deploying to the VPS I had to capitalize the directories in my project so that Silex could correctly resolve the controllers.

0
doncredas On

try this

"autoload" : {
    "psr-4" : {
        "App\\Controller\\" : "app/controller/"
    }
}
0
Aylen On

In my case, I was missing the use Symfony\Component\HttpFoundation\Request; line.

From this post:

Check that your controller has this:

use Silex\Application;

use Symfony\Component\HttpFoundation\Request;