问题
I would like to override some routes from FOSUserBundle
MyBundle/Resources/config/routing/security.yml
fos_user_security_login:
path: /{_locale}/login
defaults: { _controller: FOSUserBundle:Security:login }
requirements:
_locale: %locales%
fos_user_security_check:
path: /login_check
defaults: { _controller: FOSUserBundle:Security:check }
requirements:
_locale: %locales%
fos_user_security_logout:
path: /{_locale}/logout
defaults: { _controller: FOSUserBundle:Security:logout }
requirements:
_locale: %locales%
But it does not works, route are not found
MyBundle/Resources/config/routing/security.xml
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="fos_user_security_login" pattern="/{_locale}/login">
<default key="_controller">FOSUserBundle:Security:login</default>
</route>
<route id="fos_user_security_check" pattern="/login_check">
<default key="_controller">FOSUserBundle:Security:check</default>
<requirement key="_method">POST</requirement>
</route>
<route id="fos_user_security_logout" pattern="/{_locale}/logout">
<default key="_controller">FOSUserBundle:Security:logout</default>
</route>
</routes>
This works but I don't know how to pass my locales parameter from parameter.yml
回答1:
First of all the the yaml routes are not working because the FOSUserBundle Routes are defined in xml. So your yaml routes won't imported.
here the FOSUserBundle Routes: https://github.com/FriendsOfSymfony/FOSUserBundle/tree/master/Resources/config/routing
If the FOSUserBundle is the parent bundle of your userbundle you are able to rewrite the FOSUserBundle routing resources. How to do this is explained here: http://symfony.com/doc/current/cookbook/bundles/inheritance.html#overriding-resources-templates-routing-etc
Further more to answer to the last point how to pass the locale into the route is described here: http://symfony.com/doc/current/cookbook/bundles/inheritance.html#overriding-resources-templates-routing-etc
<route id="contact" path="/{_locale}/contact">
<default key="_controller">AcmeDemoBundle:Contact:index</default>
<requirement key="_locale">%locales%</requirement>
</route>
来源:https://stackoverflow.com/questions/25444390/override-fosuserbundle-routes-symfony2