Fatal Error: Silex \ Application :: share ()

1

I'm developing an API in Silex and when trying to test it on my server, I'm encountering the following error:

  

Fatal error: Call to undefined method Silex \ Application :: share () in /var/www/App/Config/config.php on line 24

And the share function is a function of Silex itself, but I do not know the reason for this error.

The line of code he's pointing at is this one:

'users' => $app->share(function($app) {
       $em = $app['orm.em'];
       return $em->getRepository('\App\Model\Entity\User');
})
    
asked by anonymous 24.07.2017 / 21:34

1 answer

1

I'd like to apologize in advance: I'm not a Portuguese speaker, I'm just using Google Translate to translate my answer.

As other people have indicated that you seem to be reading the documentation for Silex 1.x, but what you have installed is Silex 2.x.

One of the major differences between the two versions is that the dependency injection container that Silex uses - Pimple - was embedded in Silex itself in 1.0, while in 2.0 it is treated as a separate application (and probably this is a good idea). Share is actually a Pimple method, not a Silex method.

Well, I say it's a Pimple method ... it was a Pimple method. In Pimple 1.x, all dependencies were transient objects, unless you specifically used the share method to make it a singleton. In the current version of Pimple (3.x), the default has been changed and all dependencies are automatically singletons by default, so the share method is redundant.

What you need to do is follow the Silex 2.x and Pimple 3.x documentation when configuring your application.

References:

(Silex 1.x / Pimple 1.x) By default, each time you receive a service, Pimple returns a new instance. If you want the same instance to be returned for all calls, wrap its anonymous function with the share() method

(Silex 2.x / Pimple 3.x) By default, each time you receive a service, Pimple returns the same instance.

    
24.07.2017 / 22:04