CodeIgniter页面重定向

CodeIgniter页面重定向 首页 / Codeigniter入门教程 / CodeIgniter页面重定向

在构建Web应用程序时,无涯教程经常需要将用户从一个页面重定向到另一页面。 redirect()函数用于此目的。

语法

redirect($uri='', $method='auto', $code=NULL)
  • $uri (string)         - URI字符串

  • $method (string)- 重定向方法("auto","location"或"refresh")

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

返回类型

void

页面重定向示例

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

<?php 
   class Redirect_controller extends CI_Controller { 
	
      public function index() { 
         /*加载 URL 辅助类*/
         $this->load->helper('url'); 
   
         /*将用户重定向到某个站点*/
         redirect('http://www.learnfk.com'); 
      }
		
      public function computer_graphics() { 
         /* 加载 URL 辅助类*/
         $this->load->helper('url'); 
         redirect('http://www.learnfk.com/computer_graphics/index.htm'); 
      } 
  
      public function version2() { 
         /*加载 URL 辅助类*/
         $this->load->helper('url'); 
   
         /*将用户重定向到某个内部控制器的方法*/
         redirect('redirect/computer_graphics'); 
      } 
		
   } 
?>

更改 application/config/routes.php 中的 routes.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将您重定向到learnfk.com网站,如果您访问以下URL,则它将您重定向到learnfk.com上的计算机图形学教程。

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

教程推荐

大模型微调实践课 -〔赵鹏昊/李杰〕

云原生基础架构实战课 -〔潘野〕

快速上手C++数据结构与算法 -〔王健伟〕

技术面试官识人手册 -〔熊燚(四火)〕

MySQL 必知必会 -〔朱晓峰〕

微信小程序全栈开发实战 -〔李艺〕

罗剑锋的C++实战笔记 -〔罗剑锋〕

Serverless入门课 -〔蒲松洋(秦粤)〕

说透敏捷 -〔宋宁〕

好记忆不如烂笔头。留下您的足迹吧 :)