[EC-CUBE3]ログアウト時に処理を実行する

EC-CUBE3をカスタマイズしていて、ログアウト時に任意の処理を行うための設定。
下記のページを大いに参考にした。

設定内容

先ずログアウト時に処理を行うハンドラの初期化をApplication.phpに追加する。
なおフロント画面の挙動を定義したい時にはコンテナ名の末尾にcustomerを指定する。(ここを気づかずにだいぶハマった。。)

// フロント画面のログアウト時に処理を追加
$this['security.authentication.logout_handler.customer'] = $this->share(function ()  {
    $handler = new \Eccube\Security\FrontLogoutSuccessHandler(
        $this,
        $this['security.http_utils'],
        $this['security.firewalls']['customer']['logout']['target_url']
    );
    return $handler;
});

初期化の定義をしたFrontLogoutSuccessHandlerを新規に作成する。
そしてこのハンドラのonLogoutSuccess()に実行したい任意の処理を追加する。

<?php

namespace Eccube\Security;

use Eccube\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\Logout\DefaultLogoutSuccessHandler;

class FrontLogoutSuccessHandler extends DefaultLogoutSuccessHandler
{
    private $app;

    public function __construct(Application $app, HttpUtils $httpUtils, $targetUrl = '/')
    {
        parent::__construct($httpUtils, $targetUrl);
        $this->app = $app;
    }

    public function onLogoutSuccess(Request $request)
    {
        // 任意の処理を実行

        return parent::onLogoutSuccess($request);
    }
}

これでログアウト後のリダイレクト先を動的に変更したりなど、処理を追加することができる。

参考

EC-CUBE3 Controller内で強制的にログアウトさせたい

Symfony2 Dynamic Logout Target?

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

1 × five =

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください