CodeIgniter - 页面重定向

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

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

语法

redirect($uri='', $method='auto', $code=NULL)

参数

链接:https://www.learnfk.comhttps://www.learnfk.com/codeigniter/codeigniter-page-redirection.html

来源:LearnFk无涯教程网

  • $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

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

技术教程推荐

AI技术内参 -〔洪亮劼〕

.NET Core开发实战 -〔肖伟宇〕

系统性能调优必知必会 -〔陶辉〕

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

分布式系统案例课 -〔杨波〕

跟月影学可视化 -〔月影〕

Flink核心技术与实战 -〔张利兵〕

打造爆款短视频 -〔周维〕

高楼的性能工程实战课 -〔高楼〕

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