Getting FileLoaderLoadException in symfony - php

I am new to Symfony.I am trying to learn File upload in Symfony.I am getting below error:
(3/3) FileLoaderLoadException
The file "D:\wamp\www\symfony_project\app/config\services.yml" does not contain valid YAML in D:\wamp\www\symfony_project\app/config\services.yml (which is being imported from "D:\wamp\www\symfony_project\app/config\config.yml").
config.yml
parameters:
brochures_directory: '%kernel.symfony_project%/../web/uploads/brochures'
services.yml
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
public: true
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
public: true
AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
FileUploader.php
namespace AppBundle\Service;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploader
{
private $targetDir;
public function __construct($targetDir)
{
$this->targetDir = $targetDir;
}
public function upload(UploadedFile $file)
{
$fileName = md5(uniqid()).'.'.$file->guessExtension();
$file->move($this->getTargetDir(), $fileName);
return $fileName;
}
public function getTargetDir()
{
return $this->targetDir;
}
}

As the error says, services.yml is not valid YAML. Remove $ sign before targetDir in line:
$targetDir: '%brochures_directory%'
So it will be:
AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
I guess that this is your issue, but I cannot be sure as I don't know how looks the whole content of this file.

your services.yml does not contain valid YAML (Error message can't be more clear ;) )
Try this kind of code :
/***/
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
public: true
admin.file_uploader:
class: AppBundle\Service\FileUploader
arguments: [~, '%brochures_directory%', ~]

Change
parameters:
brochures_directory: '%kernel.symfony_project%/../web/uploads/brochures'
To
parameters:
brochures_directory: '%kernel.symfony_project%/../web/uploads/brochures'

Should work now:
# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags:
- { name: "controller.service_arguments" }
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
public: true
admin.blog_post:
class: AppBundle\Admin\BlogPostAdmin
arguments: [~, AppBundle\Entity\BlogPost, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Blog post }
public: true
AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'

You have space in front of AppBundle\Service\FileUploader:
→ ﹍AppBundle\Service\FileUploader:
arguments:
targetDir: '%brochures_directory%'
Hint: In future use Yaml linter:
bin/console lint:yaml app/config/services.yml

Related

Doctrine error during upgrading Symfony 3.4 to 5.4

I'm new at Symfony stuff but I have to try to upgrade some aplication from Symfony 3.4 to 5.4.
During moving code of repositories I've run into problem with autowiring them.
I've read that it should be done by switching them to ServiceEntityRepository and making them a constructor.
So I made all of them something like below:
namespace App\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use App\Entity\ApiBackup;
use Doctrine\Persistence\ManagerRegistry;
class ApiBackupRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ApiBackup::class);
}
This however resulted with new kind of error which I wasn't able to find:
Invalid repository class 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\EntityRepository'. It must be a Doctrine\ORM\Exception\EntityRepository.
Any ideas how to get rid of such error?
edit: services.yml
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: true
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity, Repository,Tests,Kernel.php,Query, DataForm}'
App\DataForm\:
resource: '../src/DataForm/*'
public: true
shared: false
App\Helpers\ZoneStatsService:
public: true
App\Helpers\CalendarEnabledService:
public: true
App\Helpers\MourningEnabledService:
public: true
App\Helpers\FormStepResolver:
autowire: true
public: true
App\Helpers\ApiErrorHandler:
autowire: true
public: true
App\Helpers\DnsApiHelper:
autowire: true
public: true
App\DataForm\Partner:
autowire: false
App\DataForm\MenuBloki\MenuBloki:
autowire: false
App\DataForm\MenuBloki\MenuBlokiFile:
autowire: false
App\Helpers\ResourceLoaderService:
arguments: ['#service_container', '%memcache_expiration_value%']
jms.js.extractor:
class: App\Translation\JavaScriptTranslationExtractor
tags:
- { name: jms_translation.file_visitor}

Symfony DependencyInjection : import multiple resources (yaml)

