thinkphp6:controller获取参数(thinkphp 6.0.9/php 8.0.14)

一,controller获取参数的代码:

Article.php
<?php
declare (strict_types = 1);
 
namespace app\controller;
 
use app\BaseController;
use app\result\Result;
use think\Request;
use think\facade\Cache;
 
class Article extends BaseController
{
    //演示获取参数
    public function one() {
        //所有get参数
        var_dump($_GET);
        //所有post参数
        var_dump($_POST);
        //返回所有参数:request()函数
        var_dump(request()->param());
        //返回所有参数:$this->request
        var_dump($this->request->param());
        //获取指定的参数:无缺省值
        var_dump($this->request->param('name'));
        var_dump($this->request->param('age'));
        //获取指定的参数:指定缺省值
        var_dump($this->request->param('age',18));
        //获取指定的参数:指定缺省值并指定类型
        var_dump($this->request->param('age',18,'intval'));
    }
}

说明:如果不继承BaseController,

则$this->request会不可用,
可以使用request()函数
 

说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest

         对应的源码可以访问这里获取: https://github.com/liuhongdi/
         或: https://gitee.com/liuhongdi

说明:作者:刘宏缔 邮箱: 371125307@qq.com

 

二,测试效果:

1,访问:
http://127.0.0.1:8000/article/one?name=laoliu
返回如图:
说明:如果没有参数,有缺省值会转为缺省值,没有缺省时则变量值为NULL
2,访问: 
http://127.0.0.1:8000/article/one?name=laoliu&age=30

返回:

 
3,访问:
http://127.0.0.1:8000/article/one?name=laoliu&age=abcd

返回:

说明:如果指定了参数的类型,会强制转换为指定的变量类型
 

三,查看thinkphp和php的版本:

php:

root@lhdpc:~# php --version
PHP 8.0.14 (cli) (built: Dec 23 2021 11:52:42) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.14, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.14, Copyright (c), by Zend Technologies

thinkphp:

root@lhdpc:~# cd /data/php/admapi/
root@lhdpc:/data/php/admapi# php think version
v6.0.9
原文地址:https://www.cnblogs.com/architectforest/p/15742964.html