繁体   English   中英

Symfony 3:无法从控制器内部访问容器

[英]Symfony 3: cannot access the container from inside a controller

我正在将我的应用程序从Symfony 2.8迁移到Symfony 3.3。

从我的控制器内部,我有这个:

public function indexAction()
{
    $email = new Email();

    $form = $this->createForm(GetStartedType::class, $email, [
        'action' => $this->generateUrl('get_started_end'),
        'method' => 'POST',
    ]);

    return [
        'form' => $form->createView(),
    ];
}

但我收到此异常:

在null上调用成员函数get()

我的控制器扩展了Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller

/**
 * {@inheritdoc}
 */
class DefaultController extends Controller
{
...
}

因此,我可以访问该容器。

在Symfony的代码中放一些转储,我看到容器设置正确:

namespace Symfony\Component\DependencyInjection;

/**
 * ContainerAware trait.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
trait ContainerAwareTrait
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * Sets the container.
     *
     * @param ContainerInterface|null $container A ContainerInterface instance or null
     */
    public function setContainer(ContainerInterface $container = null)
    {
        dump('Here in the ContainerAwareTrait');
        dump(null === $container);
        $this->container = $container;
    }
}

这个转储

Here in the ContainerAwareTrait
false

因此,自动装配可以很好地工作并设置容器。

但是在ControllerTrait我有这个:

trait ControllerTrait
{
    /**
     * Generates a URL from the given parameters.
     *
     * @param string $route         The name of the route
     * @param mixed  $parameters    An array of parameters
     * @param int    $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
     *
     * @return string The generated URL
     *
     * @see UrlGeneratorInterface
     */
    protected function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
    {
        dump('Here in the ControllerTrait');
        die(dump(null === $this->container));
        return $this->container->get('router')->generate($route, $parameters, $referenceType);
    }

    ...

这是转储:

Here in the ControllerTrait
true

因此,此处的containernull ,这将导致错误。

有人可以帮助我解决这个问题吗?

为什么container空?

如果可能有帮助,请使用services.yml配置(Symfony随附的默认配置):

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
    resource: '../../src/AppBundle/Controller'
    public: true
    tags: ['controller.service_arguments']

该问题已作为问题发布在Symfony的问题跟踪器上

S3.3自动装配功能使将控制器定义为服务更加容易。

将控制器定义为服务背后的通常动机是避免注入容器。 换句话说,您应该显式注入控制器使用的每个服务。 自动装配功能允许您使用动作方法注入,因此您不必在构造函数中注入一堆东西。

但是,基本的Symfony控制器类提供了许多使用大约12种不同服务的辅助函数。 确实一次注射这些药物确实很痛苦。 我曾以为自动接线功能可以为您解决这个问题,但我想不是。

因此,您基本上需要在服务定义中添加对setContainer的调用。 就像是:

AppBundle\Controller\:
    resource: '../../src/AppBundle/Controller'
    public: true
    [[setContainer, ['@service_container']]]
    tags: ['controller.service_arguments']

自动接线功能是一项尚在开发中的工作,因此如果将它更改为3.4 / 4.0,我不会感到惊讶。

此问题已由PR#23239修复,并在Symfony 3.3.3中提出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM