How to get a variable in symfony2 controller from parameters file app/config/parameters.yml.
parameters:
host_name: www.mydomain.con
You could do:
$this->container->getParameter('host_name');
Check this out
$this->container->getParameter('host_name');
And if you want in twig then try like below
Add in app/config/config.yml like:
twig:
globals:
host_name: '%host_name%'
and in twig file use as normal variable like:
{{host_name}}
Enkoy!
You will get like this
$this->container->getParameter('host_name');
Related
I'm using Symfony2 to access an API. I have a controller that initializes the Oauth and another that is for the Callback. Instead of manually typing out the api key, and other variables into these controllers, I want to have a single "configuration" file that I can include in all relevant locations (ie, the Oauth controller and OauthCallback controller).
How do I go about doing this? Should I add more lines to the config.yml or should I just create a new file called config.php and include it? With normal ol' php it'd be an easy require_once but since this is a framework, I want to make sure I'm doing it the "right" way.
Thanks!
Use parameters in parameters.yml or config.yml file.
parameters:
my_api_key: 1234
In controller get like that:
$apiKey = $this->container->getParameter('my_api_key');
IMO, you should write it in the config.yml. Remember that you can write other parameters and include it in the main config.yml with
imports:
- { resource: mi-file.extension}
In the controller, you can get this parameters with
$var= $this->container->getParameter('name_parameter');
I have a service called General, this is the configuration :
services:
app_bundle.general:
class: AppBundle\Services\General
I'm trying to set this service as a global variable for my twig templates, so in config.yml I added (like the documentation say) :
parameters:
general_service: "#services.app_bundle.general"
But with this I have this error : You cannot dump a container with parameters that contain references to other services (reference to service "services.app_bundle.general" found in "/general_service").
How can I set my service to a global variable for Twig ? And in twig, use it like : {{general_service.myMethod()}}
Thanks !
As Artamiel suggested, add your service to the twig engine globals:
#app/config/config.yml
twig:
globals:
general_service: "#app_bundle.general"
Please note: your service has the id app_bundle.general and not services.app_bundle.general as indicated in your example.
I have the following structure:
/src/Product/AdminBundle/Resources/views/main.twig
I am trying to render this template from controller:
$this->render(...);
or from routing.yml
index_page:
path: /
defaults:
_controller: FrameworkBundle:Template:template
template: ...
How can I render this from from inside the controller in the bundle? Here is what I have tried with no luck:
Product:AdminBundle:main.twig
ProductAdminBundle:main.twig
#ProductAdminBundle:main.twig
#ProductAdminBundle/main.twig
ProductAdminBundle/main.twig
ProductAdminBundle/Resources/views/main.twig
Product/AdminBundle/Resources/views/main.twig
AdminBundle:main.twig
AdminBundle/main.twig
The usual path syntax is:
BundleName:DirectoryInView:file.html.twig
In your case, this will be:
ProductAdminBundle::main.html.twig
We use ::, because your view is located in the root directory.
After trying every possible combination this is the one that worked for me:
#AdminBundle/Resources/views/main.twig
Try like this:
return $this->render('ProductAdminBundle:main.html.twig', []);
Where main.html.twig is your view name (and not main.twig).
AdminBundle::main.twig does not work
rename main.twig into main.html.twig
After that AdminBundle::main.html.twig will work!
{{ render(controller("SomeBundle:Foo:Bar", {HERE I WANT TO PASS ALL query parameters app.request.query.all}) }}
So can I access all master request query parameters in sub request and the subrequest should also run independently?
Try this:
{{ render(controller("SomeBundle:Foo:bar", {'all': app.request.query.all}) }}
and in action store it in $all variable
public function barAction($all) {
// other your code
}
From your controller:
array_merge($request->query->all(), $request->get('_route_params'));
//query->all : get all query string parameters
//_route_params : get current route parameters
From your twig template must be sth like:
app.request.query.all|merge(app.request.attributes.get('_route_params'))
I've never used this in twig templates, so first test it ;)
Then you can use that functions however you want to build the variables you'll pass to your subrequest
To just pass in what is in app.request.query.all:
{{ render(controller("SomeBundle:Foo:Bar", app.request.query.all)
To merge something extra in:
{{ render(controller("SomeBundle:Foo:Bar", { something: 'extra' }|merge(app.request.query.all))
Tested in Symfony 3.3.10 and Twig 1.35.0
If I created a service is there a way to access it from twig, without creating a twig.extension?
You can set the service a twig global variable in config.yml, e.g
#app/config/config.yml
twig:
globals:
your_service: "#your_service"
And in your template.html.twig file you can invoke your service this way:
{{ your_service.someMethod(twig_variable) }}
See here.
To get this done in Symfony 5, first you must declare the service in services.yaml, for example:
App\Service\NavigationHelper:
arguments:
foo: bar
Then you can declare the service for its use in Twig. To achieve this, you must add it as a variable in the "globals" section of the Yaml file located in packages/twig.yaml:
globals:
navHelper: '#App\Service\NavigationHelper'
Now you can use your service methods from the templates as Mun Mun Das suggested in his last code snippet.