Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

symfony

silex

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

like image 900
Luis Fernando Czares Bulbarela Avatar asked Jan 27 '15 05:01

Luis Fernando Czares Bulbarela


1 Answers

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

like image 89
mTorres Avatar answered Sep 28 '22 02:09

mTorres