`

TP5 请求信息和页面跳转

阅读更多
<?php
namespace app\index\controller;
use think\Request;
class Index
{
    /**
     * 请求信息
     * */
    public function hello(Request $request,$name = 'World')
    {
        echo '获取当前访问的url地址:<br>';
        $request = Request::instance();
        // 获取当前URL地址 不含域名
        echo 'url: ' . $request->url() . '<br/>';
        
        echo '<br>获取请求参数:<br>';
        echo '请求方法:' . $request->method() . '<br/>';
        echo '资源类型:' . $request->type() . '<br/>';
        echo '访问IP:' . $request->ip() . '<br/>';
        echo '是否AJax请求:' . var_export($request->isAjax(), true) . '<br/>';
        echo '请求参数:';
        dump($request->param());
        echo '请求参数:仅包含name';
        dump($request->only(['name']));
        echo '请求参数:排除name';
        dump($request->except(['name']));
        
        echo '<br>获取URL信息:<br>';
        // 获取当前域名
        echo 'domain: ' . $request->domain() . '<br/>';
        // 获取当前入口文件
        echo 'file: ' . $request->baseFile() . '<br/>';
        // 获取当前URL地址 不含域名
        echo 'url: ' . $request->url() . '<br/>';
        // 获取包含域名的完整URL地址
        echo 'url with domain: ' . $request->url(true) . '<br/>';
        // 获取当前URL地址 不含QUERY_STRING
        echo 'url without query: ' . $request->baseUrl() . '<br/>';
        // 获取URL访问的ROOT地址
        echo 'root:' . $request->root() . '<br/>';
        // 获取URL访问的ROOT地址
        echo 'root with domain: ' . $request->root(true) . '<br/>';
        // 获取URL地址中的PATH_INFO信息
        echo 'pathinfo: ' . $request->pathinfo() . '<br/>';
        // 获取URL地址中的PATH_INFO信息 不含后缀
        echo 'pathinfo: ' . $request->path() . '<br/>';
        // 获取URL地址中的后缀信息
        echo 'ext: ' . $request->ext() . '<br/>';
        
        echo '<br>获取当前模块/控制器/操作信息:<br>';
        echo '模块:'.$request->module();
        echo '<br/>控制器:'.$request->controller();
        echo '<br/>操作:'.$request->action();

        echo '<br>获取路由和调度信息:<br>';
        echo '路由信息:';
        dump($request->routeInfo());
        echo '调度信息:';
        dump($request->dispatch());
    }

    /**
     * 页面跳转
     * */
    use \traits\controller\Jump;
    public function index($name='')
    {
        if ('thinkphp' == $name) {
            $this->success('欢迎使用ThinkPHP5.0','aaa');
        }else if('onestopweb' == $name){
            $this->redirect('http://onestopweb.iteye.com');
        }else {
            $this->error('错误的name','bbb');
        }
    }
    public function aaa()
    {
        return 'Hello,ThinkPHP!';
    }
    public function bbb()
    {
        return 'Hello,Guest!';
    }
}

 

请求信息的效果文本:

获取当前访问的url地址:
url: /index/index/hello/test/ddd.html?name=thinkphp

获取请求参数:
请求方法:GET
资源类型:xml
访问IP:127.0.0.1
是否AJax请求:false
请求参数:
array(2) {
  ["name"] => string(8) "thinkphp"
  ["test"] => string(3) "ddd"
}
请求参数:仅包含name
array(1) {
  ["name"] => string(8) "thinkphp"
}
请求参数:排除name
array(1) {
  ["test"] => string(3) "ddd"
}

获取URL信息:
domain: http://tp5.com
file: /index.php
url: /index/index/hello/test/ddd.html?name=thinkphp
url with domain: http://tp5.com/index/index/hello/test/ddd.html?name=thinkphp
url without query: /index/index/hello/test/ddd.html
root:
root with domain: http://tp5.com
pathinfo: index/index/hello/test/ddd.html
pathinfo: index/index/hello/test/ddd
ext: html

获取当前模块/控制器/操作信息:
模块:index
控制器:Index
操作:hello
获取路由和调度信息:
路由信息:
array(0) {
}
调度信息:
array(2) {
  ["type"] => string(6) "module"
  ["module"] => array(3) {
    [0] => string(5) "index"
    [1] => string(5) "index"
    [2] => string(5) "hello"
  }
}

 

页面跳转的效果图:

 

 

 

 

 

 

 

 

 

 

 

  • 大小: 8.8 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics