Search code examples
phpsymfonysilex

Migrate Silex v1.3 to v2, ClassNotFoundException


I recently migrate Silex v1.3 to v2 by changing 3 lines in the composer.json

    "silex/silex": "~2.0",
    "silex/web-profiler": "~2.0",
    "deralex/yaml-config-service-provider": "2.0.x-dev"

But since a lot of class can't be found by Silex...

Here is my app.php file, correctly load everything I need in Silex 1.3:

<?php

use Silex\Application;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\UrlGeneratorServiceProvider;
use Silex\Provider\ValidatorServiceProvider;
use Silex\Provider\ServiceControllerServiceProvider;
use Silex\Provider\HttpFragmentServiceProvider;
use Silex\Provider\SessionServiceProvider;
use Silex\Provider\SecurityServiceProvider;
use Silex\Provider\FormServiceProvider;
use Manager\ManagerProvider;

$app = new Application();
$app->register(new UrlGeneratorServiceProvider());
$app->register(new ValidatorServiceProvider());
$app->register(new ServiceControllerServiceProvider());
$app->register(new TwigServiceProvider());
$app->register(new HttpFragmentServiceProvider());
$app->register(new SessionServiceProvider());
$app->register(new FormServiceProvider());
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
    'translator.messages' => array(),
));
$app->register(new SecurityServiceProvider(), [
    'security.firewalls'    => [
        'foo'     => [ 'pattern' => '^/foo' ],
        'default' => [
            'pattern'   => '^.*$',
            'anonymous' => true,
            'form'      => [ 'login_path' => '/login','check_path' => 'connexion' ],
            'logout'    => [ 'logout_path' => '/logout' ],
            'users'     => $app->share( function () use ( $app ) {
                return new Authenticate\UserProvider( $app['manager.user'] );
            } ),
        ],
    ],
    'security.access_rules' => [
        [ '^/.+$','IS_AUTHENTICATED_ANONYMOUSLY' ],
        [ '^/foo$','' ]
    ]
]);
$app['twig'] = $app->share($app->extend('twig', function ($twig, $app) {
    // add custom globals, filters, tags, ...

    return $twig;
}));

$app->register(new DerAlex\Silex\YamlConfigServiceProvider(__DIR__ . '/../app/settings.yml'));
$app->register(new ManagerProvider($app));

return $app;

First error:

ClassNotFoundException in app.php line 15:
Attempted to load class "UrlGeneratorServiceProvider" from namespace "Silex\Provider".
Did you forget a "use" statement for another namespace?

Second error:

UndefinedMethodException in app.php line 33:
Attempted to call an undefined method named "share" of class "Silex\Application".

Third error:

ClassNotFoundException in app.php line 56:
Attempted to load class "YamlConfigServiceProvider" from namespace "DerAlex\Silex".
Did you forget a "use" statement for "DerAlex\Pimple\YamlConfigServiceProvider"?

Fourth error:

ClassNotFoundException in ManagerProvider.php line 12:
Attempted to load interface "ServiceProviderInterface" from namespace "Silex".
Did you forget a "use" statement for "Pimple\ServiceProviderInterface"?

I don't know what should be change and how correcting that... Thanks for help


Solution

    1. UrlGeneratorServiceProvider was replaced by RoutingServiceProvider

      $app->register(new RoutingServiceProvider());

    2. Method share was removed from Pimple. Just set function that returns object to share some object.

      $app['object'] = function() { return new Object(); }

    3. May be composer is not set to autoload classes with namespace DerAlex\.... Try to use \DerAlex\Silex\YamlConfigServiceProvider.

    4. Add use statement use Pimple\ServiceProviderInterface or use this class as \Pimple\ServiceProviderInterface.

    http://php.net/manual/en/language.namespaces.php