【问题标题】:Redirect after form success symfony 4表单成功后重定向 symfony 4
【发布时间】:2018-05-24 07:56:06
【问题描述】:

我遵循了这个教程:https://symfony.com/doc/current/security/form_login_setup.html

为了构建我的表单并呈现正常,我在我的表中创建了一个用户并使用其用户名和密码登录但被重定向到空白页面,但我已将其设置为转到 /dashboard

我的 security.yaml

encoders:
    App\Entity\User: bcrypt
providers:
    in_memory: { memory: ~ }
    user_provider:
        entity:
            class: App\Entity\User
            property: username
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    main:
        anonymous: ~
        http_basic: ~
        provider:   user_provider
        form_login:
            login_path: login
            check_path: login
            default_target_path: /dashboard

我什至尝试使用隐藏的_target 输入:

<input type="hidden" name="_target_path" value="{{ path('dashboard') }}" />

登录控制器.php

<?php
    namespace App\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

    class LoginController extends Controller
    {
        public function login(Request $request, AuthenticationUtils $authenticationUtils)
        {
            $error    = $authenticationUtils->getLastAuthenticationError();
            $lastUser = $authenticationUtils->getLastUsername();

            return $this->render('login/form.html.twig', array('last_username' => $lastUser, 'error' => $error));
        }
    }

完整代码库:https://github.com/BPSBiro/symfony-forms-errors

但它只是重定向到/login 作为一个空白页。我知道这不会出错(凭据明智),因为如果我在两个字段中都输入乱码,那么它会返回到 /login 并显示表单和错误消息(无效凭据)。

表单成功后如何重定向?

【问题讨论】:

  • 你要输入路由名称吗? default_target_path: 'dashboard' 和 always_use_default_target_path: true
  • check_path 在防火墙后面?就我而言,login_path 和 check_path 的路线不同。 login_path 可以匿名访问,而不是 check_path (并且 check_path 路由到达的方法只包含一个 throw new \Exception('This should never be reach!');)
  • 我用这个symfony.com/doc/current/security/guard_authentication.html的登录表单,你可以看看
  • 您确定登录成功了吗?个人资料栏显示经过身份验证的用户?在我看来登录失败。
  • 配置文件栏呢?不要急于求成,但有时似乎登录成功,但实际上并非如此。空白页似乎也很可疑。您正在调试模式下运行吗?

标签: php symfony redirect symfony4


【解决方案1】:

所以我关闭了你的 github 并没有改变它运行它。创建了一个用户,然后尝试登录。得到指向 App\Entity\User::serialize 的异常。您在这里遇到了一个小问题:

public function serialize()
{
    # causes infinite nesting
    # return $this->serialize(array($this->id, $this->username, $this->password));

    # the fix
    return serialize(array($this->id, $this->username, $this->password));
}

谜团在于为什么你得到一个空白页而不是异常。我使用内置服务器进行测试:

bin/console server:start

我猜你的 Apache 配置实际上是在生产模式下运行,这会抑制你的错误。

【讨论】:

  • wabam 成功了 :) 感谢您抽出宝贵时间查看它,我无法到达那里。是时候安装 xdebug 我想哈哈再次感谢:)
  • 这与xdebug无关。当您尝试登录时,您应该得到一个很大的旧异常屏幕。您需要获取该屏幕,否则任何其他小错误只会给您一个无用的空白页面。
  • 我的意思是一般的调试,可能就像你说的,apache正在抑制错误:)
【解决方案2】:

如果您使用传统表单登录,为什么要保留http_basic: ~?请删除此内容并重试

【讨论】:

    猜你喜欢
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多