symfony services.yml difference between/utility of name and resource - symfony

I'm new to symfony, I noticed that in services.yml we usually define each entry by a name (for example App\Controller) and then add the path to the file/folder in the resource attribute, for example :
App\Controller :
resources: ../../Controllers/*
isn't this some kind of repetitive ? what is the use of the name we give at the beginning ?
Thank you

Related

What config when controller is other folder and template path is inadequate

I work with Symfony 4.2
I divided my dir structure, and every part of app is in other folder.
Ex. admin part code is in src/Admin/...
- src/Admin/Controller
etc.
When I set #Template in controller method symfony tell me to situate templates in templates/
But I want to it placed in teplates/Admin/
What I should change in config.
If is other solution than set every action path in #Template(...) ?
Taken from the docs:
# config/packages/twig.yaml
twig:
# ...
paths: ["%kernel.project_dir%/resources/views"]
https://symfony.com/doc/current/configuration/override_dir_structure.html#override-the-templates-directory
This might also be interesting for you:
https://symfony.com/doc/current/templating/namespaced_paths.html#multiple-paths-per-namespace

Unable to find template Bundle:Default:index.html.twig

I'm having some trouble runnning Symfony. In fact, it can't find the default twig template. I didn't touch the code at all, juste generated my bundle and trying to access /web/app_dev.php.
My template is in
/src/OC/PlatformBundle/Resources/views/Default/index.html.twig.
Symfony looked into these locations, but not where my template actually is.
/app/Resources/views
/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form
And the DefaultController.php indexAction() looks ok :
public function indexAction(){
return $this->render("OCPlatform:Default:index.html.twig");
}
If any of you have ever faced this kind of issue or have any idea where the problem comes from, I thank you in advance for your help.
Arthur
I've same problem and i resolve it with this route :
public function indexAction(){
return $this->render("#OCPlatform/Default/index.html.twig");
}
Edit ":" with "/" and it work.
Maybe can help other developper
If you want to keep using the 'OCPlatform:Default:index.html.twig' format for templates then edit your app/config/config.yml file:
#app/config/config.yml
framework:
templating:
engines: ['twig']
This fixed my problem. You can check this link
Symfony 3.4 Use view inside my bundle
Following the docs(Symfony >=3.4) you should write the path to the template in the controller like this:
public function indexAction(){
return $this->render("#OCPlatform:Default:index.html.twig");
}
This worked for me:
return $this->render('#HomeBundle/Default/index.html.twig');
Hope it hope :)
Referencing Templates in a Bundle¶
If you need to refer to a template that lives in a bundle, Symfony uses the Twig namespaced syntax (#BundleName/directory/filename.html.twig). This allows for several types of templates, each which lives in a specific location:
#AcmeBlog/Blog/index.html.twig: <br>This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a slash (/), mean the following:<br>
#AcmeBlog: is the bundle name without the Bundle suffix. This template lives in the AcmeBlogBundle (e.g. src/Acme/BlogBundle);<br>
Blog: (directory) indicates that the template lives inside the Blog subdirectory of Resources/views/;<br>
index.html.twig: (filename) the actual name of the file is index.html.twig.
In symfony 4 use this :
return $this->render("#Platform/Default/index.html.twig");

Symfony2 : Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, none given

I'm trying to implement OAuth2 in my actual symfony2 project using the FOSOAuthServerBundle.
I've been following this Article to implement it.
Since i don't use FOS User bundle i had to create a UserProvider.
I'm also using A User Repository as he did in his Article.
I've been stuck with this error :
ContextErrorException: Catchable Fatal Error: Argument 2 passed to Doctrine\ORM\EntityRepository::__construct() must be an instance of Doctrine\ORM\Mapping\ClassMetadata, none given, called in /app/cache/dev/appDevDebugProjectContainer.php on line 3527 and defined in /app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php line 67
I've followed the given code for services.yml:
And converted it to Yaml, here my file (services.yml) :
imports:
- { resource: parameters.yml }
parameters:
# wca_transition.example.class: WCA\TransitionBundle\Example
collection.import.from_email: "%import_from_email%"
collection.import.to_email: "%import_to_email%"
platform.entity.user.class: WCA\ServerBundle\Entity\Profile
platform.user.provider.class: WCA\ServerBundle\Provider\ProfileProvider
services:
# wca_betosee_transition.example:
# class: %wca_transition.example.class%
# arguments: [#service_id, "plain_value", %parameter%]
collectionImportCommand:
class: WCA\ServerBundle\Command\CollectionImporterCommand
calls:
- [setContainer, ["#service_container"] ]
platform.user.manager:
class: Doctrine\ORM\EntityManager
factory-service: doctrine
factory-method: getManagerForClass
arguments: ["%platform.entity.user.class%"]
platform.user.repository:
class: WCA\ServerBundle\Model\ProfileRepository
factory-service: platform.user.manager
factory-method: getRepository
arguments: ["%platform.entity.user.class%"]
platform.user.provider:
class: "%platform.user.provider.class%"
# arguments: ["#platform.user.repository"]
platform.grant_type.api_key:
class: WCA\ServerBundle\OAuth\ApiKeyGrantExtension
arguments: ["#platform.user.repository"]
tags:
- { name: fos_oauth_server.grant_extension, uri: http://mywebsite.local/grants/api_key }
I suppose the given error is coming from my configuration but i don't see what could be wrong since i followed what was given. Either the services created don't get the given arguments ("none given") or i don't pass the correct ones.
Any ideas or hints?
PS : If you need any other files, don't hesitate to ask, i'll update the post.
I doubt if it will help but try changing factory-service,factory-method to factory_service and factory_method to match the Symfony documentation.
Sounds like it is the only article that describes very well how to implement FOS OAuth2 bundle, even better than their own documentation.
But for some reason, services are defined in xml which I find very weird and ugly.
Anyway, as #Cerad mentioned, if you translate the xml service to the yaml format, then you'll have to replace the - by _ and it will work fine.

How to use a global config value in a private config file with symfony2

I defined some values in the define.yml in the app/config folder.
And I can access then in the PHP code.
Now I want to use the validate.yml file to check weather users give out too many data, could I use the configed valus in the config file(such as %maxInputLength%) rather than write it one more time in the
validate.yml?
for a example:
I defind a variable in define.yml:
maxLength: 10
So we can use it in the controller.
But if I want to use it in the validate.yml in my own bundle,
such as : myBandle/config/validation.yml
Now I just use the file like that:
myBundle\Entity\UserDesc:
properties:
content:
- NotBlank: ~
- MaxLength: 10
So it seems that the same value was defined at two place.In fact the 10 in the second file is just the value defined in the first file . But I don't know how to get the value from the first file.Is there any way to make that?
AFAIK you want to set value to variable in one config file and then reuse it in other .yml config file.
So you cant define some vars in e.g variables.yml file
parameters:
test_variable: "blabla"
Then in second .yml config file you can import file with defined variables:
imports:
- { resource: variables.yml }
Then you can use imported variable in this file:
property:
some_value: %test_variable%

How to access arrays in symfony's configuration files?

Does the symfony2 can handle only flat parameters?
Say we have:
services:
manager:
class: blabla
arguments: [%app.vat%]
and in app.yml :
parameters:
app.vat: 24.5
it works, but
parameters:
app:
vat: 24.5
does not work. Is there some special syntax to access arrays or this is not possible?
This is indeed possible. You can access the values from your example in your code like this:
$config = $this->get('service_container')->getParameter('app.vat');
If this is still not working you should try to rename "app" into something else (e.g. "application"). Symfony preserves the name "app" on many places and handles it in a special way.

Resources