我正在try 创建一个注销功能,当点击从下面(例子)重定向到必要的页面.

示例:

  1. 如果任何人点击以aaa.io/User-&>开头的网站注销,请转到aaa.io/登录.
  2. 如果任何人单击以aaa.io/dev/{id}->开头的网站注销,请转到aaa.io/home/{id}

如何创建两个注销功能,将重定向到两个独立的页面?我已经try 了第一个例子,运行良好.我听说我们可以使用symfony防火墙来做,但无法得到它.

#security.yaml
security:
    enable_authenticator_manager: true
    # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
    password_hashers:
        Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
        App\Entity\Dimitry:
            algorithm: auto

    providers:
        app_user_provider:
            entity:
                class: App\Entity\Dimitry
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            lazy: true
            provider: app_user_provider
            custom_authenticator: App\Security\LoginAuthenticator
            logout:
                path: app_logout
                target: app_login

//Security Controller
#[Route(path: '/login', name: 'app_login')]
    public function login(AuthenticationUtils $authenticationUtils): Response
    {
         if ($this->getUser()) {
             return $this->redirectToRoute('app_home');
         }

        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
    }

    #[Route(path: '/logout', name: 'app_logout')]
    public function logout()
    {
        return $this->redirectToRoute('app_login');

        //throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
    }

推荐答案

注销方法永远不会被调用,因为它被防火墙上的注销键拦截.因此,不会执行公共函数注销中的代码行.

在国际海事组织中,您可以使用事件:

  1. 创建订阅者、
  2. LogoutEvent::class上订阅,
  3. 分析注销事件提供的请求
  4. 用它来确定路由,
  5. 捕获注销事件提供的响应,
  6. 使用UrlGenerator重定向用户,
  7. 更新响应以重定向到相应的路由

文档提供了a very good example个,可以作为您的逻辑模板.您的订阅者可能如下所示:

// src/EventListener/LogoutSubscriber.php
namespace App\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Http\Event\LogoutEvent;

class LogoutSubscriber implements EventSubscriberInterface
{
    public function __construct(
        private UrlGeneratorInterface $urlGenerator
    ) {
    }

    public static function getSubscribedEvents(): array
    {
        //2 - Subscribe to LogoutEvent
        return [LogoutEvent::class => 'onLogout'];
    }

    public function onLogout(LogoutEvent $event): void
    {
        // get the security token of the session that is about to be logged out
        $token = $event->getToken();

        // 3. get the current request
        $request = $event->getRequest();

        // 4. Your own logic to analyze the URL
        $route = 'homepage';//default route
        if (...) {
            $route = 'URL1';
        }
        if (...) {
            $route = 'URL2';
        }

        // 5. get the current response, if it is already set by another listener
        $response = $event->getResponse();

        // configure a custom logout response to the homepage
        $response = new RedirectResponse(
            $this->urlGenerator->generate($route),
            RedirectResponse::HTTP_SEE_OTHER
        );
        $event->setResponse($response);
    }
}

Php相关问答推荐

获得客户S在WooCommerce为期1年的总支出

如何隐藏x轴图表上的值

PHP Laravel Web应用启动时间&>15秒

免费送货柜台,考虑到折扣码

移动应用:在PHP中忽略Cookie?

WooCommerce中基于用户角色的不同产品差价

Symfony Validator:如何使用XML表示法验证深度嵌套的数据?

为什么PHP PDO忽略NOT NULL约束?

Filament v3:具有 hasMany 和 BelongsTo 的关系管理器

PHP 支持 APNG 文件吗?

simplexml_load_file 和 simplexml_load_string 返回具有不同编码的相同数据

使用自定义规则进行 Livewire 验证不会显示错误

向元素添加类时添加循环错误

Laravel路由中的符号在slug中会创建额外的斜杠

从图像URL中获取文件扩展名?

Laravel:ArgumentCountError: 函数 App\Mail 的参数太少

在 Laravel、Vuejs 和 Inertia 中配置 TypeScript 的问题

在 GA4 Data API V1 Beta 中使用 inListFilter 过滤器时出错

列表不在 GROUP BY 子句中并且包含 X2CRM 中的非聚合列

一个php应用程序可以实例化多个RNG吗