2017-04-10 88 views
0

首先,我爲我的壞英語道歉。[GUZZLE] [SYMFONY3] listen guzzle exception event

我來這裏是因爲我正在尋找一些方法來聽例外狂飲事件重定向如果我收到的狀態碼401

我發現了一個事件「PostTransactionEvent」,允許獲得的數據結構到登錄頁面我的回覆。它使他的工作,但我不能重定向到登錄頁面。看來RedirectResponse方法沒有執行。

services.yml:

glpi.expire_listener: 
    class: GlpiBundle\Expire\ExpireListener 
    arguments: ["@router","@request_stack"] 
    tags: 
     - { name: kernel.event_listener, event: guzzle_bundle.post_transaction, method: check } 

ExpireListener.php

namespace GlpiBundle\Expire; 

use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEventListenerInterface; 
use EightPoints\Bundle\GuzzleBundle\Events\PostTransactionEvent; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; 

class ExpireListener implements GuzzleEventListenerInterface 
{ 
protected $service_name; 

protected $request_stack; 

public function __construct($router,$request_stack) 
{ 
    $this->router = $router; 
    $this->request_stack = $request_stack; 
} 

public function check(PostTransactionEvent $event) 
{ 

    $response_transaction = $event->getTransaction(); 


    $e = new ExpireApi(); 
    $available = $e->deconnect($response_transaction); 

    if ($available) { 
     return new RedirectResponse($this->router->generate('logout')); 

    } 

    $event->setTransaction($response_transaction); 
} 

public function setServiceName($serviceName){ 
    $this->service_name = $serviceName; 

} 
} 

要求:

  $reponse_categories =$client->get('/apirest.php/itilcategory?searchText[itilcategories_id]='.self::ID_CAMPUS_ID, 
      [ 
       "headers"=> 
        [ 
         "App-Token"=>TOKEN, 
         "Session-Token"=>SESSION 
        ], 
        'exceptions'=>false 
      ]); 

預先感謝您的幫助,

回答

0

使用此代碼:

// redirect to login page 
$redResponse = new RedirectResponse($this->router->generate('login')); 
$redResponse->send(); 

下面是完整的代碼:

https://github.com/viher3/yuwik-frontend/blob/develop/src/EventListeners/Guzzle/ExpiredJwt.php

商祺!

+0

非常感謝Viher。而已。 – ryl

+0

這就像我想工作的工作!只有一件事,我的flash在重定向到登錄頁面後是空的。因此,我沒有設置flashbah服務,而是將其設置爲路由控制器。 – ryl