I work on a symfony project and I want to do something like this in my app/config/parameters.yml
dev.google.api.key: foo
google: %%kernel.environment%.google%
It not works.
I explain : I want to have the variable google to have values depending my environment (env or prod).
So I defined two variable :
dev.google.api.key: foo
prod.google.api.key: bar
I want to have my variable google to be filled with content depending on the environment. The environment is in the variable
%kernel.environment%
So if I do :
google: %kernel.environment%.google
Google is equal to "dev.google" but I want to this string to be evaluated like this %dev.google%. But %%kernel.environment%.google% not works.
An idea ?
Thanks
You can try something like this:
in your config_dev.yml:
parameters:
google.api.key: foo
in your config_prod.yml:
parameters:
google.api.key: bar
in your config.yml:
google: %google.api.key%
Or
Create paramater_dev.yml with
parameters:
google.api.key: foo
Create paramater_prod.yml with
parameters:
google.api.key: bar
in your config_dev.yml:
imports:
- { resource: paramater_dev.yml }
- { resource: config.yml }
in your config_prod.yml:
imports:
- { resource: paramater_prod.yml }
- { resource: config.yml }
in your config.yml:
google: %google.api.key%
Related
I have a specific variable in my .env.dev.local
SRV_INST=s01
Is there a way to use custom env variables in services.yaml in a conditional way similar to when#dev ? I would like to define some services only when SRV_INST=s01 (something like when %env(SRV_INST)% === 's01').
I can use services.php, but I wanted to know if there's a way to do this in services.yaml
you found a solution - but for anyone else, there are multiple other solutions for this:
services:
App\Mailer:
# the '#=' prefix is required when using expressions for arguments in YAML files
arguments: ["#=container.hasParameter('some_param') ? parameter('some_param') : 'default_value'"]
or
services:
# ...
App\Mail\MailerConfiguration: ~
App\Mailer:
# the '#=' prefix is required when using expressions for arguments in YAML files
arguments: ['#=service("App\\Mail\\MailerConfiguration").getMailerMethod()']
you can find more information at https://symfony.com/doc/current/service_container/expression_language.html
Okay, I solved this by importing a .php additional configuration file.
// config/services.yaml
imports:
- { resource: 'services_conditional.php' }
And the services_conditional.php file:
// config/services_conditional.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use SomeCustom\Service\CustomDoubleBla;
return function (ContainerConfigurator $configurator) {
$services = $configurator->services();
if (!empty($_ENV['SRV_INST']) && $_ENV['SRV_INST'] === 's01') {
$services->set(CustomDoubleBla::class)
->decorate('custom_single_bla.service')
// pass the old service as an argument
->args([service('.inner')]);
}
};
I'm using YML parameters inside each bundle to use them as data fixtures as follows:
AppBundle\Resources\config\datafixtures.yml
parameters:
datafixtures:
defaultusers:
0:
name: john
email: john#company1.lol
1:
name: steve
email: steve#company1.lol
GeolocationBundle\Resources\config\datafixtures.yml
parameters:
datafixtures:
cities:
0:
external_service_area: Cantabria
company_area: Santander
1:
external_service_area: Gipuzkoa
company_area: San Sebastian
The problem comes when I import the files in the app config.yml file.
imports:
- { resource: '#AppBundle/Resources/config/datafixtures.yml' }
- { resource: '#GeolocationBundle/Resources/config/datafixtures.yml' }
Instead of merging the trees, it overrides them. Is there a way to make them merge?
Bassically the tree form makes them override each other, so the best way to avoid it is to define each node with a single complete name:
AppBundle\Resources\config\datafixtures.yml
parameters:
datafixtures.defaultusers:
GeolocationBundle\Resources\config\datafixtures.yml
parameters:
datafixtures.cities:
I'm trying to set up AWS S3/Cloudfront to work with liipimaginebundle in Symfony, but I really have no idea what I'm doing.
So far I have tried the following documented here http://symfony.com/doc/current/bundles/LiipImagineBundle/cache-resolver/aws_s3.html:
Installed aws-sdk-php:
"require": {
"aws/aws-sdk-php": "^3.28",
}
Set up my parameters (with the correct values not this dummy data):
amazon.s3.key: "your-aws-key"
amazon.s3.secret: "your-aws-secret"
amazon.s3.bucket: "your-bucket.example.com"
amazon.s3.region: "your-bucket-region"
Set up a resolver (although I'm not sure what that even means).
"%amazon.s3.cache_bucket%" is in the documentation but the parameter doesn't exist so I used "%amazon.s3.bucket%" instead:
liip_imagine:
cache: profile_photos
resolvers:
profile_photos:
aws_s3:
client_config:
credentials:
key: "%amazon.s3.key%"
secret: "%amazon.s3.secret%"
region: "%amazon.s3.region%"
bucket: "%amazon.s3.bucket%"
get_options:
Scheme: https
put_options:
CacheControl: "max-age=86400"
Added these lines to create the services:
services:
acme.amazon_s3:
class: Aws\S3\S3Client
factory: Aws\S3\S3Client
arguments:
-
credentials: { key: "%amazon.s3.key%", secret: "%amazon.s3.secret%" }
region: "%amazon.s3.region%"
acme.imagine.cache.resolver.amazon_s3:
class: Liip\ImagineBundle\Imagine\Cache\Resolver\AwsS3Resolver
arguments:
- "#acme.amazon_s3"
- "%amazon.s3.bucket%"
tags:
- { name: "liip_imagine.cache.resolver", resolver: "amazon_s3" }
I'm currently getting this error when I run php bin/console server:run:
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\UndefinedFunctionException: Attempted to call function "S3Client" from namespace "Aws\S3". in /var/www/swing-polls/var/cache/dev/appDevDebugProjectContainer.php:360
I've tried half a dozen other configs/tutorials to no avail. If someone can point me in the right direction I'd be incredibly grateful.
Using the code provided at Simple S3 Symfony Service with a few tweaks, I've been able to get my images to upload to my s3 bucket, but I just don't know how to get liipimaginebundle work with them.
In vendor/liip/imagine-bundle/DependencyInjection/Compiler/ResolversCompilerPass.php you can see the CompilerPass is getting the value from "resolver" attribute of the tag and is using it to create a Reference object. This means the resolver should contain the id of a service.
Try replacing
tags:
- { name: "liip_imagine.cache.resolver", resolver: "amazon_s3" }
with
tags:
- { name: "liip_imagine.cache.resolver", resolver: "acme.amazon_s3" }
I have generated a Grid with CRUD actions for my Labellisation entity on Sylius.
The grid is displayed well, but I would like to get associated elements too (the defaultAdresse of the -> customer of the -> current labellisation), so I need to use a custom repository method.
I tried to do this with this conf :
labellisation_grid:
resource: |
alias: grid.label
criteria:
valide: true
except: ['create', 'delete', 'update']
grid: public_labels
templates: LabelBundle:public/Crud
type: sylius.resource
defaults:
_sylius:
repository:
method: findAllValides
(adding all the defaults block), but I have an error because the method findAllValides is not defined. I do have a findAllValides method in my LabellisationRepository.
Debugging the ResourcesResolver, I saw in the getResource that the $repository passed to this function has a customRepositoryClassName = LabelBundle\Repository\LabellisationRepository (this path is the good one to my LabellisationRepository).
Is there something wrong with my code ?
For example, in my services.yml file next code:
parameters:
social:
facebook:
app_id: 123456789
secret: dkl41a5dw1daw11d1wa135451awwlflaw
google:
id: 12548411654
services:
bw.user.social:
class: BW\UserBundle\Service\SocialService
arguments: [%social.facebook%]
This method does not work. I can use this:
services:
bw.user.social:
class: BW\UserBundle\Service\SocialService
arguments: [%social%]
But it's not exactly what I need. How can I pass only facebook value without google and , it's very important, not change parameters structure?
You have to write your parameters like this :
parameters:
social.facebook:
app_id: 123456789
secret: dkl41a5dw1daw11d1wa135451awwlflaw
social.google:
id: 12548411654
Then you can use :
services:
bw.user.social:
class: BW\UserBundle\Service\SocialService
arguments: [%social.facebook%]