I updated my project to Symfony 3.3. I want to use the new autoconfigure feature for services. I tried to get rid of $this->get() but I have errors in Controllers and Commands.
With the code sample below in a Controller, I have this error:
recapitulatifCollesAction() requires that you provide a value for the "$checkGele" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.
In commands, I don't know how to get rid of $container->get() at all.
Do you have an idea how I can get this to work ?
Controller:
public function recapitulatifCollesAction($estEnCours, CheckGeleService $checkGele)
{
// ...
$checkGele->getGeleAutorisation($colle);
// ...
}
My config:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
Edit : new error after modifying config.yml
For controllers you need also add the service argument resolver to autowire service in "actions" methods. This's all about default autowiring in 3.3:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
AppBundle\:
resource: '../../src/AppBundle/*'
exclude: '../../src/AppBundle/{Controller, Entity, Repository}'
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: [ controller.service_arguments ]
Related
I am working on upgrade Symfony 3.3 to 4.4 and almost done with it, but I have one last issue.
I enabled autowiring with default config, but whole project uses lots of #ParamConverter conversions without annotation.
Problem: ParamConverter with auto_convert tries to convert services and classes that are mentioned to controller by typehinting for autowiring (not entities).
Controller:
/**
* #Route("/report", name="report_page")
*/
public function report(
Request $request,
FileManager $fileManager
): Response {
// code
}
Error:
The class 'App\Service\FileManager' was not found in the chain configured namespaces App\Entity.
Service 'App\Service\FileManager' exists and works correctly.
ParamConverter config (by default 'request: {converters: true, auto_convert: true}'):
sensio_framework_extra:
router:
annotations: false
Doctrine config:
doctrine:
dbal:
default_connection: default
connections:
default:
# config
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
Base:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: App\Entity
Controller setting (services.yaml):
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{DataFixtures,Entity,Objects,Repository,Utils}'
App\Controller\:
resource: '../src/Controller'
tags: ['controller.service_arguments']
I understand that I can set 'sensio_framework_extra.request.auto_convert' to false and use #ParamConverter annotation everywhere in controllers, but there are too many places with this conversion and I try to get default behavior, so additional #ParamConverter annotations would be extra.
Just in case: I tried to disable auto_convert and everything worked correctly (services were autowired and entities were passed to controllers).
Also I checked that I installed last available versions of packages and bundles.
Need some hints on it. Probably I missed something. I tried to find exact way how ParamConverter works by default and how it checks that class is entity or not, but it is too complicated and I decided that I dug too deep.
EDIT:
Probably I have some issue with autowiring's config and not paramconverter. But I checked php bin/console debug:autowiring and got all needed services as available via autowiring.
It looks like symfony is trying to convert the fileManager like an entity.
Try moving the injected service(s) into the __construct method of the controller (and add the property to the controller) like:
private $fileManager;
...
public function __construct(FileManager $fileManager) {
$this->fileManager = $fileManager;
}
and then access it in the report method:
$this->fileManager
I trying to install SonataAdmin on my Symfony Project but at the end of the part-2 of the documention when i'm trying to go on "http://localhost:8000/admin/" I have a error : "You have requested a non-existent service "admin.category" in . (which is being imported from "C:\wamp64\www\Sonata/app/config\routing.yml"). Make sure there is a loader supporting the "sonata_admin" type."
I have no idea why, i give give my all my parameters code maybe it's can help you to understand my problem.
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
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
# 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'
`
The indentation is going wrong i add you a picture of this file. Service code
The sonata admin services must be public. In your config, you have default as public: false and that is why you get this error.
So you have 2 options:
Specify public: true for your admin service (in your example file)
Or the better way: create a new services file (eg admin.yml) where you dont use these defaults (the _defaults key with public: false). Public is true by default, so you don't have to specify that by _defaults. In this case you must import your new file in config.yml to work:
Top of app/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: admin.yml }
app/admin.yml content:
services:
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
I think you did a mistake by writing your category.admin service in: Sonata/app/config/routing.yml,
instead of Sonata/src/YourAdminBundle/Resources/config/services.yml
Run this command on terminal. Because You might have missed installing
php composer.phar require sonata-project/doctrine-orm-admin-bundle
After This Add this code below to your AppKernel.php
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
// set up basic sonata requirements
// ...
new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
// ...
);
}
I am using JMSSerializerBundle with Symfony3. I am using TimestampableEntity trait. What I want to achieve is to receive that trait with JMSSerializer.
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
use Gedmo\Timestampable\Traits\TimestampableEntity;
class Thread
{
use TimestampableEntity;
use SoftDeleteableEntity;
(...)
}
I have added metadata to the jms_serializer config like this:
config.yml
jms_serializer:
enable_short_alias: false # controls if "serializer" service is aliased to jms_serializer.serializer s
metadata:
directories:
- { path: "%kernel.root_dir%/Resources/Gedmo/serializer", namespace_prefix: 'Gedmo\Timestampable\Traits' }
\app\Reources:
Gedmo\Timestampable\Traits\TimestampableEntity:
exclusion_policy: ALL
properties:
created_at:
expose: true
But it is not working.
I know that I can configure it to use my Thread class and expose all necessary fields but I wonder if it is possible for traits.
So I have my KnpGaufretteBundle configured like this:
knp_gaufrette:
adapters:
local_shared:
local:
directory: "%shared.dir%"
create: false
filesystems:
local_shared:
adapter: local_shared
alias: defaul_fs
Then I need to inject it to my own service, I do this:
acme.user_manager:
class: %acme_user.user_manager.class%
arguments: [#doctrine.orm.entity_manager, #security.context]
calls:
- [setFilesystem, [#default_fs]]
But this breaks with exception The service definition "default_fs" does not exist.
If I use #gaufrette.local_shared_filesystem as a parameter instead of #default_fs then it works like expected. But I would like to use an alias.
Please help.
I'm assuming that you noticed that you have a typo in your config declaration:
alias: defaul_fs
has no "t"
Here are params.
app/config/parameters.yml
parameters:
test:
enabled: true
validate: true
And this is service which I want to configure with test param from previous file.
MyBundle/Resources/config/services.yml
imports:
- { resource: "parameters.yml" }
parameters:
services:
my.form.type:
class: My\Bundle\Form\Type\MyType
arguments:
- %test%
Import doesn't work this way. How should I do it?
PS I know I can refer to #service_container. But the point is to pass array explicitly via yaml.
you can ommit ...
imports:
- { resource: "parameters.yml" }
parameters:
... parameters.yml should automatically be parsed and the parameters should be available for injection if you surround them with %.
Try:
services:
my.form.type:
class: My\Bundle\Form\Type\MyType
arguments: ["%test%"]
alias: my_form_type