Thinkphp5获取参数方法

Thinkphp5中路由获取参数

第一种方法:

在定义路由的时候,如下写法:

use  think\Route;

Route::post(‘hello/:id’,sample/test/hello);    //路由post方法

url地址:http://ServerName/hello/123?name=huihui

上面路由地址传了两个参数:id=123   name=huihui

第二种方法:

用Request方法来获取参数,先要引入Request类

use  think\Request;

Class Test

{

       Public function  hello(){

              $id= Request::instance()->param(‘id’);

              $name= Request::instance()->param(‘name’);

}

}

该方法不区分get,post等http请求类型,都是这样获取参数。

还可以一次性获取所有参数写法:

$all = Request::instance()->param();    $all是个数组。

还可以区分,如果只想获取问号后面的参数:

$all =Request::instance()->get();  

如果只想获取id的参数:

$all =Request::instance()->route();

如果只想获取post传的参数:

$all =Request::instance()->post();

以上附加的三个方法也可以指定具体的参数名,比如:

$all =Request::instance()->get(‘name’); 

第三种方法:

助手函数:

$all =input(‘param.’);    获取所有的参数

也有灵活的写法:

$all =input(‘get.name’);  

$all =input(‘post.age);

 等等。

你可能感兴趣的:(thinkphp5,Thinkphp5获取参数方法)