Symfony 3.4.3 - 服务容器:错误要求您提供值

时间:2018-02-02 15:10:25

标签: php forms dependency-injection dto symfony-3.4

我花了很多时间,但我没有解决它 我想在我的控制器

的操作中传递LoggerInterface

这是我的service.yml

# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
    #parameter_name: value

services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: true
        # automatically registers your services as commands, event subscribers, etc.
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false

    # makes classes in src/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    AppBundle\:
        resource: '../../src/AppBundle/*'
        # you can exclude directories or files
        # but if a service is unused, it's removed anyway
        exclude: '../../src/AppBundle/{Entity,Repository,Tests}'

    # 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']

    # AppBundle\Controller\ArticlesController:
    appbundle.form.type.articles:
        class: AppBundle\Controller\ArticlesController 
        autowire: false
        tags: ['controller.service_arguments']

这是我的控制器

<?php
//AppBundle\Controller\ArticlesController.php
namespace AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use AppBundle\Dto\ArticlesRequest; 
use AppBundle\Form\ArticlesType;
use Psr\Log\LoggerInterface; 



class ArticlesController extends Controller
{
    public function ListAction()
    {
        return $this->render("@App/Articles/list.html.twig");
    }

    public function CreateAction(Request $request, LoggerInterface $logger) 
    {
        $createArticleRequest = new ArticlesRequest();

        $form = $this->createForm(ArticlesType::class, $createArticleRequest); 
        return $this->render("@App/Articles/create.html.twig", array('form' => $form->createView()));
        //return $this->render("@App/Articles/create.html.twig");

    }

    public function EditAction()
    {
        return $this->render("@App/Articles/edit.html.twig");
    }

    public function DeleteAction()
    {
        return $this->render("@App/Articles/delete.html.twig");
    }

}

这是错误消息:

Controller&#34; AppBundle \ Controller \ ArticlesController :: CreateAction()&#34;要求您为&#34; $ logger&#34;提供值。论点。参数可以为空,并且没有提供空值,没有提供默认值,或者因为在此之后存在非可选参数。

2 个答案:

答案 0 :(得分:0)

我解决了这段代码

而不是

public function CreateAction(Request $request, EntityManagerInterface $logger) 

这个

public function CreateAction(Request $request, EntityManagerInterface $logger = null) 

仅添加= null

答案 1 :(得分:0)

在appbundle.form.type.articles下的services.yml中,您需要将autowire传递为true。否则,symfony无法将依赖项注入您的服务中。
如果按照上述建议添加空默认值,则将无法记录任何内容,因为您无法登录空值。而且,如果您不想记录任何内容,只需从参数中删除LoggerInterface。