app/Plugin/NZCustompg/Event/MypageEventSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace Plugin\NZCustompg\Event;
  3. use Eccube\Event\TemplateEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. class MypageEventSubscriber implements EventSubscriberInterface
  7. {
  8.     private $requestStack;
  9.     
  10.     public function __construct(RequestStack $requestStack)
  11.     {
  12.         $this->requestStack $requestStack;
  13.     }
  14.     
  15.     public static function getSubscribedEvents()
  16.     {
  17.         return [
  18.             'Mypage/index.twig' => 'onMypageIndex',
  19.             '@admin/login.twig' => 'onRenderTemplate',
  20.         ];
  21.     }
  22.     
  23.     public function onMypageIndex(TemplateEvent $event)
  24.     {
  25.         $request $this->requestStack->getCurrentRequest();
  26.         
  27.         if ($request) {
  28.             $session $request->getSession();
  29.             $formKey $session->get('nzcustompg_form_key');
  30.             
  31.             if ($formKey) {
  32.                 // JavaScriptで自動リダイレクト
  33.                 $snippet sprintf(
  34.                     '<script>window.location.href = "/nzcustompg/%s";</script>',
  35.                     htmlspecialchars($formKeyENT_QUOTES'UTF-8')
  36.                 );
  37.                 
  38.                 $event->addSnippet($snippet);
  39.                 
  40.                 // セッションをクリア
  41.                 $session->remove('nzcustompg_form_key');
  42.                 $session->remove('nzcustompg_redirect_url');
  43.             }
  44.         }
  45.     }
  46.     
  47.     public function onRenderTemplate(TemplateEvent $event)
  48.     {
  49.         // 何もしない(エラー回避用)
  50.     }
  51. }