У меня проблемы с добавлением службы / помощника в мой контроллер.Начиная с нового 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