使用 symfony 响应登录路由

时间:2021-07-06 05:03:24

标签: php reactjs symfony

我有一个问题,我有一个使用 react 的 symfony 项目,为了使路由能够到达 react 或后端,我有一个这样定义的默认控制器:

class DefaultController extends AbstractController
{
    /**
     * @Route("/{reactRouting}", name="home",  requirements={"reactRouting"="^(?!api).+"},defaults={"reactRouting": null})
     */
    public function index()
    {
        return $this->render('default/index.html.twig');
    }

这样,除了来自 api/'stay' 的路由之外的所有路由都在 react 中,意思是到达后端我需要一个以 /api 开头的路由。 现在这是我的 security.yaml:

security:
    #enable_authenticator_manager: true
    encoders:
        App\Entity\Students:
            algorithm: auto

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\Students
                property: email

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: false
            lazy: true
            pattern: ^/api/
            guard:
                authenticators:
                    - App\Security\TokenAuthenticator
            logout:
                  path: app_logout
                  target: /
                  invalidate_session: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        # - { path: ^/admin, roles: ROLE_ADMIN }
         - { path: ^/login, roles: PUBLIC_ACCESS}
         - { path: ^/api/login, roles: PUBLIC_ACCESS}
         - { path: ^/api/register, roles: PUBLIC_ACCESS}
         - { path: ^/api, roles: ROLE_STUDENT}

因此,对于登录,我想访问我的 LoginController.php,但是如果我执行 /login,它不会去任何地方,如果我执行 /api/login,它会访问我也不想要的 tokenAuthenticator。

我该怎么做才能让 /api/login 或 /login(网址对我来说并不重要)到达我的 LoginController?

 /**
     * @Route("/api/login", name="app_login")
     * @throws Exception
     */
    public function login(Request $request): Response
    { //some code}

我尝试在 security.yaml 中添加 form_login 但没有成功,我也尝试了 json_login ... 有什么想法吗?

谢谢

0 个答案:

没有答案