thinphp中$this->redirect与redirect重定向区别

来源:互联网 发布:蚊子为什么要吸血知乎 编辑:程序博客网 时间:2024/06/07 12:44

$this->redirect($this->indexUrl);

使用ThinkPHP快三天了,之前听说很简单,加上项目时间短,就没看文档,直接从别人的源代码开始学起了。

上手确实还算快,但最让我头疼的就是ThinkPHP的URL问题了,URL一会是这个,一会又变成那个了,尤其是我在重定向的时候,被搞得简直是要疯掉了。

问题:

$this->indexUrl =__ROOT__."/index.php/Admin/index";
$this-><span style="font-family: Arial, Helvetica, sans-serif;">redirect($this->indexUrl);</span>

当前URL为http://localhost/myproject/index.php/Home/Login

这段代码一执行,浏览器上的URL立马变成了http://localhost/myproject/index.php/Home/Loginmyproject/index.php/Home/index,

很明显,这次重定向的URL是原URL直接加上了传进去的URL。百度了一下,发现ThinkPHP有两种重定向手段,一种是利用Controller的$this->redirect()函数,另一种就是用$this->redirect()的原型函数redirect(string url, int delay, string msg),这里的url就是绝对路径了。一运行,浏览器中的URL果然变成了http://localhost/myproject/index.php/Home/index。

有了这两种重定向函数,就可以在相对路径与绝对路径中游刃有余了



0 0
原创粉丝点击