首页 文章

找不到“GET / login /”symfony3的路由

提问于
浏览
-1

我正在使用Symfony 3.3中的FOSUserBundle登录工作之前但是现在它没有工作说没有找到“GET / login /”的路由我输入“/ login”但它重定向到“/ login /”,我不知道知道发生了什么我试图解决它,但我失败了:(这是我的security.yml:

security:
        encoders:
            FOS\UserBundle\Model\UserInterface: bcrypt

        role_hierarchy:
            ROLE_ADMIN:       ROLE_USER
            ROLE_SUPER_ADMIN: ROLE_ADMIN

        providers:
            fos_userbundle:
                id: fos_user.user_provider.username

        firewalls:
            main:
                pattern: ^/
                form_login:
                    provider: fos_userbundle
                    login_path: /login
                    check_path: /login_check
                    csrf_token_generator: security.csrf.token_manager
                logout:
                        path:   /logout
                        target: /login
                anonymous:   true

        access_control:
            - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
            - { path: ^/admin/, role: ROLE_ADMIN }

这是app / routing.yml

stage_admin:
    resource: "@StageAdminBundle/Resources/config/routing.yml"
    prefix:   /

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

stage_controller:
    resource: "@StageAdminBundle/Controller/"
    type:     annotation
    prefix:   /

app:
    resource: '@AppBundle/Controller/'
    type: annotation

这是all.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">

    <import
        resource="@FOSUserBundle/Resources/config/routing/security.xml" />
    <import
        resource="@FOSUserBundle/Resources/config/routing/profile.xml"
        prefix="/profile" />
    <import
        resource="@FOSUserBundle/Resources/config/routing/registration.xml"
        prefix="/register" />
    <import
        resource="@FOSUserBundle/Resources/config/routing/resetting.xml"
        prefix="/resetting" />
    <import
        resource="@FOSUserBundle/Resources/config/routing/change_password.xml"
        prefix="/profile" />
</routes>

当我输入:php bin / console debug:router,我发现rout / login存在..

_wdt                                ANY        ANY      ANY    /_wdt/{token}
  _profiler_home                      ANY        ANY      ANY    /_profiler/
  _profiler_search                    ANY        ANY      ANY    /_profiler/search
  _profiler_search_bar                ANY        ANY      ANY    /_profiler/search_bar
  _profiler_info                      ANY        ANY      ANY    /_profiler/info/{about}
  _profiler_phpinfo                   ANY        ANY      ANY    /_profiler/phpinfo
  _profiler_search_results            ANY        ANY      ANY    /_profiler/{token}/search/results
  _profiler_open_file                 ANY        ANY      ANY    /_profiler/open
  _profiler                           ANY        ANY      ANY    /_profiler/{token}
  _profiler_router                    ANY        ANY      ANY    /_profiler/{token}/router
  _profiler_exception                 ANY        ANY      ANY    /_profiler/{token}/exception
  _profiler_exception_css             ANY        ANY      ANY    /_profiler/{token}/exception.css
  _twig_error_test                    ANY        ANY      ANY    /_error/{code}.{_format}
  stage_admin_homepage                ANY        ANY      ANY    /
  stage_admin_federation              ANY        ANY      ANY    /federation
  stage_admin_joueur                  ANY        ANY      ANY    /joueur
  fos_user_security_login             GET|POST   ANY      ANY    /login
  fos_user_security_check             POST       ANY      ANY    /login_check
  fos_user_security_logout            GET|POST   ANY      ANY    /logout
  fos_user_profile_show               GET        ANY      ANY    /profile/
  fos_user_profile_edit               GET|POST   ANY      ANY    /profile/edit
  fos_user_registration_register      GET|POST   ANY      ANY    /register/
  fos_user_registration_check_email   GET        ANY      ANY    /register/check-email
  fos_user_registration_confirm       GET        ANY      ANY    /register/confirm/{token}
  fos_user_registration_confirmed     GET        ANY      ANY    /register/confirmed
  fos_user_resetting_request          GET        ANY      ANY    /resetting/request
  fos_user_resetting_send_email       POST       ANY      ANY    /resetting/send-email
  fos_user_resetting_check_email      GET        ANY      ANY    /resetting/check-email
  fos_user_resetting_reset            GET|POST   ANY      ANY    /resetting/reset/{token}
  fos_user_change_password            GET|POST   ANY      ANY    /profile/change-password
  federation_index                    GET        ANY      ANY    /federation/
  federation_new                      GET|POST   ANY      ANY    /federation/new
  federation_show                     GET        ANY      ANY    /federation/{id}
  federation_edit                     GET|POST   ANY      ANY    /federation/{id}/edit
  federation_delete                   DELETE     ANY      ANY    /federation/{id}
  joueur_index                        GET        ANY      ANY    /joueur/
  joueur_new                          GET|POST   ANY      ANY    /joueur/new
  joueur_show                         GET        ANY      ANY    /joueur/{id}
  joueur_edit                         GET|POST   ANY      ANY    /joueur/{id}/edit
  joueur_delete                       DELETE     ANY      ANY    /joueur/{id}
  homepage                            ANY        ANY      ANY    /

请帮帮我:)谢谢

