ThinkPHP5学习笔记(6)请求和响应

在线视频课程:http://www.kancloud.cn/tpshop/thinkphp5/220692

  • Request请求对象和Response响应对象
  • $_GET、$_POST、$_REQUEST、$_COOKIE
  • 调用请求对象$request=Request::instance()
  • $this->request或request()->url();助手函数
  • input()助手函数
  • 响应格式 json xml 模板渲染
  • 页面跳转 重定向
  • $this->success $this->error $this->redirect

<?php
namespace app\admin\controller;

use think\Request;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        return ‘<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>‘;
    }

    public function hello(){
        //  获取请求类型
        if(Request::instance()->isGet()) {echo ‘当前为GET请求‘;}
        if(Request::instance()->isPost())echo ‘当前为POST请求‘;
        if(Request::instance()->isPut()) echo ‘当前为PUT请求‘;
        if(Request::instance()->isDelete()) echo ‘当前为DELETE请求‘;
        if(Request::instance()->isAjax()) echo ‘当前为AJAX请求‘;
        if(Request::instance()->isPjax()) echo ‘当前为PJAX请求‘;
        if(Request::instance()->isMobile()) echo ‘当前为Mobile请求‘;
        if(Request::instance()->isHead()) echo ‘当前为HEAD请求‘;
        if(Request::instance()->isPatch()) echo ‘当前为PATCH请求‘;
        if(Request::instance()->isOptions()) echo ‘当前为OPTIONS请求‘;
        if(Request::instance()->isCli()) echo ‘当前为cli‘;
        if(Request::instance()->isCgi()) echo ‘当前为cgi‘;

        if(request()->isGet()){echo ‘当前为GET请求‘;};

        // 获取URL信息

        $request = Request::instance();
        $request = request();
        echo ‘domain:‘.$request->domain().‘<br/>‘;
        echo ‘file:‘.$request->baseFile().‘<br/>‘;
        echo ‘url:‘.$request->url().‘<br/>‘;
        echo ‘url with domain:‘.$request->url(true).‘<br/>‘;
        echo ‘url without query:‘.$request->baseUrl().‘<br/>‘;
        echo ‘root:‘.$request->root().‘<br/>‘;
        echo ‘root with domain:‘.$request->root(true).‘<br/>‘;
        echo ‘pathinfo:‘.$request->pathinfo().‘<br/>‘;
        echo ‘pathinfo:‘.$request->path().‘<br/>‘;
        echo ‘ext‘.$request->ext().‘<br/>‘;

        // 设置/获取 模块/控制器/操作名称
        $request = Request::instance();
        echo ‘当前模块名称是‘.$request->module();
        echo ‘当前控制器名称是‘.$request->controller();
        echo ‘当前操作名称是‘.$request->action();

        Request::instance()->module(‘module_name‘);
        echo ‘当前模块名称是‘.$request->module();

        // 获取请求参数
        echo ‘请求方法:‘.$request->method().‘<br/>‘;
        echo ‘资源类型:‘.$request->type().‘<br/>‘;
        echo ‘访问地址:‘.$request->ip().‘<br/>‘;
        echo ‘是否Ajax请求:‘.var_export($request->isAjax(),true);
        echo ‘请求参数:‘;
        dump($request->param());
        echo ‘请求参数:仅包含name‘;
        dump($request->only([‘name‘]));
        echo ‘请求参数:排除name‘;
        dump($request->except([‘name‘]));

        // 获取路由和调度信息

        echo ‘路由信息‘;
        dump($request->route());

        echo ‘调度信息‘;
        dump($request->dispatch());

        // 设置请求信息
        $request = Request::instance();
        $request->root(‘index.php‘);
        $request->pathinfo(‘admin/index/hello‘);

    }

    public function hello2(){
        $data = [‘name‘=>‘thinkphp‘,‘status‘=>1];
        // 浏览器安装json插件
        return json($data);
        // return xml($data);
        // $this->assign(‘name‘,‘渲染 模板‘);
        // return $this->fetch();
    }

    // hello3
    // 如果没有 extends Controller 就写:
    // use \traits\controller\Jump;
    public function hello3(){

        $this->success(‘正常的页面跳转‘,‘hello‘);
        $this->error(‘错误的页面跳转‘,‘hello2‘);
        $this->redirect(‘http://tp-shop.cn‘);
    }

    public function hello4(){
        // 检测变量是否设置
        Request::instance()->has(‘id‘,‘get‘);
        Request::instance()->has(‘name‘,‘post‘);

        input(‘?get.id‘);
        input(‘?post.name‘);

        // 变量获取
        $request = Request::instance();
        echo $request->bind(‘user_name‘,‘张三‘);
        echo $request->user_name;

        print_r($request->param());
        echo ‘<br/>‘;
        echo $request->param(‘name‘);
        print_r(input());
        echo ‘<br/>‘;
        echo input(‘name‘);

        print_r(Request::instance()->param(false));
        print_r(Request::instance()->param(true));

        echo (input(‘param.name‘));
        print_r (input(‘param.‘));
         // 或
        echo (input(‘name‘));
        print_r(‘‘);

        // 获取GET变量
        echo (Request::instance()->get(‘id‘));
        echo (Request::instance()->get(‘name‘));
        print_r(Request::instance()->get());    // 获取经过过滤的
        print_r(Request::instance()->get(false));//获取原始变量
        // 或
        echo input(‘get.id‘);
        echo input(‘get.name‘);
        print_r(input(‘get.‘));

        // 获取POST变量
        echo (Request::instance()->post(‘name‘));
        print_r(Request::instance()->post());
        print_r(Request::instance()->post(false));
        // 或
        echo (input(‘post.name‘));
        print_r(‘post.‘);

        // 获取PUT变量
        echo (Request::instance()->put(‘name‘));
        print_r (Request::instance()->put());
        print_r (Request::instance()->put(false));
        // 或
        echo (input(‘put.name‘));
        print_r(input(‘put.‘));

        //  获取REQUEST变量
        echo (Request::instance()->request(‘id‘));
        print_r(Request::instance()->request());
        print_r(Request::instance()->request(false));
        // 或
        echo (input(‘request.id‘));
        print_r (input(‘request.‘));

        // 获取SERVER变量
        echo (Request::instance()->server(‘PHP_SELF‘));
        print_r(Request::instance()->server());
        // 或
        echo (input(‘server.PHP_SELF‘));
        print_r(input(‘server.‘));

        //获取SESSION变量
        echo (Request::instance()->session(‘user_id‘));
        print_r(Request::instance()->session());
        // 或
        echo (input(‘session.user_id‘));
        print_r(input(‘session.‘));

        // 获取Cookie变量
        echo (Request::instance()->cookie(‘user_id‘));
        print_r(Request::instance()->cookie());
        // 或
        echo (input(‘cookie.user_id‘));
        print_r(input(‘cookie.‘));

        // 变量过滤
        // 在application/config.php中设置
        // ‘default_filter‘=>‘htmlspecialchars‘;
        Request::instance()->filter(‘htmlspecialchars‘);
        Request::instance()->filter([‘strip_tags‘,‘htmlspecialchars‘]);
        // 在添加变量的时候添加过滤方法
        Request::instance()->get(‘name‘,‘‘,‘htmlspecialchars‘);
        Request::instance()->param(‘username‘,‘‘,‘strip_tags‘);
        Request::instance()->post(‘name‘,‘‘,‘org\Filter::safeHtml‘);

        Request::instance()->param(‘username‘,‘‘,‘strip_tags,strtolower‘);

        Request::instance()->post(‘email‘,‘‘,‘FILTER_VALIDATE_EMAIL‘);
        Request::instance()->post(‘email‘,‘‘,‘email‘);
        Request::instance()->get(‘name‘,‘‘,false);

        // 获取部分变量
        Request::instance()->only(‘id,name‘);
        Request::instance()->only([‘id‘,‘name‘]);
        Request::instance()->only([‘id‘,‘name‘],‘get‘);
        Request::instance()->only([‘id‘,‘name‘],‘post‘);

        // 排除部分变量
        Request::instance()->except(‘id,name‘);
        Request::instance()->except([‘id‘,‘name‘]);
        Request::instance()->except([‘id‘,‘name‘],‘get‘);
        Request::instance()->except([‘id‘,‘name‘],‘post‘);

        //  变量修饰符
        input(‘get.id/d‘);
        input(‘get.name/s‘);
        input(‘get.ids/a‘);
        Request::instance()->get(‘id/d‘);

    }

    public function hello5(){
        Request::instance()->get([‘id‘=>10]);
        Request::instance()->post([‘name‘=>‘thinkphp‘]);
        // 尽量避免直接修改$_GET,$_POST,也不能直接修改param变量,下面写法无效
        // Request::instance()->param([‘id‘=>10]);

    }

}
时间: 2024-10-11 06:18:40

ThinkPHP5学习笔记(6)请求和响应的相关文章

[ajax 学习笔记] ajax 的服务器响应

在上一篇[ajax 学习笔记] ajax初试中,简单了解了一下ajax. 我是参考AJAX详解.chm学习的,资源已上传.参考链接:Ajax 专题 今天又学习了ajax中关于服务器响应的一些知识. ajax中服务器的响应是通过响应函数将服务器返回的数据呈现到页面的.服务器的响应体现在服务器响应回调函数中. 在上一篇的例子中,响应函数为: function updatePage(){ if(xmlHttp.readyState == 4){ //http就绪状态 if(xmlHttp.status

iOS学习笔记---网络请求

一.HTTP协议的概念 HTTP协议:Hyper Text Transfer Protocol(超文本传输协议)是用于从万维网服务器传送超文本到本地浏览器的传输协议.HTTP是一个应用层协议,由请求和响应构成,是一个标准的客户端服务器模型(c/s) HTTP协议的特点 支持客户/服务器模式(c/s模式) 简单快速 灵活,HTTP允许任意类型的对象进行传输,正在传输的对象类型由Content-type加以标记 iOS学习笔记---网络请求

WebService学习笔记-Ajax请求Webservice

Webservice地址为 http://192.168.13.232:8989/ws_01/umgsai JSP页面地址为 http://192.168.13.232:8080/Demo/index.jsp Webservice的请求体如下 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.umgsai.com/" x

.net学习笔记----HttpContext请求上下文

一.HttpContext概述 HttpContext基于HttpApplication的处理管道,由于HttpContext对象贯穿整个处理过程,所以,可以从HttpApplication处理管道的前端将状态数据传递到管道的后端,完成状态的传递任务. HttpContext的生命周期从服务器接收的HTTP请求开始到反应发送回客户端结束. 在WebForm或类库(包括MVC)项目中,通过Current静态属性,就能够获得HttpContext的对象. HttpContext context =

angular2 学习笔记 ( Http 请求)

refer : https://angular.cn/docs/ts/latest/guide/server-communication.html https://xgrommx.github.io/rx-book/index.html 概念上没什么太多的区别. 下面记入一些例子和小区别 : 不同的地方 : 1.不支持 upload file (游览器的 xhr 可以很容易的通过 send formData 实现 ajax upload), ng2 没有 2.不支持 ng1 的 intercep

angular学习笔记(二十五)-$http(3)-转换请求和响应格式

本篇主要讲解$http(config)的config中的tranformRequest项和transformResponse项 1. transformRequest: $http({ transformRequest: function(data){ //对前台发送的data进行处理 return data } }) 这个在测试的时候遇到了很大的问题.只要经过transformRequest函数处理,哪怕是不做任何处理,node后台都会报错,需要尝试使用php 2. transformResp

Binder学习笔记(七)—— ServiceManager如何响应addService请求

有了<ServiceManager如何响应checkService请求>的探索,研究addService就轻车熟路了,中间过程不再多说,仅把关键节点列出: frameworks/native/cmds/servicemanager/service_manager.c:347 int main(int argc, char **argv) { …… binder_loop(bs, svcmgr_handler); …… } frameworks/native/cmds/servicemanage

Android学习笔记之HttpClient实现Http请求....

PS:最近光忙着考试了....破组成原理都看吐了....搞的什么也不想干...写篇博客爽爽吧....貌似明天就考试了...sad... 学习笔记: 1.如何实现Http请求来实现通信.... 2.解决Android 2.3 版本以后无法使用Http请求问题....   这里我使用HttpClient来开发Http程序来完成简单的网络通信....其实使用HttpUrlConnection也可以实现,不过HttpClient可以完成HttpUrlConnection的所有功能,并且还自己增加了其他的

angular学习笔记(二十六)-$http(4)-设置请求超时

本篇主要讲解$http(config)的config中的timeout项: $http({ timeout: number }) 数值,从发出请求开始计算,等待的毫秒数,超过这个数还没有响应,则返回错误 demo: html: <!DOCTYPE html> <html ng-app = 'HttpGet'> <head> <title>18.4 $http(2)</title> <meta charset="utf-8"