Zend Framework动作助手Redirector用法实例详解

(编辑:jimmy 日期: 2024/10/1 浏览:2)

本文实例讲述了Zend Framework动作助手Redirector用法。分享给大家供大家参考,具体如下:

Redirector 提供另一种实现方式,帮助程序重定向到内部或者外部页面;

转向器(Redirector)助手让你使用一个转向器对象帮助程序重定向到新的URL。与_redirect()方法相比,它具有多项优势。例如能够在转向器对象中预先配置整个站点的行为,或者使用与Zend_Controller_Action::_forward()相似的gotoSimple($action, $controller, $module, $params)接口。

转向器拥有影响重定向行为的大量方法:

setCode() 设置重定向过程中使用的HTTP响应码。

setExit() 在重定向后强制执行exit()方法。默认已设定。

setGotoSimple()设置默认的URL,当没有提供参数给gotoSimple()方法时转向该URL。可以使用类似Zend_Controller_Action::_forward()的API:setGotoSimple($action, $controller = null, $module = null, array $params = array());

setGotoRoute()设置基于一个注册路由器的URL。通过传入一个键/值数组和一个路由器名,它将根据路由器的类型和定义来组织URL。

setGotoUrl()设置默认的URL,当没有参数传入gotoUrl(),将使用该URL。接受单个URL字符串。

setPrependBase()在setGotoUrl()、gotoUrl()或者gotoUrlAndExit()指定的URL前面,加入请求对象的基地址(base URL)。

setUseAbsoluteUri()强制转向器在重定向时使用绝对的URI。当该选项设定后,将使用$_SERVER['HTTP_HOST']、 $_SERVER['SERVER_PORT']和 $_SERVER['HTTPS']以及重定向方法指定的URL,来形成一个完整的URI。该选项目前默认关闭,将来的版本可能会默认开启。

此外,转向器中还有大量方法来执行实际的重定向。

gotoSimple()使用setGotoSimple()(类似_forward()的API)来构建URL并执行重定向。

gotoRoute()使用setGotoRoute()(路由组装route-assembly)来构建URL并执行重定向。

gotoUrl()使用setGotoUrl() URL字符串)来构造URL并执行重定向。
最后,你可以在任何时刻使用getRedirectUrl()确定当前的重定向URL。

基础用例

Example #5 设定选项

这个例子改变了几个选项,包括设定重定向时使用的HTTP状态码为303,重定向时不默认退出,以及定义了默认的URL供重定向使用。

class SomeController extends Zend_Controller_Action
{
  /**
   * Redirector - defined for code completion
   *
   * @var Zend_Controller_Action_Helper_Redirector
   */
  protected $_redirector = null;
  public function init()
  {
    $this->_redirector = $this->_helper->getHelper('Redirector');
    // Set the default options for the redirector
    // Since the object is registered in the helper broker, these
    // become relevant for all actions from this point forward
    $this->_redirector->setCode(303)
             ->setExit(false)
             ->setGotoSimple("this-action",
                     "some-controller");
  }
  public function myAction()
  {
    /* do some stuff */
    // Redirect to a previously registered URL, and force an exit
    // to occur when done:
    $this->_redirector->redirectAndExit();
    return; // never reached
  }
}

Example #6 使用默认设定

这个例子假定使用默认设定,也就意味着任何重定向将导致立即退出。

// ALTERNATIVE EXAMPLE
class AlternativeController extends Zend_Controller_Action
{
  /**
   * Redirector - defined for code completion
   *
   * @var Zend_Controller_Action_Helper_Redirector
   */
  protected $_redirector = null;
  public function init()
  {
    $this->_redirector = $this->_helper->getHelper('Redirector');
  }
  public function myAction()
  {
    /* do some stuff */
    $this->_redirector
      ->gotoUrl('/my-controller/my-action/param1/test/param2/test2');
    return; // never reached since default is to goto and exit
  }
}

Example #7 使用goto()的_forward()API

gotoSimple()'s API 模拟了Zend_Controller_Action::_forward()。主要的不同在于它通过传入的参数构造URL,使用默认路由器的默认格式:module/:controller/:action/*。然后重定向而不是继续动作链循环。

class ForwardController extends Zend_Controller_Action
{
  /**
   * Redirector - defined for code completion
   *
   * @var Zend_Controller_Action_Helper_Redirector
   */
  protected $_redirector = null;
  public function init()
  {
    $this->_redirector = $this->_helper->getHelper('Redirector');
  }
  public function myAction()
  {
    /* do some stuff */
    // Redirect to 'my-action' of 'my-controller' in the current
    // module, using the params param1 => test and param2 => test2
    $this->_redirector->gotoSimple('my-action',
    'my-controller',
    null,
    array('param1' => 'test',
       'param2' => 'test2'
       )
    );
  }
}

Example #8 通过gotoRoute()使用路由组装(route assembly)

下面的例子使用了路由器的assemble()方法,基于传入参数的关联数组来创建URL。假定下面的路由已经注册:

$route = new Zend_Controller_Router_Route(
  'blog/:year/:month/:day/:id',
  array('controller' => 'archive',
     'module' => 'blog',
     'action' => 'view')
);
$router->addRoute('blogArchive', $route);

给定一个数组,其中年份为2006,月份为4,日期为24,id为42,据此可以组装URL/blog/2006/4/24/42。

class BlogAdminController extends Zend_Controller_Action
{
  /**
   * Redirector - defined for code completion
   *
   * @var Zend_Controller_Action_Helper_Redirector
   */
  protected $_redirector = null;
  public function init()
  {
    $this->_redirector = $this->_helper->getHelper('Redirector');
  }
  public function returnAction()
  {
    /* do some stuff */
    // Redirect to blog archive. Builds the following URL:
    // /blog/2006/4/24/42
    $this->_redirector->gotoRoute(
      array('year' => 2006,
         'month' => 4,
         'day' => 24,
         'id' => 42),
      'blogArchive'
    );
  }
}

Zend_Controller_Action_Helper_Redirector的源码。

通过源代码不难看出实现方法,以及常见的使用方法。

<"off") "\n", "\r"), '', $url);
    if (null !== $options) {
      if (isset($options['exit'])) {
        $this->setExit(($options['exit']) "%s" called on redirector', $method));
  }
}

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?