8.2 页面跳转,重定向,空控制器,空操作
页面跳转
1、方法存在的文件路径
TP5\thinkphp\library\traits\controller\Jump.php
成功跳转 $this->success();
失败跳转 $this->error();
2、以登录功能为例:
login.php
<?php namespace app\index\controller; use\think\Controller; class Login extends Controller{ public function login(){ return view(); } public function check(){ $username = $_POST['username']; $password = $_POST['password']; if($username=="admin"&&$password=="123"){ //$this->success(提示信息,跳转的URL地址(默认到登陆界面), //用户需要返回的数据,跳转时间) $this->success("成功","user/index","","1"); }else{ //$this->error(提示信息,跳转的URL地址(默认到登陆界面), //用户需要返回的数据,跳转时间) //$this->error("失败"); //redirect('跳转地址','其他参数',code,'隐士参数');重定向 $this->redirect('user/index',['id'=>100,'name'=>'abc']); } } //空操作 public function _empty(){ $this->redirect('index/index'); } } ?>
//login.html
!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="{:url('check')}" method="post"> <h3>登陆页面</h3> <p> USERNAME:<input type="text" name="username" id="username" value="" /> </p> <p> PASSWORD:<input type="password" name="password" id="password" value="" /> </p> <p> <input type="submit" value="登陆"/> </p> </form> </body> </html>
<?php //类名要跟php文件名一样 namespace app\index\controller; use think\Request; class User{ public function index(){ $request = Request::instance(); var_dump($request); //echo "我是index模块下的user控制器中的index方法"; } } ?>
空控制器
<?php //空控制器 namespace app\index\controller; use think\Controller; class Error extends Controller{ //index public function index(){ $this->redirect('index/index'); } public function _empty(){ $this->redirect('index/index'); } } ?>