Smern
Smern

Reputation: 19076

Custom ControllerProvider Class not found

I have a custom controller provider class that is working fine. I've tried adding a second one and it just keeps saying:

Fatal error: Class 'Bar\BarControllerProvider' not found in C:\xampp\htdocs\foobar\src\app.php on line 18

I've got it set up the same way as the first one, with

namespace Bar;

...

class BarControllerProvider implements ControllerProviderInterface {
    public function connect(Application $app) {
        ...
        $controllers = $app['controllers_factory'];

        $controllers->get('/', function () use ($app) {
           ...
        });

        ...

        return $controllers;
    }
}

And the autoloader set up in composer:

{
    "require": {
        "silex/silex": "~1.3",
        "doctrine/dbal": "~2.2",
        "symfony/security": "^3.0"
    },
    "autoload" : {
        "psr-0": {
            "Foo": "/src/Foo",
            "Bar": "/src/Bar"
        }
    }
}

File directory looks something like this:

-config (bunch of stuff in here)
-src
  |-Foo
  |  |-FooControllerProvider.php
  |-Bar
  |  |-BarControllerProvider.php
  |-app.php
-vendor (bunch of stuff in here)
-web (bunch of stuff in here)
-composer.json
-composer.lock

app.php has these:

$app->mount("/foos", new Foo\FooControllerProvider());
$app->mount("/bars", new Bar\BarControllerProvider());

I've actually deleted the entire vendor folder and did a fresh composer install and that didn't make any difference.

If I comment out the bars mount, foos will work fine. Why can't it find the BarControllerProvider?

Upvotes: 0

Views: 115

Answers (1)

Matteo
Matteo

Reputation: 39390

I think you don't need different mapping defined for the same folder, and i suggest you to use the PSR-4 autoloading instead of the PSR-0 autoloading, as described here in the doc:

PSR-4 is the recommended way though since it offers greater ease of use (no need to regenerate the autoloader when you add classes).

Simply try to map the src folder as follow:

{
    "autoload": {
        "psr-4": { "": "src/" }
    }
}

Hope this help

Upvotes: 3

Related Questions