Symfony – Creating a ParamConverter – Fails to register the converter name

I created a custom @ParamConverter to manage editing a student record based on a Route of /student/{id},
but when I run it I get an Exception "No converter named ‘asymatic_param_converter’ found for conversion of parameter ‘id’.

The error is caused because in vendor/sensio/framework-extra-bundle/src/Request/ParamConverter/ParamConverterManager.php ,
in function applyConverter(Request $request, ParamConverter $configuration)
my converterName is not in the array of $this->namedConverters;

The array of namedConverters is created by the add(ParamConverterInterface $converter, $priority = 0, $name = null)
It is called 4 times, once each for ‘doctrine.orm’ and ‘datetime’ and twice for my custom param converter,
once from the services.yaml definition and once from the @ParamConverter annotation.

But in neither case is the ‘name’ being passed to this "add() function.

Question: What am I missing or what param/argument do I have to specify to get the name of my custom converter passed to the ParamConverterManager so that it gets added to the list of converters?

Symfony version 5.0.7 on SUSE Linux 4.12.14

Relevant Extracts from configuration and classes are:-

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

    # set up a custom ParamConverter to catch edits of non-existent record id's 
    app.asymatic_param_converter:
        class: AppParamConverterAsymaticParamConverter
        properties: {name: 'Goldberg' }
        tags: { name: 'request.param_converter', converter: 'asymatic_param_converter' }
    ...

Student Controller (part):-

       /**
         * @Route("/student/{id}", name="student_edit", requirements={"id":"d+"} )
         * @ParamConverter(converter="asymatic_param_converter", name="id", class="AsymaticParamConverter" )
         */
    public function edit(Request $request, Student $student, StudentRepository $studentRepo)
    {
        $form = $this->createForm(AppFormStudentType::class, $student);
        $form->handleRequest($request);
        .... 

in {project_root}/src/ParamConverter/AsymaticParamConverter.php

namespace AppParamConverter;

use SensioBundleFrameworkExtraBundleConfigurationParamConverter;
use SensioBundleFrameworkExtraBundleRequestParamConverterDoctrineParamConverter;
use SensioBundleFrameworkExtraBundleRequestParamConverterParamConverterInterface;
use SymfonyComponentHttpFoundationRequest;

use DoctrineCommonPersistenceManagerRegistry;
use DoctrineDBALTypesConversionException;
use DoctrineORMEntityManagerInterface;
use DoctrineORMNoResultException;
use SymfonyComponentExpressionLanguageExpressionLanguage;
use SymfonyComponentExpressionLanguageSyntaxError;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;

class AsymaticParamConverter extends DoctrineParamConverter implements ParamConverterInterface
{
    public function __construct(ManagerRegistry $registry = null, ExpressionLanguage $expressionLanguage = null, array $options = [])
    {
         parent::__construct($registry, $expressionLanguage, $options);
    }
    
         // to assist with debugging sensio... ParamConverterManager.php
    public function getMyName() {
        return "AsymaticParamConverter";
    }

        // following 2 functions fulfil ParamConverterInterface requirements
        // and are currently taken directly from DoctrineParamConverter
    public function apply(Request $request, ParamConverter $configuration)
    {
        ... 
    }
     public function supports(ParamConverter $configuration)
    {
        ....
    }
}

Lastly, looking for where the names for ‘doctrine.orm’ come from, in the compiled Container varcachetestContainerUL92jrbApp_KernelTestDebugContainer.php
I can see the ParamConverters are loaded from the following function

 /**
     * Gets the private 'sensio_framework_extra.converter.manager' shared service.
     *
     * @return SensioBundleFrameworkExtraBundleRequestParamConverterParamConverterManager
     */
protected function getSensioFrameworkExtra_Converter_ManagerService()
{
    $instance->add(($this->privates['app.asymatic_param_converter'] ?? 
        $this->getApp_AsymaticParamConverterService()), 0, NULL);
    $instance->add(($this->privates['AppParamConverterAsymaticParamConverter'] ?? 
        $this->getAsymaticParamConverterService()), 0, NULL);
    $instance->add(($this->privates['sensio_framework_extra.converter.doctrine.orm'] ?? 
        $this->getSensioFrameworkExtra_Converter_Doctrine_OrmService()), 0, 'doctrine.orm');

And these NULLS are the issue; If I replace them with ‘asymatic_param_converter’ the Exception is not thrown, and I can use the ParamConverter’s apply() function. But that’s not a solution!

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *