暂无 |

ThinkPHP redirect 实例

在 Index 模块 index 方法中,重定向到本模块的 select 操作:

class IndexAction extends Action{
    public function index(){
        $this->redirect('select', array('status'=>1), 3, '页面跳转中~');
    }
}

重定向后得到的 URL 可能为:http://www.520mg.com/index.php/Index/select/status/1

由于该方法调用了 U 函数来生成实际的 URL 重定向地址,因此重定向后的 URL 可能因配置不同而有所不同:

隐藏了入口文件 index.php 的
http://www.520mg.com/Index/select/status/1
隐藏了入口文件 index.php 且设置了伪静态的
http://www.520mg.com/Index/select/status/1.html

一些常用的 redirect 重定向例子:

// 不延时,直接重定向
$this->redirect('select', array('status'=>1));
// 延时跳转,但不带参数,输出默认提示
$this->redirect('select', '', 3);
// 重定向到其他模块操作
$this->redirect('Public/login');
// 重定向到其他分组
$this->redirect('Admin-Public/login');
提示:
  1. 当延时跳转时,必须输入 params 参数(可以为空),也就是 delay 必须出现在第 3 位上。
  2. 如果发现跳转后的 URL 有问题,由于 redirect 方法调用 U 方法来生成跳转后的地址,这时候可以测试一下 U 方法生成的地址是否正确,再检查一下系统配置。
  3. 如果不想使用 U 方法生成跳转地址,可以直接使用 PHP header 函数或 $this->redirect 的原型函数 redirect(string url, int delay, string msg),注意该 url 是个绝对地址,具体参见 PHP header 函数。
java教程
php教程
php+mysql教程
ThinkPHP教程
MySQL
C语言
css
javascript
Django教程

发表评论

    评价:
    验证码: 点击我更换图片
    最新评论