2014-03-28 35 views
0

在我的視圖助手中,我需要將用戶重定向到路由。你知道我該怎麼做嗎?重定向用戶在視圖助手中路由

我的視圖助手:

namespace MyProject\View\Helper; 

use Zend\View\Helper\AbstractHelper; 
use Zend\Session\Container; 
use Zend\Http\Response; 


class Connected extends AbstractHelper 
{ 

    public function __invoke() 
    { 

    $Session = new Container('base'); 
    $Response = new Response; 

    if(!$Session->offsetExists('user_id')) 
     return $Response->toRoute('auth'); 
    } 

} 

PHP的錯誤:

PHP Fatal error: Call to undefined method Zend\\Http\\Response::toRoute() in /SRV/www/firewall/ZendFramework/module/MyProject/src/MyProject/View/Helper/Connected.php on line 19 

感謝您對您幫助所有! 最好的問候,

+0

請參閱此線程提供的答案:http://stackoverflow.com/questions/14468200/zf2-use-redirect-in-outside-of-controller – Sam

+0

我已經解決了我的問題。謝謝你,山姆。 – Dimitri

+1

迪米你可以發佈這個作爲你自己的問題的答案爲其他用戶獲得更多的參考:) – Sam

回答

0

謝謝大家,但我已經解決了我的問題。

我知道是不是更好的解決方案但做工精細:

class Connected extends AbstractHelper 
{ 

    private $global; 

    public function __construct($global) 
    { 
    $this->global = $global; 
    } 

    public function __invoke() 
    { 
    # Call the container 
    $Session = new Container('base'); 

    # If the session doesn't exist, we redirect the user 
    if(!$Session->offsetExists('user_id')) 
     header('Location: ' . $this->global['global']['url']); 

    } 

} 

感謝大家的幫助!