This seems like it should be easy, but I am unable to find the answer. How can one reuse a bundle multiple times within the same symfony project? For example if I have an article bundle that I want to use multiple times on the same website.
I see in the app routing.yml you can add a prefix to the routed URL's, however if I try this multiple times with a different prefix each time only the last one works. Assumedly because the unique route names within the bundle are not prefixed, just the routs.
Foo:
resource: "#Foo/Resources/config/routing.yml"
prefix: /bar/
Bah:
resource: "#Foo/Resources/config/routing.yml"
prefix: /bah/
So where do I go from here? Is there some way to auto prefix unique route names, database tables etc (while still being able to reference / link to everything from within templates). Or is this a situation that symfony has just not been designed to accommodate?
I believe that there are two options:
Create the object in the your bundle:
Inside of your foo controller, do something like this:
use Acme\BahBundle\Class;
You should then be able to call it
$class = new Class();
$class->function('params');
The other option is to register the bundle as a service, check out the doc for more info:
http://symfony.com/doc/2.0/book/service_container.html
Related
I'm seeing some places where there is an "options" property on routes (below is taken from https://github.com/FriendsOfSymfony/FOSJsRoutingBundle/blob/master/Resources/doc/index.md#generating-uris):
my_route_to_expose:
pattern: /foo/{id}/bar
defaults: { _controller: HelloBundle:Hello:index }
options:
expose: true
Note the "options" key under "my_route_to_expose".
While this obviously exists, I cannot see any place in the Symfony documentation that mentions this. I've also tried poking around the code in the project that the above example came from, but cannot seem to find where they are picking up on this at all.
From what I can assume (since I see no documentation for it) is that it can be used to store just arbitrary data with a route that you as the developer can pick up on and use, however, I don't know when and where you would be using this information at.
So, what can this be used for and in what context can it be accessed?
That key is used by FOSJsRoutingBundle and is not a part of Symfony2 core.
By default FOSJsRoutingBundle doesn't expose all your route (I think for performance & security reasons) but only those that have that key set to true.
I have two routes in two different bundles with the same pattern. As the documentation says, only the first route is used.
route 1 is in a bundle named KitStyleBundle
route 2 is in another bundle (I dont know the name, it could be added by the developpers and represent the application they have to develop)
Question : Normally route 1 is used, but if a route 2 is defined in another bundle, I would like this route to be used. Is it possible to change the order of loading with routes. Or is there any other way to achieve what I want to ?
Routes are not automatically loaded. You can simply change the order of the include statements in your routing config file (app/config/routing.yml)
I got a problem to add custom logic to some Symfony classes.
SwitchUserListener
I want to add a check, that a user cannot switch to a another user, which have more rights/roles, than the initial user.
First attempt
Overwrite the parameter in the security_listeners.xml with the key:
security.authentication.switchuser_listener.class But where can I overwrite it?
In the security.yml it didn't work:
security:
...
authentication:
switchuser_listener:
class: Symfony\Component\Security\Http\Firewall\SwitchUserListener
Second attempt
Overwrite the service for the SwitchUserListner service id: security.authentication.switchuser_listener
I create the same service in my service.xml of my bundle, but my class was not used / called.
Another idea was to overwrite only the class, but that only works for bundles, but the SwitchUserListener was not in the SecurityBundle, it was in the symfony component directory and that seemed to me as a really bad idea to overwrite the SecurityBundle
Third attempt
Now I get the solution: First time I didn't realize that the dispatcher call listener for the SWTICH_USER event in the SwitchUserListener:
$switchEvent = new SwitchUserEvent($request, $token->getUser());
$this->dispatcher->dispatch(SecurityEvents::SWITCH_USER, $switchEvent);
So I need only to create a service with the special tag for this event type:
<tag name="kernel.event_listener" event="security.switch_user" method="onSecuritySwitchUser" />
And do the check in the given method.
This seems to be a better solution thatn the other two. But there is still a problem. In my listener for the SwitchUserEvent I need to ignore my custom check if the user wants to exit the switched user.
So I need to check the requested path: ignore if path containts '?switch_user=_exit'
But the path (URL parameter) can be changed:
# app/config/security.yml
security:
firewalls:
main:
# ...
switch_user: { role: ROLE_ADMIN, parameter: _want_to_be_this_user }
But in my bundle I can't read this parameter, because it will not be passed to the service container. It will be passed to the constructor of the SwitchUserListner class and will be saved there as private attribute, never accessable (without Reflection) from outside. (that happens here: SecurityExtension.php line 591) So what to do? Define the parameter twice go against DRY. Use Reflection?
And the other point is that there aren' every time events that will be fired on which I write a subscriber class. So what would be another / best solution for it?
I ask this question because I will get some similar problem where I want to add or overwrite something of the symfony intern components.
TemplateGuesser
I wanted to modify the TemplateGuesser: For a specific bundle all Templates which has the annotation #Tempalte the tempate file should be located with the controller TestController#showAction at this path:
Resources/views/customDir/Test/show.html.twig
So the guesser should be put and locate everything into a additional folder customDir instead of using only views. When using the render function with a specific template, the guesser should ignore the annotation.
I created my own Guesser and overwrite the service id: sensio_framework_extra.view.guesser and in comparision to the SwitchUserListener this time my class is really called instead of the original guesser. Why it works here but not with the SwitchUserListener?
Is this a good solution at all? I also tried to add a second listener, which calls the TemplateGuesser, its the service sensio_framework_extra.view.listener with the class Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener But that didn't work.
Whenever you need to add custom logic or extend the framework behaviour, you can use and abuse the container configuration. That means you can overwrite pretty much every service Symfony defines by just creating a new class that extends that service – or not, really – and creating the service definition for it with the same key as the original service you wanted to extend or change behaviour.
For instance, Symfony has a base template guesser registered as a service with the sensio_framework_extra.view.guesser id. If you want to extend that or change behaviour, you only need to create your own class and register it with the same id of the original service – remember that the bundles loading order affects the service definitons with the same id, where the last one loaded is the one that will be created.
That should solve both of your problems.
I've started a Symfony2 project from scratch where I then installed FOSUserBundle.
Then, I have written (actually, generated with ORM Designer) some entities that need to have relations between them, and with the User entity.
I have Items belonging to Users, Collections belonging to Users that group Items, and so on.
Since I used FOSUserBundle I only have a basic User class (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md , step 3a) defined using annotations, no config/doctrine folder and no User.yml file in it.
I then created the MyBundle/Resources/config/doctrine folder and added the yml files mentioned above.
When I try to generate the entities with the command-line tool everything works fine: it will create the Entities from my yml files.
However, at this point, trying to load up in browsers the url where the login previously worked (when I only had the FOSUserBundle installed) will throw this error:
MappingException: No mapping file found named
'/var/www/concert/src/X/MyBundle/Resources/config/doctrine/User.orm.yml'
for class 'X\MyBundle\Entity\User'.
Following actions, such as generating the CRUD logic, will not work as long as I have an *.orm.yml file in the config/doctrine folder. If I remove those, CRUD generation will work, but generation of actual mysql tables won't.
Juggling with these gets me to a point where I can also get the tables, but then the actual app doesn't work if I try to use any of the url's where the newly generated CRUD is involved because since the entities are based on yml (which I remove to get things "working") it won't have any mapping knowledge.
Is this inherently wrong? To have yml-based entities in relationship with an User entity based on the FOSUserBundle and still be able to get the nice command-line generation tools?
The problem you describe stems from mixing configuration formats (yaml and I assume annotations). You can easily fix this by ditching the annotations in your models and replacing them with yaml-files like you would do in your own models.
Unfortunately the FOSUserBundle-docs only show you how to use annotations, so here is a quick transformation into yaml format when your X\MyBundle\Entity\User extends FOSUSerBundle's UserEntity:
X\MyBundle\Entity\User:
type: entity
table: fos_user
id:
id:
type: integer
strategy: { generator: "AUTO" }
The remaining stuff is taken care of by FOSUserBundle, as the BaseModel is a mapped-superclass and already describes the stuff in the User.orm.xml, but you could just as well replace the existing values or add additional values just like you would do with your own models.
If you don't use annotations throughout your app, you might also want to disable them in your app/config/config.yml to prevent side effects.
Is there any difference in using annotation routes or group routes in symfony 2 when it comes to performance, convenience, maintainability or in any aspect that would make the other more advisable to use?
Use SensioFrameworkExtraBundle, already shipped with Symfony 2. Having route names and paths in the same place of controller action is the right way to go. If you want to modify a path or action name simply go to that action, without editing routing.yml.
Then give a name to your "grouped routes" e.g. bundle_controller pattern or maybe bundle_entity (if controller is used for CRUD on a single entity):
# app/config/routing.yml
acme_hello_my_annotated_controller:
resource: "#AcmeHelloBundle/Controller/MyAnnotatedController.php"
type: annotation
Eventually prefixing all paths defined by annotations (add prefix: /my/path/prefix).
EDIT: don't know anything about performances. I'm talking only about convenience.