编辑:解决方案是:我在我自己的包中添加了一个新的SecurityContoller,通过复制FOSUserBundle的SecurityContoller的内容,我在其中添加了“login”路由

<?php

namespace Stage\AdminBundle\Controller;


use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Security;

/**
 * Federation controller.
 *
 * @Route("login")
 */

class SecurityContoller extends Controller
{

    /**
     * @Route("/")
     */
    public function loginAction(Request $request)
    {
        /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
        $session = $request->getSession();

        $authErrorKey = Security::AUTHENTICATION_ERROR;
        $lastUsernameKey = Security::LAST_USERNAME;

        // get the error if any (works with forward and redirect -- see below)
        if ($request->attributes->has($authErrorKey)) {
            $error = $request->attributes->get($authErrorKey);
        } elseif (null !== $session && $session->has($authErrorKey)) {
            $error = $session->get($authErrorKey);
            $session->remove($authErrorKey);
        } else {
            $error = null;
        }

        if (!$error instanceof AuthenticationException) {
            $error = null; // The value does not come from the security component.
        }

        // last username entered by the user
        $lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);

        $csrfToken = $this->has('security.csrf.token_manager')
            ? $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue()
            : null;

        return $this->renderLogin(array(
            'last_username' => $lastUsername,
            'error' => $error,
            'csrf_token' => $csrfToken,
        ));
    }

    /**
     * Renders the login template with the given parameters. Overwrite this function in
     * an extended controller to provide additional data for the login template.
     *
     * @param array $data
     *
     * @return Response
     */
    protected function renderLogin(array $data)
    {
        return $this->render('@FOSUser/Security/login.html.twig', $data);
    }

    public function checkAction()
    {
        throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
    }

    public function logoutAction()
    {
        throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
    }
}

2 回答

  • 0

    我之前有过类似的问题,尝试在防火墙上添加一个“默认”条目

    firewalls:
        default:
            pattern: ^/login$
            anonymous: ~
    
  • 1

    试试这个:

    security:
            encoders:
                FOS\UserBundle\Model\UserInterface: bcrypt
    
            role_hierarchy:
                ROLE_ADMIN:       ROLE_USER
                ROLE_SUPER_ADMIN: ROLE_ADMIN
    
            providers:
                fos_userbundle:
                    id: fos_user.user_provider.username
    
            firewalls:
                main:
                    pattern: .*
                    form_login:
                        provider: fos_userbundle
                        login_path: /login
                        check_path: /login_check
                        csrf_token_generator: security.csrf.token_manager
                    logout:
                            path:   /logout
                            target: /login
                    anonymous:   true
    
            access_control:
                - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
                - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
                - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
                - { path: ^/admin/, role: ROLE_ADMIN }
    

    并改为Sha512:

    security:
            encoders:
                FOS\UserBundle\Model\UserInterface: sha512
    

相关问题