2017-12-28 20 views
2

私は自分のサービスクラスを作成しました。その中には、リダイレクト先のルートを選択する前に、最小限の論理チェックを実行することになっているhandleRedirect()という関数があります。サービスクラス内でリダイレクトしますか?

class LoginService 
{ 
    private $CartTable; 
    private $SessionCustomer; 
    private $Customer; 

    public function __construct(Container $SessionCustomer, CartTable $CartTable, Customer $Customer) 
    { 
     $this->SessionCustomer = $SessionCustomer; 
     $this->CartTable  = $CartTable; 
     $this->Customer   = $Customer; 

     $this->prepareSession(); 
     $this->setCartOwner(); 
     $this->handleRedirect(); 
    } 

    public function prepareSession() 
    { 
     // Store user's first name 
     $this->SessionCustomer->offsetSet('first_name', $this->Customer->first_name); 
     // Store user id 
     $this->SessionCustomer->offsetSet('customer_id', $this->Customer->customer_id); 
    } 

    public function handleRedirect() 
    { 
     // If redirected to log in, or if previous page visited before logging in is cart page: 
     //  Redirect to shipping_info 
     // Else 
     //  Redirect to/
    } 

    public function setCartOwner() 
    { 
     // GET USER ID FROM SESSION 
     $customer_id = $this->SessionCustomer->offsetGet('customer_id'); 
     // GET CART ID FROM SESSION 
     $cart_id = $this->SessionCustomer->offsetGet('cart_id'); 
     // UPDATE 
     $this->CartTable->updateCartCustomerId($customer_id, $cart_id); 
    } 
} 

このサービスは、ログインまたは登録が成功した後にコントローラで呼び出されます。私はここからredirect()->toRoute();にアクセスする最良の方法が何であるか(または私がここでそれを行うべきかどうか)はわかりません。

また、自分のコードがどのように構成されているかについての他のコメントがありましたら、お気軽にお寄せください。

答えて

0

サービス内でプラグインを使用することは、コントローラを設定する必要があるため、悪い考えです。サービスが作成され、プラグインをインジェクトすると、コントローラインスタンスが分からないため、エラー例外が発生します。ユーザーをリダイレクトする場合は、リダイレクトプラグインのようにレスポンスオブジェクトを編集するだけです。

例を明確かつ単純にするためにコードを削除したことに注目してください。

class LoginServiceFactory implements FactoryInterface 
{ 
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null) 
    { 
     return new LoginService($container->get('Application')->getMvcEvent()); 
    } 
} 

class LoginService 
{ 
    /** 
    * @var \Zend\Mvc\MvcEvent 
    */ 
    private $event; 

    /** 
    * RedirectService constructor. 
    * @param \Zend\Mvc\MvcEvent $event 
    */ 
    public function __construct(\Zend\Mvc\MvcEvent $event) 
    { 
     $this->event = $event; 
    } 

    /** 
    * @return Response|\Zend\Stdlib\ResponseInterface 
    */ 
    public function handleRedirect() 
    { 
     // conditions check 
     if (true) { 
      $url = $this->event->getRouter()->assemble([], ['name' => 'home']); 
     } else { 
      $url = $this->event->getRouter()->assemble([], ['name' => 'cart/shipping-info']); 
     } 

     /** @var \Zend\Http\Response $response */ 
     $response = $this->event->getResponse(); 
     $response->getHeaders()->addHeaderLine('Location', $url); 
     $response->setStatusCode(302); 

     return $response; 
    } 
} 

今、あなたのコントローラ内から次の操作を行うことができます

確かにクリアでシンプルな

return $loginService->handleRedirect();

+0

、ありがとうございました! – herondale

関連する問題