Get config data in Symfony2 - symfony

I am aware that this:
$this->container->getParameter('param_name')
...will get the parameter name from the main config file in /app/config/.
But how do I get config data from the config file in the bundle's config file and in a method inside an event listener class. The code runs on every request (the config file is in the bundle which holds the event listener class).
I am writing a bundle which based on the user agent gives permissions to different devices. So I will detect the device into a group, e.g. "IPHONE", "IPAD", "ANDROID_4+", "ANDROID_<4" etc. and then my config file would look something like this:
parameters:
allow_feature_1: IPHONE, IPAD, ANDROID_4+
allow_feature_2: IPHONE, IPAD, ANDROID_<4, ANDROID_4+
I just need to get those config values into my class.
I have all the event listener code done.

Firstly you should store your parameters in separate file parameters.yml and import this file in config.yml. For getting access for parameters from parameters.yml in an event listener you should inject this parameters http://symfony.com/doc/2.1/book/service_container.html#service-parameters (for sf2.1).

Related

Access monolog configuration values

I need to get the list of available channels from monolog configuration file , whene i try to inject these parameters into my custom service like this
logger:
class: App\Services\Logger
arguments:
$channels: '%monolog.channels%'
i got
You have requested a non-existent parameter "monolog.channels".
is odd that symfony don't provide a simple way to access all configurations (even for other bundles like monolog )
is their a way to do this or am i missing something ?

How to generate the SERVER_NAME in a console with symfony?

I want to write a script that send notification mails to users and execute it every hour and I want it to match the server name that host the symfony site. I've already tried with $_SERVER but it's not defined and the solution on this post but there is no request with the console so it's does work.
Since there is no traditional 'web request/response' thing in console command, all URLs you generate with {{ path('...') }} in twig or $this->generateUrl('...') or similar controllers/services´, will be probablyhttp://localhost/...
To fix that, simply add: (if you're on symfony 5.1+)
# config/packages/routing.yaml
framework:
router:
# but you should move it to your .env file and user two differen env-variables for host and scheme
default_uri: 'https://you-site-hostname.com/'
or if you are using older version, put
router.request_context.scheme and router.request_context.host to you parameters. Like
# config/services.yaml
parameters:
router.request_context.scheme: 'https'
asset.request_context.host: 'you-site-hostname.com'
Take a look here: https://symfony.com/doc/current/routing.html#generating-urls-in-commands
And here (my personal advice) https://symfonycasts.com/screencast/mailer/route-context

keycloak starts with a new realm and some client configurations

I try to use keycloak as the authentication service in my design. In my case, when the keycloak starts, I need one more realm besides default master realm. Assuming the new agency is called "demo".
So it means when keycloak starts, it should have two realms (master and demo).
In addtion, in the realm demo, I need to configure the default client "admin-cli" to enable "Full Scope Allowed". Also need to add some buildin mapper to this client.
In this case, I wonder whether I can use something like initialization file which keycloak can load when starting ?
Or I need to use keycloak client APIs to do this operations (e.g., Java keycloak admin client)?
Thanks in advance.
You can try the following:
Create the Realm;
Set all the options that you want;
Go to Manage > Export;
Switch Export groups and roles to ON;
Switch Export clients to ON;
Export.
That will export a .json file with the configurations.
Then you can tested it be deleting your Demo Realm and:
Go to Add Realm;
Chose the .json file that was exported;
Click Create.
Check if the configurations that you have changed are still presented on the Demo Realm, if there are then it means that you can use this file to import the Realm from. Otherwise, for the options that were not persistent you will have to create them via the Admin Rest API.

How to pass Hasura action handler url from environment variables?

I am creating some Hasura actions to handle custom logic. My backend is running on http://localhost:3002. I want the handler's URL's first portion to get from an environment variable.
I also added ACTION_RESOLVER_API_ENDPOINT: "http://localhost:3000" to the environment section of the docker-compose.yml file
I want the URL to be like this.
${ACTION_RESOLVER_API_ENDPOINT}/<functionName> how to access that variable and use it in the handler section of the Hasura console.
Add to the input the following value:
{{ACTION_BASE_ENDPOINT}}/functionName
and the ACTION_BASE_ENDPOINT will come from the ENV variable with the same name.

Using Symfony expression in 3rd party bundle configuration

I am trying to configure HWIOAuth bundle to use dynamic client id. I have a service that can figure out what variable value to use depending on some request conditions.
Here's an expression in my HWI configuration, but can't seem to get it work:
hwi_oauth:
firewall_names:
- main
resource_owners:
auth:
client_id: "#=service('host.configmgr').GetClientIdParameter()"
client_secret: '%client_secret%'
The issue us with #=service('host.configmgr').GetClientIdParameter(). Can that even execute evaluate? The service is properly executes an used by another class somewhere else with success.
The %client_secret% parameter replacement works fine.
For more context, this part of the configuration lives in a dedicated file that is included in config.yml via { resource: hwiconfig.yml }
After some research looks like expressions are only supported for service definitions. This functionality is not supported for configurations outside services like the HWI bundle config.

Resources