symfonyservicevichuploaderbundle

Error service symfony : A namer must be configured


I want to use create folder function with vich considering id.

My service:

<?php

namespace App\Service\Namers;


use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Naming\DirectoryNamerInterface;
use App\Entity\Users;

class VichNamer implements DirectoryNamerInterface {

    public function directoryName(object $object, PropertyMapping $mapping): string {
        return 'test';
    }
    
}

my vich_uploader.yaml:

vich_uploader:
    db_driver: orm

    metadata:
        type: attribute

    mappings:
    
        tattoo_images:
            uri_prefix: /images/
            upload_destination: '%kernel.project_dir%/public/images/'
            directory_namer: App\Service\Namers\VichNamer

My services.yaml:

parameters: images_directory: '%kernel.project_dir%/public/uploads'

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

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
    App\EventSubscriber\EasyAdminSubscriber:
        tags:
            - { name: 'doctrine.event_subscriber', event: preUpdate }
    App\Service\Namers\VichNamer:
        public: true
        tags: 
            - { name: 'vich.namedirectory' }

My error:

A namer must be configured.

I user symfoy 6.2

thank you


Solution

  • The namer is used to name the files and directories it saves to the filesystem You can add nammer by default like this in your config:

    vich_uploader:
    .....
    
      mappings:
    
        tattoo_images:
            uri_prefix: /images/
            upload_destination: '%kernel.project_dir%/public/images/'
            directory_namer: App\Service\Namers\VichNamer
            namer:
              service: Vich\UploaderBundle\Naming\PropertyNamer
              options: { property: 'slug' }
    

    You can see all options working with namer here : namers