Error service symfony : A namer must be configured

Error service symfony : A namer must be configured

Problem Description:

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

My service:

<?php

namespace AppServiceNamers;


use VichUploaderBundleMappingPropertyMapping;
use VichUploaderBundleNamingDirectoryNamerInterface;
use AppEntityUsers;

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: AppServiceNamersVichNamer

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
    AppEventSubscriberEasyAdminSubscriber:
        tags:
            - { name: 'doctrine.event_subscriber', event: preUpdate }
    AppServiceNamersVichNamer:
        public: true
        tags: 
            - { name: 'vich.namedirectory' }

My error:

A namer must be configured.

I user symfoy 6.2

thank you

Solution – 1

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: AppServiceNamersVichNamer
        namer:
          service: VichUploaderBundleNamingPropertyNamer
          options: { property: 'slug' }

You can see all options working with namer here : namers

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject