Skip to content

Instantly share code, notes, and snippets.

@tomasnorre
Last active January 31, 2024 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tomasnorre/7eeef0ab8072d1f639bd5119a4d05239 to your computer and use it in GitHub Desktop.
Save tomasnorre/7eeef0ab8072d1f639bd5119a4d05239 to your computer and use it in GitHub Desktop.
Symfony - controller.service_arguments

I'm like 6 hours old in working with symfony, and I really love the documentation, but this one bit I don't get what I'm doing wrong.

When calling my url: /game/complete?personId=1&exerciseId=2 i get the error:

Could not resolve argument $personId of "App\Controller\GameController::complete()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?

Problem Solved: I was missing the params in my routing annotation, see updated file.

<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\Exercise;
use App\Entity\Person;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
class GameController extends AbstractController
{
/**
* @Route("/game", name="game")
*/
public function game()
{
return $this->render('game/index.html.twig', [
'controller_name' => 'GameController',
'exercise' => $this->pickRandomExercise()
]);
}
/**
* @param int $personId
* @param int $exerciseId
*
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/game/complete", name="game-complete", defaults={"personId": 1, "exerciseId": 1})
*/
public function complete(int $personId, int $exerciseId)
{
/** @var Person $person */
$person = $this->getDoctrine()->getRepository(Person::class)->find($personId);
$exercise = $this->getDoctrine()->getRepository(Exercise::class)->find($exerciseId);
$person->addCompletedExercise($exercise);
return $this->redirectToRoute('game');
}
# 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:
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
@ZhineSmathew
Copy link

ZhineSmathew commented Jan 31, 2024

i Got this same error

" Could not resolve argument $email of "App\Controller\CustomerSignupController::checkbusinessemail()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"? "

  • Add above Mentioned code in services.yaml file but is not resolved
# my symfony call method, Here the issue 
public function checkBusinessEmail($email)
    {
        print_r($email);die;
        $lean_tcard_params = $this->getParameter('lean_tcard');
        $checkUserExistsApiUrl = $lean_tcard_params['api_url'] . 'user/email-check';
        $data = [
            'email' => $email
        ];
        $checkUserExistsApi = $this->executeCurl($checkUserExistsApiUrl, $data);
        $status = $checkUserExistsApi['result'];
        return $status;
    }

@ZhineSmathew
Copy link

ZhineSmathew commented Jan 31, 2024

App\Controller\:
    resource: '../src/Controller/'
    tags: ['controller.service_arguments']

When i add this code got another error

" Invalid service id: "App\Controller" in /Applications/XAMPP/xamppfiles/htdocs/saas/src/../config/services.yaml (which is being imported from "/Applications/XAMPP/xamppfiles/htdocs/saas/src/Kernel.php") "

@tomasnorre
Copy link
Author

It's really cool that you followed up on this, I totally forgot about it. Perhaps it will help someone else in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment