• 《ThinkPHP 5.0快速入门》 请求和响应


    1、请求对象

    //传统调用
    $request
    = Request::instance();//实例化对象 $request->url();//获取当前的域名
    //继承thinkController
    class Index extends Controller(){
        public function hello(){
            return $this->request->url();//获取当前域名
        }
    }
    //自动注入请求对象
    class Index(){
        public function hello(Request $request){
            return $request->url();
        }
    }
    //hello方法的request参数是系统自动注入的,而不需要通过URL请求传入。个人感觉此方法最直观、最实用
    //动态绑定属性
    class Base extends Controller(){
        public function _initialize(){//优先执行的函数
            $user = User::get(Session::get('user_id'));//User = 用户模型
            Request::instance()->bind('user',$suer);//绑定用户信息到request
        }
    }
    
    //在其他控制器获取
    class Index extends Base(){
        public function(Request $request){
            $request->user->id;
            $request->user->name;
        }
    }
    //使用助手函数:不需要继承Controller类、也无需引入Request
    class Index{
        public function hello(){
            return request()->url();//获取url
    }

    2、请求信息

    //获取请求参数
    class Index(){
        public function hello(Request $request){
         ...
    $params = $request->param();//请求参数 } } 路径:http://tp5.com/index/index/hello.html?test=ddd&name=thinkphp 参数: array (size=2) 'test' => string 'ddd' (length=3) 'name' => string 'thinkphp' (length=8) name:thinkphp
    //input助手函数
    ...
    public function hello(){
        $name = input('name');
        $test = input('test');
    }
    //设置默认值和变量过滤
    ...
    public function hello(Request $request){
        $request->param('name','World','strtolower');
        //$request->param('变量','默认值','过滤条件');
    }
    //Request对象也可以用于获取其它的输入参数
    $request->file('image');
    // '上传文件信息:image';
    $request->cookie('name');
    // 'cookie参数:name';
    input('file.image');
    // '上传文件信息:image';
    input('cookie.name');
    // 'cookie参数:name';
    //获取请求参数   http://tp5.com/index/index/hello/test/ddd.html?name=thinkphp
    echo '请求方法:' . $request->method(); //GET
    echo '资源类型:' . $request->type(); //HTML
    echo '访问IP:' . $request->ip(); //127.0.0.1
    echo '是否AJax请求:' . var_export($request->isAjax(), true); //false
    dump($request->param());
    echo '请求参数:仅包含name';
    dump($request->only(['name']));
    echo '请求参数:排除name';
    dump($request->except(['name']));

    3、响应对象

    //默认输出
    return
    ['name' => 'thinkphp', 'status' => '1']; 'default_return_type' => 'json',// 默认输出类型 输出:{"name":"thinkphp","status":"1"} 'default_return_type' => 'xml',// 默认输出类型 输出:<think><name>thinkphp</name><status>1</status></think>
    use 	raitscontrollerJump;//包含许多返回的方法
    class Index{
        public function index($name=''){
            if ('thinkphp' == $name) {
                $this->success('欢迎使用ThinkPHP
            5.0','hello');
            } else {
                $this->error('错误的name','guest');
            }
        }
    
        public function hello(){
            $this->redirect('http://www.baidu.com');//跳转页面
        }
    
    }
  • 相关阅读:
    mdadm
    RAID磁盘阵列学习笔记
    内存究竟有多快?
    fping
    Intel® RAID Software User’s Guide
    为什么寄存器比内存快?
    OC-Category
    OC-id、构造方法
    OC- @property @synthesize
    OC-点语法
  • 原文地址:https://www.cnblogs.com/xinchenhui/p/11651403.html
Copyright © 2020-2023  润新知