app/Plugin/ShippingRedirect/Event.php line 36

Open in your IDE?
  1. <?php
  2. namespace Plugin\ShippingRedirect;
  3. use Eccube\Event\TemplateEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Routing\RouterInterface;
  10. class Event implements EventSubscriberInterface
  11. {
  12.     protected $router;
  13.     protected $redirect_url;
  14.     public function __construct(RouterInterface $router) {
  15.         $this->router $router;
  16.     }
  17.     
  18.     /**
  19.      * @return array
  20.      */
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             'Shopping/index.twig' => 'index',
  25.             KernelEvents::RESPONSE => 'onResponse',
  26.         ];
  27.     }
  28.     /**
  29.      * @param TemplateEvent $event
  30.      */
  31.     public function index(TemplateEvent $event)
  32.     {
  33.         $parameters $event->getParameters();
  34.         $Order $parameters['Order'];
  35.         // shippingIdを取得して、リダイレクト先にshopping_shipping_editを設定する
  36.         $this->redirect_url $this->router->generate('shopping_shipping_edit', ['id' => $Order->getShippings()->current()->getId()]);
  37.     }
  38.     public function onResponse(ResponseEvent $event)
  39.     {
  40.         // shopping_nonmember のフルパスのURLを取得
  41.         $url_shopping_nonmember $this->router->generate('shopping_nonmember', [], UrlGeneratorInterface::ABSOLUTE_URL);
  42.         // Requestオブジェクトを取得
  43.         $request $event->getRequest();
  44.         // リファラを取得
  45.         $referrer $request->headers->get('referer');
  46.         // リファラが shopping_nonmember の場合、$this->redirect_url にリダイレクトする
  47.         if($referrer === $url_shopping_nonmember && isset($this->redirect_url)){
  48.             $response = new RedirectResponse($this->redirect_url);
  49.             $event->setResponse($response);
  50.         }
  51.     }
  52. }