doubei5310
2015-11-01 23:46
浏览 81
已采纳

Silex控制器依赖注入

I'm trying to inject a dependency into my Silex Controller, because I need an object of type user in my controller and handle some stuff with it.

$app->mount("/users", new \MyApp\Controller\Provider\User($user));

And I implemented the controller by implementing the ControllerProviderInterface:

class User implements ControllerProviderInterface{
    protected $user;

    public function __construct($user){
        //...
    }

    public function connect(Application $app)
    {
        //...
    }
}

The routes and the methods are all set up in the controller. Without the dependency injection everything works fine. But as long as I edit the code and add the injection I get the following error:

Missing argument 1 for ...::__construct()

When I create the object, I send that parameter to it, but somehow Silex creates an instance before with a constructor without passing any argument.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将依赖项注入我的Silex控制器,因为我需要在控制器中使用user类型的对象 用它处理一些东西。</ p>

  $ app-&gt; mount(“/ users”,new \ MyApp \ Controller \ Provider \ User($ user)); 
  </ code> </ pre> 
 
 

我通过实现ControllerProviderInterface实现了控制器:</ p>

 类用户实现ControllerProviderInterface {
 protected $ user;  
 
公共函数__construct($ user){
 //....n} 
 
公共函数connect(Application $ app)
 {
 //....n} 
}  
 </ code> </ pre> 
 
 

路由和方法都在控制器中设置。 没有依赖注入一切正常。 但只要我编辑代码并添加注入,我就会收到以下错误:</ p>

缺少参数1 ... :: __ construct()</ p> </ blockquote>

当我创建对象时,我将该参数发送给它,但不知何故,Silex在使用构造函数之前创建了一个实例而没有传递任何参数。</ p> </ div >

3条回答 默认 最新

相关推荐 更多相似问题