I am currently building my own Symfony bundle (I am using version 3.3). It works fine so far, but now I wanted to move the bundle-specific service definition out of my app/config/services.yml to a service definition within the bundle.
I created a src/MyBundle/Resources/config/services.yml and followed the guide How to Load Service Configuration inside a Bundle to load it. That works just fine, but I thought that the paths inside the newly created services.yml look a bit ugly:
MyBundle\:
resource: '../../*'
exclude: '../../{Tests}'
So I thought it would look a little cleaner, if I use the #Bundle-notation instead:
MyBundle\:
resource: '#MyBundle/*'
exclude: '#MyBundle/{Tests}'
However, than I was facing the error:
FileLocatorFileNotFoundException
The file "#MyBundle" does not exist (in: <...>\MyBundle\DependencyInjection/../Resources/config), where <...> corresponds to an absolute path.
I also tested whether it was a naming issue of the bundle name, but when I have a wrongly named bundle reference in the app/config/services.yml, e.g. #SomeBundleThatDoesNotExist then I get a different error:
FileLoaderLoadException
Bundle "SomeBundleThatDoesNotExist" does not exist or it is not enabled.
So my question is: Why does the #Bundle-notation work fine in the app/config/services.yml but not in the src/MyBundle/Resources/config/services.yml?
If you use the service remember make public for external use:
autowire: true
public: true
But, if your symfony dont know where is the bundle please chec this answer:
Symfony generated Bundle doesn't work
Related
I am trying to upgrade a Symfony 3 application to Symfony 4, but I am having trouble with Doctrine's namespace aliases. My entities are located under AppBundle\Entity (as was the default in older Symfony versions) and there was an alias for that, so my entities are referred to via AppBundle:SomeEntity all over my codebase. I understand that the new way to do this is to use the FQCN instead, but I would like to avoid having to do a massive find&replace operation right now.
I modified config/packages/doctrine.yaml with the following mapping:
mappings:
AppBundle:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/AppBundle/Entity'
prefix: 'AppBundle\Entity'
alias: AppBundle
However, I only get a ReflectionException saying Class AppBundle:SomeEntity does not exist. As far as I can tell from the stack trace, the alias is never resolved, just passed directly down to AbstractManagerRegistry as a class name, which then just passes it to the ReflectionClass constructor. The calling code, e.g., looks like this (inside a controller): $this->getDoctrine()->getRepository("AppBundle:SomeEntity")->...
I have now idea what the problem is. It looks like there is just no logic that would resolve aliases in this code path. It is probably a configuration issue. However, the configuration is mostly the default that comes with Symfony 4. I basically just replaced "App" with "AppBundle". Can anyone help me out here?
I have 2 bundle MyBundle1 and MyBundle2 , MyBundle2 inherits MyBundle1.
In /app/config/services.yml i have:
imports:
- resource: '#MyBundle1/Resources/config/services.yml'
- resource: '#MyBundle2/Resources/config/services.yml'
The problem is that '#MyBundle2/Resources/config/services.yml' will be considered as '#MyBundle1/Resources/config/services.yml' because it inherits it, so override its files.
My question is: How can I access to the real '#MyBundle2/Resources/config/services.yml', not the one rewritten ?
Thanks
The Symfony cookbook explains that Bundles override their parent Bundles resource files when
they're in the same location inside the bundle
They've been imported using the #MyBundle/...../services.yml (the #-part) syntax.
In order to have your Bundle not override your parent Bundles services you can use a different filename (eg. _services.yml) or refer to it using the full path in your config.
EDIT | I tried prefixing controllers class name with "Web" - no dice :(
According to everything I have read and previously experienced:
http://symfony.com/doc/current/best_practices/controllers.html
I should be able to move/organize controllers into sub-folders but now I cannot seem to get Symfony to cooperate:
AppBundle/Controller
- MemberController.php
- DefaultController.php
Works fine - but if I move
AppBundle/Controller
/web/MemberController
- DefaultController
Then clear the cache - I get the following error(s):
[Symfony\Component\Config\Exception\FileLoaderLoadException]
Class AppBundle\Controller\MemberController does not exist in /var/www/project/src/AppBundle/Controller (which is being imported from "/var/www/project/app/config/routing.yml").
[ReflectionException]
Class AppBundle\Controller\MemberController does not exist
Of course it doesn't I moved it up down one directory into a sub-folder "web". What gives? My routing.yml is as simple as:
site:
resource: "#AppBundle/Controller"
type: annotation
I am building a company Symfony2 library that includes several bundles and a library of classes that can be used by any code. The plan is to eventually turn this into an internal package and install it into projects via composer so that it will live in the vendor directory.
One component of the library has a pair of database entity classes and a single repository class associated with it.
Because the only logic associated with these classes is contained in the repository class, it doesn't seem to make sense to to surround it with a bundle.
I have, as yet, been unable to get Doctrine to load this repository. It initially failed with a
The class 'Dplh\Library\DplhEnum\Entity\EnumGroupRepository' was not found in the chain configured namespaces Dplh\DplhSecurityBundle\Entity
exception. (The reference to my security bundle was initially rather confusing. Turns out that this is Doctrine helping out by listing all of the known entity namespaces (see Symfony error The class XXX was not found in the chain configured namespaces XXX)).
As I understand it, this happens because Doctrine is configured to use auto-mapping and expects all entities to be defined in src/WhateverBundle/Entity directories.
This can be changed with additional Doctrine configuration for specific mappings in config.yml (http://zalas.eu/how-to-store-doctrine-entities-outside-of-a-symfony-bundle/).
So far, I have been unable to get this to work either. I expect it's a minor config thing somewhere. I added the following to config.yml:
doctrine:
dbal:
# ...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
DplhEnum:
type: annotation
is_bundle: false
dir: %kernel.root_dir%/../src/Dplh/Library/DplhEnum/Entity
prefix: Dplh\Library\DplhEnum\Entity
alias: DplhEnum
I get two different exceptions depending on my call to getRepository().
// Using the fully-qualified class name.
$this->getEntityManager()->getRepository('Dplh\Library\DplhEnum\Entity\EnumGroup' )
This throws
Attempted to load class "EnumGroupRepository" from namespace "Dplh\DplhEnumBundle\Entity".
Did you forget a "use" statement for another namespace?
// Using the alias.
$this->getEntityManager()->getRepository('DplhEnum\EnumGroup' )
this throws
Class 'DplhEnum\EnumGroup' does not exist
I have verified that the EnumGroupRepository.php file is in the src/Dplh/Library/DplhEnum/Entity directory and the EnumGroupRepository class is in the Dplh\Library\DplhEnum\Entity namespace and that they are in the correct directory.
I have the following configuration in my SF2 Bundle
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
services:
catalogue.title:
class: %catalogue.title.class%
Which works like a charm.
Now I want to enhance the configured service and tag it so it gets the knp_paginator injected (I've also tried with other tags). My config becomes:
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
services:
catalogue.title:
class: %catalogue.title.class%
tags:
- { name: knp_paginator.injectable, paginator: knp_paginator }
For the mentioned tag to work the service implements the Knp\Bundle\PaginatorBundle\Definition\PaginatorAware. As mentioned on the KnpPaginatorBundle docs.
Running my app will show the following:
ReflectionException: Class %catalogue.title.class% does not exist.
Since it works without adding the tags part, I know the class does exist.
Replacing %catalogue.title.class% with the value of the parameter (the fully qualified class name) solves the issue, but we are using this way of configuring our service classes in all our projects and really don't want to deviate from that just to resolve this issue.
[EDIT] Using a different parameter like catalogue_title_class or my_abc has the same result.
I'm running Symfony version 2.3.5.
What am I doing wrong?
Probably this exceptions throws because of same names of class of parameter and provided service name.
Try to rename
parameters:
catalogue.title.class: My\SomeBundle\Services\TitleService
to some other name
parameters:
catalogue_title.class: My\SomeBundle\Services\TitleService
and use this
services:
catalogue.title:
class: %catalogue_title.class%
tags:
- { name: knp_paginator.injectable, paginator: knp_paginator }