french_dev
french_dev

Reputation: 2177

Symfony2 "Parameter "" for route "" must match "[^/]++" ("" given) to generate a corresponding URL."

I have this routes file:

indexRechercheZones:
    path:     /gestionzonestechniques
    defaults: { _controller: MySpaceGestionEquipementsTechniquesBundle:GestionZonesTechniques:indexZonesTechnique }
    requirements:
    methods: GET

modifierZones:
    path:     /gestionzonestechniques/modifier/{nom}
    defaults: { _controller: MySpaceGestionEquipementsTechniquesBundle:GestionZonesTechniques:modifierZonesTechnique }
    requirements:
    methods: GET

modifierZones_process:
    path:     /gestionzonestechniques/modifier/process/{nom}
    defaults: { _controller: MySpaceGestionEquipementsTechniquesBundle:GestionZonesTechniques:modifierZonesTechnique }
    requirements:
    methods: POST

Now when I want to proceed to the indexRechercheZones route, an error occured:

An exception has been thrown during the rendering of a template ("Parameter "nom" for route "modifierZones" must match "[^/]++" ("" given) to generate a corresponding URL.") in MySpaceGestion...sBundle:...:indexZonesTechniques.html.twig at line 71.

In my twig line 71, I have this code:

<a href="{{ path('modifierZones', {'nom': zonetechnique.nom}) }}"><button class="btn btn-warning btn-xs">Modifier</button></a>

I think that is a regex problem, a problem with the url writing rule for symfony, but I don't how I can fix this error. I have tried some thing like adding this line in my route:

indexRechercheZones:
    path:     /gestionzonestechniques/
    defaults: { _controller: MySpaceGestionEquipementsTechniquesBundle:GestionZonesTechniques:indexZonesTechnique }
    requirements:
        nom:    \d+ 
    methods: GET

or like this at the requirements:

requirements:
    nom:    '[a-zA-Z0-9-_\/-\s.^]+'

But it does not match. Someone could help?

Thank you...

Upvotes: 7

Views: 19002

Answers (2)

Eugene Kaurov
Eugene Kaurov

Reputation: 2991

I prefer to use annotation instead of .yml

#[Route('/gestionzonestechniques/{nom}', name: 'indexRechercheZones', requirements: ['nom' => '\d+'], methods: ['GET'])]
public function action(Request $request): JsonResponse
{}

And yes, the error messages tells that $nom value does not match your pattern '\d+'.

You need either control the value before to generate the route URI either to change the pattern to '.*'

Upvotes: 0

user4460771
user4460771

Reputation:

Like @Coussinsky said, some values for your field "nom" are empty on your database.

You could make this parameter to null if you want, or just change the empty values on your database.

Upvotes: 10

Related Questions