目录

CodeIgniter - 页面重定向( Page Redirection)

在构建Web应用程序时,我们经常需要将用户从一个页面重定向到另一个页面。 CodeIgniter使我们的工作变得轻松。 redirect()函数用于此目的。

Syntax

重定向( $uri = '', $method = 'auto', $code = NULL

Parameters

  • $uristring ) - URI字符串

  • $methodstring ) - 重定向方法('auto','location'或'refresh')

  • $codestring ) - HTTP响应代码(通常为302或303)

Return type

void

第一个参数可以有两种类型的URI。 我们可以将完整的站点URL或URI段传递给您想要指向的控制器。

第二个可选参数可以包含auto,location或refresh中的三个值中的任何一个。 默认为auto。

第三个可选参数仅适用于位置重定向,它允许您发送特定的HTTP响应代码。

例子 (Example)

创建一个名为Redirect_controller.php的控制器并将其保存在application/controller/Redirect_controller.php

<?php 
   class Redirect_controller extends CI_Controller { 
      public function index() { 
         /*Load the URL helper*/ 
         $this->load->helper('url'); 
         /*Redirect the user to some site*/ 
         redirect('http://www.iowiki.com'); 
      }
      public function computer_graphics() { 
         /*Load the URL helper*/ 
         $this->load->helper('url'); 
         redirect('http://www.iowiki.com/computer_graphics/index.htm'); 
      } 
      public function version2() { 
         /*Load the URL helper*/ 
         $this->load->helper('url'); 
         /*Redirect the user to some internal controller’s method*/ 
         redirect('redirect/computer_graphics'); 
      } 
   } 
?>

更改application/config/routes.phproutes.php文件,为上述控制器添加路由,并在文件末尾添加以下行。

$route['redirect'] = 'Redirect_controller'; 
$route['redirect/version2'] = 'Redirect_controller/version2'; 
$route['redirect/computer_graphics'] = 'Redirect_controller/computer_graphics';

在浏览器中键入以下URL以执行该示例。

http://yoursite.com/index.php/redirect

上面的URL会将您重定向到iowiki.com网站,如果您访问以下URL,它会将您重定向到iowiki.com上的计算机图形教程。

http://yoursite.com/index.php/redirect/computer_graphics
↑回到顶部↑
WIKI教程 @2018