symfony / symfony-docs

The Symfony documentation
https://symfony.com/doc
Other
2.15k stars 5.1k forks source link

[Upload files] parameter or bind? #20039

Closed Nayte91 closed 1 month ago

Nayte91 commented 2 months ago

Hello,

I was following the documentation here https://symfony.com/doc/current/controller/upload_file.html, everything was ok but I was stuck on this part :

Now, create the brochures_directory parameter that was used in the controller to specify the directory in which the brochures should be stored:

# config/services.yaml

# ...
parameters:
    brochures_directory: '%kernel.project_dir%/public/uploads/brochures'

When I was trying (I'm on a 7.1 project), on my own controller, I get an error:

Controller "App\Controller\news\NewsController::create" requires the "$bannersDirectory" argument that could not be resolved. Either the argument is nullable and no null value has been provided, no default value has been provided or there is a non-optional argument after this one.

So I changed a bit my services.yaml, removing this "parameters" part, and adding:

services:
    _defaults:
        autowire: true
        autoconfigure: true
        bind:
            $bannersDirectory: '%kernel.project_dir%/public/uploads/banners'

And it works!

So I was wondering, has the docs a flaw here? Or do I miss a point from newer SF version, about binding and parameters? That totally can be as I have troubles to understand this config/container part.

I can update the doc, but I need to know why my try didn't work, and what is ultimately the most elegant way to do :)

xabbuh commented 1 month ago

Are you sure that this is related to the service and not simply related to the name mismatch (banner vs brochure)?

Nayte91 commented 1 month ago

Hello, Thank you for your time,

I'm pretty sure I managed correctly the name of the variable when I tested (trying with underscore or camelCase, trying few variable names, and adjusting on the controller side), but it's possible that I misstyped something, somewhere.

What I understand from your answer, is that the documentation doesn't worry you, "binding" with the "parameters" key for a controller seems working for you?

I will reproduce with a synthetic example.

xabbuh commented 1 month ago

I have set up a small application locally and played a bit with it. This indeed does not work and we should merge #20043 to fix it.