码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-06-03 23:26:39      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:thinkphp


在线视频课程: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]);

    }


}


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

标签:thinkphp

原文地址:http://phpervip.blog.51cto.com/11075781/1931987

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!