Romain
Romain

Reputation: 61

After Update from SF4.4 to 5.4, generateUrl error : "Some mandatory parameters are missing" for route with optional parameters

Since I updated Symfony 4.4 to symfony 5.4, i have a last problem to resolve.

Methode "generateURl()" in controller, and "path()" in twig template return an exception for route with optional param that i dont specify for generateURl method in controller or path methode in twig:

Some mandatory parameters are missing ("optionalvarname") to generate a URL for route "the_route".

Otherwise routes themselves work well. I can connect on its without specifie the defaut param. Everythings worked well in symfony 4.4

But in Symfony 5.4 I can't omit to specify optional param for this two methods.

Exemple with an "action" optional parameters :

/**
 * @Route("/{id}/edit/{action}", name="tenant_vente_edit", methods={"GET","POST"})
 */
public function edit(
    Request $request, Vente $vente,string $action="update",Panier $panier,
    MouvementRepository $mouvementRepository,VenteUtils $venteUtils): Response
{

.....

$this->generateUrl('tenant_vente_edit',['id'=>$vente->getId()])

I omited "action" optional param and this generate exception : Some mandatory parameters are missing ("action") to generate a URL for route "tenant_vente_edit".

But the route itslef works...when there is no action parameter in url.

To "generateUrl()" and "path()" works omiting optionals parameters i have to add the defaut value "?update" in the comment :

/**
 * @Route("/{id}/edit/{action?update}", name="tenant_vente_edit", methods={"GET","POST"})
 */
public function edit(
    Request $request, Vente $vente,string $action="update",Panier $panier,
    MouvementRepository $mouvementRepository,VenteUtils $venteUtils): Response
{

In this case i have no error for "generateUrl()" method and "path()" method.

Would you know why i have this behviour ? I don't want to go back on all my route...I have a lot..

Thank you very much for your help and sorry for my english....

My new composer.json for update to 5.4

{
"type": "project",
"license": "proprietary",
"require": {
    "php": ">=8.1.0",
    "ext-ctype": "*",
    "ext-iconv": "*",
    "beberlei/doctrineextensions": "^1.3",
    "composer/package-versions-deprecated": "1.11.99.4",
    "doctrine/annotations": "^1.0",
    "doctrine/doctrine-bundle": "^2.5",
    "doctrine/doctrine-migrations-bundle": "^3.2",
    "doctrine/orm": "^2.11",
    "knplabs/knp-paginator-bundle": "^5.8",
    "knplabs/knp-snappy-bundle": "^1.7",
    "phpdocumentor/reflection-docblock": "^5.3",
    "phpoffice/phpspreadsheet": "^1.14",
    "sensio/framework-extra-bundle": "^6.1",
    "symfony/asset": "5.4.*",
    "symfony/console": "5.4.*",
    "symfony/dotenv": "5.4.*",
    "symfony/expression-language": "5.4.*",
    "symfony/flex": "^1.17|^2",
    "symfony/form": "5.4.*",
    "symfony/framework-bundle": "5.4.*",
    "symfony/monolog-bundle": "^3.1",
    "symfony/process": "5.4.*",
    "symfony/property-access": "5.4.*",
    "symfony/property-info": "5.4.*",
    "symfony/proxy-manager-bridge": "5.4.*",
    "symfony/security-bundle": "5.4.*",
    "symfony/serializer": "5.4.*",
    "symfony/swiftmailer-bundle": "^3.1",
    "symfony/translation": "5.4.*",
    "symfony/twig-bundle": "5.4.*",
    "symfony/validator": "5.4.*",
    "symfony/web-link": "5.4.*",
    "symfony/webpack-encore-bundle": "^1.5",
    "symfony/yaml": "5.4.*",
    "vich/uploader-bundle": "^1.19",
    "twig/extra-bundle": "^2.12",
    "twig/twig": "^2.12"
},
"require-dev": {
    "phpunit/phpunit": "^9.5",
    "symfony/browser-kit": "5.4.*",
    "symfony/css-selector": "5.4.*",
    "symfony/debug-bundle": "5.4.*",
    "symfony/maker-bundle": "^1.0",
    "symfony/phpunit-bridge": "^5.3",
    "symfony/stopwatch": "5.4.*",
    "symfony/web-profiler-bundle": "5.4.*"
},
"config": {
    "preferred-install": {
        "*": "dist"
    },
    "sort-packages": true,
    "allow-plugins": {
        "symfony/flex": true
    }
},
"autoload": {
    "psr-4": {
        "App\\": "src/"
    }
},
"autoload-dev": {
    "psr-4": {
        "App\\Tests\\": "tests/"
    }
},
"replace": {
    "paragonie/random_compat": "2.*",
    "symfony/polyfill-ctype": "*",
    "symfony/polyfill-iconv": "*",
    "symfony/polyfill-php71": "*",
    "symfony/polyfill-php70": "*",
    "symfony/polyfill-php56": "*"
},
"scripts": {
    "auto-scripts": {
        "cache:clear": "symfony-cmd",
        "assets:install %PUBLIC_DIR%": "symfony-cmd"
    },
    "post-install-cmd": [
        "@auto-scripts"
    ],
    "post-update-cmd": [
        "@auto-scripts"
    ]
},
"conflict": {
    "symfony/symfony": "*"
},
"extra": {
    "symfony": {
        "allow-contrib": false,
        "require": "5.4.*"
    }
}

}

Upvotes: 3

Views: 678

Answers (1)

Romain
Romain

Reputation: 61

it seams optional paraméters must be at the end of controller arguments.

In this two cases routes work if you omit optional parameter in url.

First case :

  /**
 * @Route("/{id}/edit/{action}", name="tenant_vente_edit", methods={"GET","POST"})
 */
public function edit(
    Request $request, Vente $vente,Panier $panier,MouvementRepository $mouvementRepository,
    VenteUtils $venteUtils,?string $action=null): Response
{

Second case

  /**
     * @Route("/{id}/edit/{action}", name="tenant_vente_edit", methods={"GET","POST"})
     */
    public function edit(
        Request $request, Vente $vente,Panier $panier,MouvementRepository $mouvementRepository,
       ?string $action=null, VenteUtils $venteUtils): Response
    {

But :

$this->generateUrl('tenant_vente_edit',['id'=>$vente->getId()])

Works only in the second case with "action" optional argument in last position of controller arguments, idem for twig and "path()".

I didnt have this behaviour in symfony 4.4

So no other choice to check all my routes. To avoid this kind of problem i advice to put default value in comment instead of controller arguments.

  /**
     * @Route("/{id}/edit/{action?}", name="tenant_vente_edit", methods={"GET","POST"})
     */
    public function edit(
        Request $request, Vente $vente,Panier $panier,MouvementRepository $mouvementRepository,
        VenteUtils $venteUtils,?string $action): Response

action?(_blank) for null value in this example.

Upvotes: 3

Related Questions