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.
Related
I'm using a service as twig global variable. In the service constructor I set a default value of the property $title. It works initially.... Twig render the property value using the command {{ service.getTitle() }} in a template file. But after update the service property by the controllers constructor and rendering the view, the value is not updated at screen. The goal is set a twig global variable by the controllers to render in all views. How to do it?
twig.yaml
twig:
globals:
pageMap: "#Base.PageMap"
services.yaml
services:
Base.PageMap:
class : App\Bundle\Base\Services\PageMap
public: true
controller
public function __construct(PageMap $pageMap)
{
$pageMap->setTitle('Registration listing');
}
twig template:
<div class="title">{{ pageMap.getTitle() }}</div>
Twig globals are setup at init time and compiled/cached for the duration of an execution.
If you want to update and be able to call things dynamically, you should create a RuntimeExtension (see documentation here : https://symfony.com/doc/current/templating/twig_extension.html#creating-lazy-loaded-twig-extensions)
Calling it from your template will be a little more expensive (but more correct !)
i have a mustache template that has the following line
{{{widgets.service_name.js.footer}}}
i found the corresponding service defined in a symfony2 bundle
service_name:
class: A\B\C\D\EventListener\AssetsListener
arguments:
- #templating
- %a.b.timestamp%
- %kernel.environment%
tags:
- { name: kernel.event_listener, event: kernel.response, method: injectAsset, priority: -255}
Is there a mustache specific meaning in .js.footer (prefix to the symfony2 service name) i am unable to find any reference related to this.
Anyone could provide some pointers i would greatly appreciate it.
Thanks
Dots in mustache tags are equivalent to either array access, properties or method calls. So this:
{{{widgets.service_name.js.footer}}}
Means something like this:
$widgets['service_name']['js']['footer'];
$widgets['service_name']->js->footer;
$widgets['service_name']->js()->footer();
… or some combination of the above. Which it actually translates to depends on what the service is, what public methods or properties it exposes, and what they return.
Here's more on Mustache dot notation, and on variable resolution in Mustache.php.
I want to create a custom route loader as instructed in http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html.
What I have to do is read the read the routes from an xml file (not in "symfony xml" format) and create the according route collection.However I want to do that using the '#' directive.as in:
xmlRoutes:
resource: '#FooBarBundle/Resources/routes.xml'
But in order to resolve the path to routes.xml I need the 'file_locator' service from the container.is it possible to access services in a custom router class.if not, how can I make a symfony\Component\Config\FileLocator to resolve that path?
Yes you could access the file_locator as it's a service. What you need to do is make your custom_route_loader a service itself (I dind't read the cookbook you linked but I'm pretty sure that they would advice to define it as a service) and inject the file_locator service into it.
So basically you'll do something like
#config.yml
[...]
services:
yourbundlename.custom_route_loader:
class: Path\To\Your\Bundle\CustomRouteLoader
arguments: [ #file_locator ]
And into you CustmRouteLoaderClass
#Path\To\Your\Bundle\CustomRouteLoader
class CustomRouteLoader
{
public function __construct(Symfony\Component\HttpKernel\Config\FileLocator $file_locator) {
$this->file_locator = $file_locator;
[...]
}
[...]
}
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.
Let's say I have a simple traditional contact form on my site and I would like it to use the subject "Test: (subject_field value)" in dev environment and "(subject_field_value)" in prod environment when sending e-mail. Is there a way to define a variable called "subject_prefix" in config_dev.yml and config_prod.yml and then just use something like $this->get('config')->get('subject_prefix')? I would expect that call to return "Test: (subject_field value)" in dev environment and "(subject_field_value)" in prod environment.
The best to do is :
In the config.yml
parameters:
url: domain.com
In the controller :
$value = $this->container->getParameter('url');
Hope it helps.
In Symfony 2.7+:
$value = $this->getParameter('url');
See the How to expose a Semantic Configuration for a Bundle cookbook article.
If you do not want to clutter your "parameters.yml" and do want to store it in the config.yml and config_dev.yml look at my answer here:
How do I read configuration settings from Symfony2 config.yml?
The one that contains the two approaches:
FIRST APPROACH: Separated config block, getting it as a parameter
SECOND APPROACH: Separated config block, injecting the config into a service
Hope this helps!