08.
Tagging Services (and having Fun!)
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
25 Comments
I'm getting a really strange error:
Cannot autowire service "AppBundle\Service\MarkdownTransformer": argument "$cache" of method "__construct()" references interface "Doctrine\Common\Cache\Cache" but no such service exists. You should maybe alias this interface to one of these existing services: "annotations.filesystem_cache", "annotations.cache", "doctrine_cache.providers.doctrine.orm.default_metadata_cache", "doctrine_cache.providers.doctrine.orm.default_result_cache", "doctrine_cache.providers.doctrine.orm.default_query_cache", "doctrine_cache.providers.my_markdown_cache".
I've tried adding this 'Knp\Bundle\MarkdownBundle\MarkdownParserInterface: '@markdown.parser'' into services.yaml but it doesn't work.
Please help.
Hey Tom
You have to define that argument of the MarkdownTransformer service in your "services.yml" file, the service name you should use is
'@doctrine_cache.providers.my_markdown_cache'Cheers!
I have same error but Diego's answer doesn't solve my problem...
services.yml : https://pastebin.com/WKttLXd3
How can I solve this problem ? Thank you
I modified my services.yml by removing everything that doesn't appear in the tutorial. It works....
But why does it work?
Hey Alexandre P.
Wow it's cool that you solved issue! It works because you disabled autowiring feature. It's enabled by default in your symfony version, and you will learn about it in next chapter!
Cheers!
Hi all,
I dont understand why I have always difficult/different scenarios.
I have a SoftwareSetting entity/table.I store version number in database and other values.
Now in my twig base template in a footer i want to just show version number. I could hardcode but then there is no point to have it in DB,or pass it to every template but what I am looking for is a global variable.
This SoftwareSetting entity/table has also other values like isModuleAvailable.
Now if isModuleAvailable is true then I show a module name in navbar otherwise it is disabled.
Again I could pass this value to every template but I have 50 routes where I would have to pass this. Save would be for version.
Admin has a functionality to change any of these settings.
I was reading about this: https://twig.symfony.com/do...
Then other user said stay away from global variables.
Thing is that I need these 2 variables on every route and I am too lazy to attach/pass it from controller to twig template.
ok I dont know if this is a good practice but I am using globals here is how I am doing it so check it out. Might help you.
// twig extension
// register service in services.yml
// software entity
and then in my base.html.twig I can call just {{ version }} and this variable is accessible in all your templates and you do not have to pass it to twig from controller.
Hope that helps someone
Hey Peter K.!
Yea, I know this exact situation, and you're 100% correct to think that "passing the variables into Twig from a controller" is not the right path - that would be totally crazy :). Globals are a fine way to handle this - your templating section is one of the only places where using globals just makes sense. However, I solve this problem (which I have quite commonly) in a slightly different way: by adding custom functions or filters to my Twig extension. It's basically the same thing - just a matter of taste. So, I would have something like
`{{ get_version() }}
`in my templates. I like the functions because they're more flexible (arguments when necessary) and it's more clear where they come from (when I see a global variable, I think "is a controller passing that?").
Cheers!
Thanks for the feedback you are right I will change it to functions.
Off topic how do you format code here in comments? I have pasted it directly from phpStorm to keep format and you can see how ugly it looks above.
Hey Peter K.
You have to wrap your code between <'pre'><'code'><'/code'><'/pre'> (Remove single quotes)
Have a nice day :)
Hi all . I have a problem when adding twig extension . The project give me some error as below :
(3/3) FileLoaderLoadException
The file "/var/www/html/aqua_note/app/config/services.yml" does not contain valid YAML in /var/www/html/aqua_note/app/config/services.yml (which is being imported from "/var/www/html/aqua_note/app/config/config.yml").
in FileLoader.php (line 179)
at FileLoader->doImport('/var/www/html/aqua_note/app/config/services.yml', null, false, '/var/www/html/aqua_note/app/config/config.yml')
in FileLoader.php (line 101)
at FileLoader->import('services.yml', null, false, '/var/www/html/aqua_note/app/config/config.yml')
in YamlFileLoader.php (line 184)
Please does anyone help me
Hey Eni Shima
Look's like your services.yml file has a syntax error, could you show it here? so I can help you debugging
Cheers!
Hi MolloKhan .Yes here is my services.yml . I added public: true in app.markdown_extension: but the problem still exist
# Learn more about services, parameters and containers at
# https://symfony.com/doc/cur...
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
# 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'
app.markdown_transformer:
class: AppBundle\Service\MarkdownTransformer
arguments: ['@markdown.parser', '@doctrine_cache.providers.my_markdown_cache']
public: true
app.markdown_extension:
class: AppBundle\Twig\MarkdownExtension
tags:
- { name: twig.extension }
arguments: ['@app.markdown_transformer']
public: true
Could you upload it to https://pastebin.com/ or any other platform, please? It's hard to see the problem and it lost the identation.
As a first guess, I think removing the first line "parameters:" might fix it
Cheers!
I have removed the "parameter" but it haven change . Here is the file services.yaml :https://pastebin.com/QW05QRF5 .
This is strange, your services.yml file look's good (or I just can't detect the error), try to not importing it and see if the error goes away, just to be 100% sure this file is the problem, if that's not the case, check that all classes exists, and their namespaces are correct.
I have done all that the tutorial have said but when we try to add the twig extension , it gives error . The error is as below : PHP Fatal error: Uncaught Symfony\Component\DependencyInjection\Exception\AutowiringFailedException: Cannot autowire service "AppBundle\Service\MarkdownTransformer": argument "$markdownParser" of method "__construct()" references interface "Knp\Bundle\MarkdownBundle\MarkdownParserInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "markdown.parser.min", "markdown.parser.light", "markdown.parser.medium", "markdown.parser.max", "markdown.parser.flavored". in /var/www/html/aqua_note/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php:297
Stack trace:
#0 /var/www/html/aqua_note/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php(223): Symfony\Component\DependencyInjection\Compiler\AutowirePass->autowireMethod(Object(ReflectionMethod), Array)
#1 /var/www/html/aqua_note/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php(146): Symfony\Component\DependencyInj in /var/www/html/aqua_note/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php on line 297
.
I think the problem is on the MarkdownExtension.php class , but i haven't guess yet what it is . anyway this is are my 2 classes :https://pastebin.com/7vRnaKnp and https://pastebin.com/pWAhHGnY . Please can you help me
I believe you are running on Symfony 3.3, right? The dependency injection service has changed a bit in that version.
What you need to do is create an alias for MarkdownParserInterface, like this:
And now, Symfony will be able to autowire any service that depends on MarkdownParserInterface
if you want to learn more about how all this works, you want watch our fabulous tutorial of Symfony 3.3 https://knpuniversity.com/screencast/symfony-3.3
Cheers!
uh oh... disaster struck and I need help figuring out what's gone wrong please.
So, I'm working through the tutorial and then all of a sudden I get "SQL STAT[HY000] [2002] No connection could be made beacuase the target machine actively refused it.
The parameters.yml file looks like this:
I've not touched the Genus.php file (there are no errors that I can see)
I checked in the console:
λ mysql -u root aqua_note
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
I checked in the C:\wamp64\bin\mysql\mysql5.7.19\data\aqua_note folder and sure enough there is a nice collection of files. So the data is there.
Now, I'm stuck.... I must have missed something obvious along the way but I cannot figure out what.
Advice please?
<noob>
In future, start the database (navigate to the myql folder and run mysqld
</noob>
Hey Rich,
I'm glad you got it working quicker than I replied to you, nice work! I see you're on Windows and using Wamp server. Looks like you have to start MySQL server manually, because on Linux servers it's started in the background after installation. And thank you for sharing it with our other Windows users!
Cheers!
I have SF 3.3.6 and it even works without registering it inside services.yml. Is autoloading twig extensions a new feature?
If I try to register it via services.yml it works as well. So isn't it necessary anymore to include the twig extension in services.yml?
Hey Mike,
Yes, it's a new feature. Actually, Twig 2.x allow to autoregister Twig extensions, so probably you're on the new Twig 2.x :) If so - you don't need to register it manually now. Btw, we have written a blog post how to avoid such kind of problems when you're on Symfony 3.3: http://knpuniversity.com/bl... - I think it could be interesting for you.
Cheers!
Thanks Victor for your blazing fast answer! You're a tutorial god!
Hahaha, thanks :)
"Houston: no signs of life"
Start the conversation!