【问题标题】:Laravel logging out, adding a listenerLaravel 注销,添加监听器
【发布时间】:2019-08-01 19:35:27
【问题描述】:

我正在尝试为我们现有的 laravel 站点(laravel 5.2)的注销功能添加一些逻辑,但它并不像登录那么简单。

客户端的现有注销工作正常,但我要做的就是添加对我的 Cognito 实例的调用以将用户从他们的 cognito 会话中注销。基本上,当用户单击注销时,我想将他们从网站中注销,因为它已经这样做了,但也点击了我的注销端点以进行 cognito

我的困惑来自于现有的路由和身份验证控制器不完全匹配。

routes.api.php

Route::get('logout', 'API\Auth\AuthController@getLogout');

routes.auth.php

Route::get('logout', 'Auth\AuthController@getLogout')
    ->name('auth.logout');

Auth/AuthController.php(在我的构造函数中)

$this->middleware('guest', ['except' => 'getLogout']);

我的注销链接点击site/logout,它肯定会注销用户,但我想将我的呼叫放在正确的位置。我还想确保在成功注销时刷新或销毁会话

最近有人告诉我,我可能(并且可能应该)为注销事件添加一个侦听器并在那里进行调用。

在这种情况下,我究竟该怎么做,具体会去哪里?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    在您的EventServiceProvider 中,您可以将侦听器附加到注销事件并处理侦听器中的所有注销逻辑。

    protected $listen = [
        'Illuminate\Auth\Events\Logout' => [
            'App\Listeners\LogSuccessfulLogout',
        ],
    ];
    

    然后您可以在App\Listeners 中创建您的 LogSuccessfulLogout 侦听器:

        namespace App\Listeners;
        use Illuminate\Auth\Events\Logout;
    
        class LogSuccessfulLogout
        {
            /**
             * Create the event listener.
             *
             * @return void
             */
            public function __construct()
            {
                //
            }
    
            /**
             * Handle the event.
             *
             * @param  Logout  $event
             * @return void
             */
            public function handle(Logout $event)
            {
                // Do your logic
            }
        }
    

    来源:https://laravel.com/docs/5.2/authentication#events

    【讨论】:

    • 一个问题:该文件中已经有一个受保护的$listen。我可以添加另一个完整的函数,还是应该将事件添加到该函数中?
    • 你必须将你的监听器添加到那个数组中
    猜你喜欢
    • 2019-02-25
    • 2013-05-22
    • 2016-11-30
    • 1970-01-01
    • 2018-05-03
    • 2019-12-16
    • 1970-01-01
    • 2018-08-30
    • 2020-06-13
    相关资源
    最近更新 更多