Tuesday, October 22, 2019

Symfony file upload mysterious error

Uncaught PHP Exception Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: "The file "" does not exist" at /var/app/current/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php line 116 {"exception":"[object] (Symfony\\Component\\HttpFoundation\\File\\Exception\\FileNotFoundException(code: 0): The file \"\" does not exist at /var/app/current/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php:116)"}


If you see this error the odds are high that you are trying to upload a file larger than the max values set in you php.ini config file (upload_max_filesize and post_max_size).

Wednesday, October 2, 2019

Symfony service container: binding arguments by name or type

This cool feature was introduced in Symfony 3.4 (https://symfony.com/blog/new-in-symfony-3-4-local-service-binding) but I didn't use till today.

Example: create a custom Monolog channel and inject the service in controller (Symfony 4).
I was thinking that I will have to create some sort of custom service definition in order to inject the service "monolog.logger.mychannel" into my controller, but no, it was way easier.

In services.yaml  just add:



services:
    # default configuration for services in *this* file
    _defaults:
        bind:
            # pass this service to any $mychannelLogger argument for any
            # service that's defined in this file
            $mychannelLogger: '@monolog.logger.mychannel'

Now is super easy to inject the service in controllers by adding "$mychannelLogger" in the list of function parameters:


/**
     * @Route("/{id}", name="project_update", methods={"POST"})
     */
    public function delete(Request $request, LoggerInterface $mychannelLogger): Response
    {
    


Read more about it in Symfony docs: https://symfony.com/doc/current/service_container.html#services-binding