Symfony container traits

醉酒当歌 提交于 2019-12-05 07:13:31
Chris Trahey

I'll venture a guess based on a quick glance into the Symfony source code: You still need to declare that you adhere to the ContainerAwareInterface Interface.

This is what the code looks like whenever Symfony is setting a container on a controller.

if ($controller instanceof ContainerAwareInterface) {
  $controller->setContainer($this->container);
}

So then I suppose you need to do something like this:

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
// ...
class MainController implements ContainerAwareInterface 
{
    use ContainerAwareTrait;
    /**
     * @Route("/", name="_index")
     * @Template()
     */
    public function indexAction()
    {
         var_dump($this->container);

         return array();
    }

}

As an aside, this is arguably a pretty good case for Duck Typing, particularly if they had named the method something a bit more specific or if it were cheaper to inspect the parameter types to methods at runtime

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