没有前缀的登录路径 FOS Userbundle

login path FOS Userbundle without prefix

我希望登录页面位于 my.vendor.org/ 而不是 my.vendor.org/login.

我的security.yml看起来像这样

form_login:
    provider: fos_userbundle
    csrf_provider: security.csrf.token_manager
    check_path: /login_check
    login_path: /login
    default_target_path:  /home
    use_forward:    false
    failure_path:   null

如果我将 login_path 更改为:

 login_path: /

我会得到一个错误:

找不到 "GET /"

的路线

我该如何解决这个问题?

我会把它作为评论包括在内,但缺乏声誉

这是一种非常规的用户登录方式。请区分您希望在主页和任何其他页面上显示登录表单的位置,或者您是否希望主页实际成为登录页面。

我更喜欢第一种方法,但你的问题被表述为第二种方法。

回答你的问题。确保应用的 home 路由的路由文件没有被方法限制 [POST]。该路由现在由 FOSUserBundle 管理,因此如果有任何问题,您将不得不检查并可能在那里破解它,因此我建议您重新考虑您的方法。

第一种方法很简单,只需创建一个带有 post 参数的 html 表单到您的 fos 登录检查 url 就像这样

.....

<form action="{{ path('fos_user_security_check') }}" method="post" class="pure-form" style="display:inline-block; margin: 5px;">
    <input type="text" name="_username" required="required" placeholder="Username or Email" style="width: 150px;"/>
    <input type="password" name="_password" required="required"  placeholder="Password" style="width: 150px;"/>
    <input type="checkbox" id="remember_me" name="_remember_me" value="on" style="display:none;" checked/>
    <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}" />
    <button type="submit" class="pure-button pure-button-primary">Log In</button>
</form>

.....

确保在你的 config.yml 中添加 csrf_token 作为全局 twig 变量,就像这样

.....

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    globals:
        fos_csrf_provider: "@security.csrf.token_manager"

.....

我通过覆盖 security.xml 文件解决了这个问题。请注意,ID 为 fos_user_security_login 的路由具有路径“/”而不是“/login”

<!-- app/Resources/FOSUserBundle/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" path="/" methods="GET POST">
            <default key="_controller">FOSUserBundle:Security:login</default>
        </route>

        <route id="fos_user_security_check" path="/login_check" methods="POST">
            <default key="_controller">FOSUserBundle:Security:check</default>
        </route>

        <route id="fos_user_security_logout" path="/logout" methods="GET">
            <default key="_controller">FOSUserBundle:Security:logout</default>
        </route>

    </routes>