【问题标题】:Silex 2.0 Unable to pass the Application $app to a UserServiceProviderSilex 2.0 无法将应用程序 $app 传递给 UserServiceProvider
【发布时间】:2016-11-06 22:13:08
【问题描述】:

我一直在为 Silex(最初为 1.3)使用 SimpleUser (https://github.com/jasongrimes/silex-simpleuser) 插件。我正在将我的应用程序升级到 Silex 2.0。 SimpleUser 中的 UserProviderInterface 实现了 ServiceProviderInterface 和 ControllerProviderInterface。前者现在在 Pimple 中定义,而不是像在 1.3 中那样在 Silex 中定义,并且它的 register 方法的参数是 Pimple 容器而不是 Silex 应用程序(它扩展了 Pimple 容器类)。 PHPStorm 将此标记为 PHP 致命错误(声明必须与 ServiceProviderInterface-->register(pimple: \Pimple\Container) 兼容)。我不确定 PHPStorm 中的检查是否过于严格,或者这是一个严重的问题。

其他帖子表明您不能简单地覆盖接口定义 (Can you override interface methods with different, but "compatible", signatures?)(也可以是 http://php.net/manual/en/language.oop5.interfaces.php)。

我刚刚解决了我的问题。 Silex 2.0 Provider 说明表明您可以使用 Container $app 作为参数实现 register 方法,其中 $app 是一个 Silex\Application 实例。 PHPStorm 不再反对 UserServiceProvider 中的 register 方法。我将继续发布此内容,以防其他人遇到此问题。

【问题讨论】:

  • 好问题,好研究,好答案!用一个具体的例子来更新你的问题,并提供一个适当的“答案”可能是一个想法。

标签: php service interface silex provider


【解决方案1】:

进一步调试后的一点进一步信息。使用 Silex\ServiceProviderInterface 和 Silex\ControllerProviderInterface 为 Silex 1.3 实现了 SimpleUser UserServiceProvider,其中 ServiceProviderInterface 提供了注册和引导方法。

在 Silex 2.0 中,ServiceProviderInterface 仅提供一个注册方法,现在位于 Pimple 命名空间中。 BootableProviderInterface 可以提供 boot 方法,而 ControlleProviderInterface 可以提供 connect 方法,并且位于 Silex\API 命名空间中。

SimpleUserProvider 必须实现所有三个才能在 Silex2.0 中工作。 register() 方法现在需要一个 Pimple\Container 作为它的参数,而不是一个 Silex\Application,而 boot() 和 connect() 方法仍然有一个 Silex\Application 作为第一个参数。

例如 类 UserServiceProvider 实现 服务提供者接口, BootableProvider接口, 控制器提供者接口 { 公共函数寄存器(容器 $app){ ... } 公共功能启动(应用程序 $app){ ... } 公共功能连接(应用程序$app){ …… } ... }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2012-12-24
    • 1970-01-01
    相关资源
    最近更新 更多