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

£可爱£侵袭症+ 提交于 2019-12-21 03:29:25

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/28163693/attempted-to-call-method-share-on-class-silex-application-in-silex-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!