简体   繁体   中英

Override FOSUserBundle routes Symfony2

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM