Attempted to call method “share” on class “Silex\A

2020-06-08 13:01发布

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

标签: symfony silex
1条回答
做自己的国王
2楼-- · 2020-06-08 13:56

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

查看更多
登录 后发表回答