为什么我的控制器看不到我添加的服务?

时间:2019-03-28 17:10:30

标签: php symfony service symfony4

我在控制器中添加服务/帮助程序遇到困难。据我所知,自新版Symfony4以来,它应该可以自动连接服务,但是仍然出问题了。 我得到的错误是:

  

InvalidArgumentException

     

无法确定“ App \ Controller \ KibbleController :: index()”的控制器参数:$ dinoHelper参数使用不存在的类或接口进行类型提示:“ App \ Service \ DinoHelper”。

我尝试使用新的DinoHelper在控制器内部添加服务,但效果很小。

src / Controller / KibbleController

namespace App\Controller;

use App\Entity\SimpleKibble;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Exception;
use App\Service\DinoHelper;

class KibbleController extends AbstractController
{
    /**
     * @Route "/"
     * @param Request $request
     * @param DinoHelper $dinoHelper
     * @return Response
     */
    public function index(Request $request, DinoHelper $dinoHelper)
    {
        $form = $this->createFormBuilder()
            ->add('Dino', TextType::class)
            ->add('submit', SubmitType::class, ['label' => 'Submit'])
            ->getForm();

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();

            $dinoHelper->getKibbleEntity($data['Dino']);

src / Service / DinoHelper.php

<?php

namespace App\Service;

use App\Entity\Dodo;
use App\Entity\Dilo;
use App\Entity\Raptor;

class DinoHelper
{

    public function getKibbleEntity(string $name)
    {
        switch ($name)
        {
            case 'Dodo' :
                $dinoEntity = new Dodo();
                break;
            case 'Dilo' :
                $dinoEntity = new Dilo();
                break;
            case 'Raptor' :
                $dinoEntity = new Raptor();
                break;
        }
        return isset($dinoEntity) ? $dinoEntity : null;

    }
}

services.yaml

# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: 'en'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

0 个答案:

没有答案