来源:http://trac.seagullproject.org/wiki/Howto/HttpRedirects

HTTP重定向

SGL_HTTP::redirect()方法是用来实现页面的转换/重定向,事实上它只是一个php的header函数的封装。调用redirect与创建URLS相似,因为它可以不提供参数,或一个,或很多,顺着这种方式日益增多地定义一个更精确的目标URL.

重定向到相同的页面,调用当前的action,如果没有action,使用默认的:

SGL_HTTP::redirect();

重定向到相同模块的其它action:

HTTP::redirect(array('action' => 'myaction'));

重定向到其它的module/manager:

SGL_HTTP::redirect(array('moduleName' => 'foo', 'managerName' => 'bar'));

重写向到外部站点:

SGL_HTTP::redirect('http://example.com');

最后,下列将重定向到指定模块类的指定action,并传递适当的参数。

$options = array(
    'moduleName' => 'publisher',
    'managerName' => 'article',
    'action' => 'edit',
    'frmArticleId' => 23
);
SGL_HTTP::redirect($options);

SGL_HTTP::redirect()方法也

  • 确保了重定向URL是完遵循RFC2616规范
  • 根据需要添加session信息
  • 如果需要使用HTTPS协议

在模块中添加自定义的重定向链接

默认情况下,大部分模块通过在管理类的action映射数组中指定redirectToDefault以便在数据修改之后重定向到默认页面。这对应于所有模块父类中的SGL_Manager::_cmd_redirectToDefault。

可能你需要自己决定重定向的目标,这种情况下你可以用相同的方法:

  function _cmd_redirectToDefault(&$input, &$output)
  {
      SGL::logMessage(null, PEAR_LOG_DEBUG);
      //  if no errors have occured, redirect
      if (!SGL_Error::count()) {
          SGL_HTTP::redirect(array('frmCatID' => $this->_redirectCatId));
      //  else display error with blank template
      } else {
          $output->template = 'docBlank.html';
      }
  }

如果这样配置的话,首先判断是否出错是很重要的,因为它们应该在重定向之前被捕收到,这样它们才能被输出到屏幕上。

 
howto/httpredirects.txt · 最后更改: 2010/05/30 00:21 (外部编辑)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2