Search code examples
symfonysilex

Attempted to call method "share" on class "Silex\Application" in Silex 2


I am developing a project with silex-skeleton in its most recent version. When trying to use the share method shows me the following error:

Code:

$app['login'] = $app->share(function() use($app) {
    return new Model\UserModel($app);
});

Error: Attempted to call method "share" on class "Silex\Application"

Any suggestions or possible cause of this failure


Solution

  • Silex 2.0 is using Pimple 3.0 which has removed the shared method, now all services are shared by default, if you want a new instance you must call the factory method as stated in the changelog for version 2.0.

    So if you want a login service you should create it like this:

    <?php
    
    $app['login'] = function($app) {
        return new Model\UserModel($app);
    };
    

    You can take a look at the docs for the 3.0 Pimple version directly on it's GitHub repository

    PS: Keep in mind that, at the time of this writing, Silex 2.0 is in development, so be prepared to adapt your code until it gets a 2.0 stable version. 2.0 has reached prod status as of 2016-05-18