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
Related
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 ]
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"
let's say that you have the three following symfony2 config files (extremely simplified), where the file loaded by AppKernel as the current environment is the first one, app/config/config_env.yml:
app/config/config_env.yml:
imports:
- { resource: config2.yml }
- { resource: config3.yml }
parameters:
param: one
app/config/config2.yml:
imports:
- { resource: config4.yml }
parameters:
param: two
app/config/config3.yml:
imports:
- { resource: config5.yml }
parameters:
param: three
app/config/config4.yml:
parameters:
param: four
app/config/config5.yml:
parameters:
param: five
Whenever I refer to param from my code, can you help me understand what is the value associated with param (one, two, three, four or five), and why? Or, in other words, what are Symfony2 precedence rules regarding imports?
Thanks,
Rodrigo
Let's think about what symfony is doing.
First of all, it loads the config_env.yml.
imports:
- { resource: config2.yml }
- { resource: config3.yml }
parameters:
param: one
Next step: Load the imports.
imports:
- { resource: config4.yml }
parameters:
param: two
imports:
- { resource: config5.yml }
parameters:
param: three
parameters:
param: one
Next step: combine them
imports:
- { resource: config4.yml }
- { resource: config5.yml }
parameters:
param: two
param: three
param: one
Next step: Load the imports
parameters:
param: four
parameters:
param: five
parameters:
param: two
param: three
param: one
Next step: combine them
parameters:
param: four
param: five
param: two
param: three
param: one
Now it's important to know that symfony overrides set parameters.
So what's the answer? At the end param is one. Try it.