I am trying to implement Symfony Dependency Injection Component (https://symfony.com/doc/current/components/dependency_injection.html) in a non Symfony project.
It works well if I put all my services and parameters into a single "services.yaml" file.
Ex:
# services.yaml
parameters:
mysql.host: "127.0.0.1"
mysql.database: "database"
mysql.username: "root"
mysql.password: ""
oracle.database: ""
oracle.username: ""
oracle.password: ""
cookie.domain: ".site.local"
cookie.lifetime: 0
cookie.useHttps: true
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
OtherNamespace\:
resource: '../someHomemadeLibrary/src/*'
OtherNamespace\Database\Drivers\MySqlDriver:
class: OtherNamespace\Database\Drivers\MySqlDriver
arguments:
$host: "%mysql.host%"
$database: "%mysql.database%"
$username: "%mysql.username%"
$password: "%mysql.password%"
# ...
As the file is getting very large, I wanted to split it into different files, as explained here : https://symfony.com/doc/current/service_container/import.html#importing-configuration-with-imports
So I tried :
# index.php
$containerBuilder = new ContainerBuilder();
$loader = new YamlFileLoader($containerBuilder, new FileLocator('config/'));
$loader->load('services.yaml');
# services.yaml
imports:
- { resource: "databases.yaml" }
- { resource: "sessions.yaml" }
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
OtherNamespace\:
resource: '../someHomemadeLibrary/src/*'
# databases.yaml
parameters:
mysql.host: "127.0.0.1"
mysql.database: "database"
mysql.username: "root"
mysql.password: ""
services:
OtherNamespace\Database\Drivers\MySqlDriver:
class: OtherNamespace\Database\Drivers\MySqlDriver
arguments:
$host: "%mysql.host%"
$database: "%mysql.database%"
$username: "%mysql.username%"
$password: "%mysql.password%"
# ...
But in this configuration, I get the following error :
Fatal error: Uncaught
Symfony\Component\DependencyInjection\Exception\RuntimeException:
Cannot autowire service "OtherNamespace\Database\Drivers\MySqlDriver": argument
"$host" of method "__construct()" is type-hinted "string", you
should configure its value explicitly.
If I switch the import order, then it is the CookieSession that has a problem. It is like the import is overriding the parameters and services to only use the last one ? How can I split my services and their parameters in multiple files ?
Thank you !
The problem was that my services were correctly imported from my databases.yaml and other yaml into my services.yaml file, but in this file, I also had automatic classes conversion into services.
So my services defined were reloaded/converted, but without the parameters and all. I added exclusions and it works!
App\:
resource: '../src/*'
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
OtherNamespace\:
resource: '../someHomemadeLibrary/src/*'
exclude:
[
'.../theClassFileDeclaredInAnotherYaml.php',
'../or/a/whole/folder/not/to/be/loaded/again/*.php'
]
Thanks to #NicoHaase, #Cerad and #WillB. for their help !

Autoconfiguration replace imported services

When I import services in the main configuration:
imports:
- { resource: services/attribute_loaders.yaml }
The services in the included file will be replaced with an autoconfigured version, so I've missed all my configured tags:
services:
App\Infrastructure\Bridge\Doctrine\EventListener\AttributeLoader\OrderAttributeLoader:
autowire: true
tags:
- name: 'doctrine_mongodb.odm.event_listener'
entity: 'App\Infrastructure\Bridge\Doctrine\EventListener\AttributeLoader\OrderAttributeLoader'
event: 'postLoad'
As a workaround, I've excluded my service from autoconfigure.
Is there any way to decompose services into several files?
That's exactly how you do it.
If you are going to use multiple files to configure your services, you simply can't define the same service twice.
Your definitions need to be specific enough so they do not overlap.
If you are going to use broad resource settings, you will need to add all the corresponding excludes so services defined in different files are not defined before it's time:
services:
App\:
resource: '../src/*'
exclude:
- '../src/Infrastructure/Symfony/DependencyInjection'
- '../src/Infrastructure/Symfony/Kernel.php'
- '../src/Tests'
- '../src/Messenger'
Services on src/Messenger, that belong to the App\Messenger namespace, can be defined independently on a different file:
E.g. something like this:
# messenger_services.yaml
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false
App\Messenger\CommandHandler\:
resource: '../../src/Messenger/CommandHandler/*'
tags:
- {name: 'messenger.message_handler', bus: command.bus}
App\Messenger\EventHandler\:
resource: '../../src/Application/EventHandler/*'
tags:
- {name: 'messenger.message_handler', bus: event.bus}

service error after upgraded from symfony 3.3 to symfony 3.4

I've upgraded my symfony projet version from 3.3 to 3.4 a few month ago and everything woked find but i recently tried to ad an event listener and then i've got that error
The configuration key "public" cannot be used to define a default value in "/home/pc-dev/Labs/Projets-jebuy/je-buy.com/app/config/services.yml". Allowed keys are "private", "tags", "autowire", "autoconfigure", "bind" in /home
/pc-dev/Labs/Projets-jebuy/je-buy.com/app/config/services.yml (which is being imported from "/home/pc-dev/Labs/Projets-jebuy/je-buy.com/app/config/config.yml").
obviuosly nothing seriuos i edited the default service.yml file from this
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
twig.extension:
class: AppBundle\Twig\Extension\MenuExtension
arguments:
- '#doctrine'
tags:
- {name : twig.extension }
# service.extension:
# class: AdminBundle\DependencyInjection\AdminExtension
# arguments: []
# tags: []
to this (because in symfony 3.4 or 4, all services are by default private)
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
private: true
twig.extension:
class: AppBundle\Twig\Extension\MenuExtension
arguments:
- '#doctrine'
tags:
- {name : twig.extension }
# service.extension:
# class: AdminBundle\DependencyInjection\AdminExtension
# arguments: []
# tags: []
but when i tried to run the server i've now got this error
The service "templating.loader.cache" has a dependency on a non-existent service "templating.loader.wrapped".
and i don't now how to solve it and where is that template.loder.wrapped service.

Do steel need register Twig Extensions in the services.yml?

I learning Symfony3 on https://knpuniversity.com. Now on create their own filters in Twig (https://knpuniversity.com/screencast/symfony-services/create-twig-extension). In this video the author creates a class filter, but when using this filter in the Twig template:
<dd>{{ genus.funFact|markdownify }}</dd>
Symfony crashes with the error, and the author of the video says that the filter has used need to say Symfony about new filter Twig'. This is done using the register Twig extension as service in the services.yml. But, I have it all working, without errors, without any registrations in service.yml.
Do steel need register Twig Extensions in the services.yml?
Or is now not required and determines its own Twig extension?
I have a Symfony 3.3.6 (in video used 3.0.0).
Here listing my service.yml:
parameters:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
AppBundle\Service\MarkdownTransformer:
arguments:
- '#markdown.parser'
- '#doctrine_cache.providers.my_markdown_cache'
public: true
# AppBundle\Twig\MarkdownExtension:
# arguments: ['#AppBundle\Service\MarkdownTransformer']
# tags:
# - { name: twig.extension }
# public: true
In the bottom I'm added the settings block, which registers a new TWIG extension. Without this block, Symfony throws an exception (in the author of the lesson). But I have everything working without this block. So for example, I have it commented out.

Categories