重定向用户以在视图助手中路由

时间:2014-03-28 10:27:15

标签: php redirect zend-framework2

在我的View Helper中,我需要将用户重定向到路由。你知道我怎么做吗?

我的视图助手:

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

谢谢你的帮助! 最好的问候,

1 个答案:

答案 0 :(得分: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']);

  }

}

谢谢大家的帮助!

相关问题