Thinkphp5获取参数方法

<p>Thinkphp5中路由获取参数</p><p>第一种方法:</p><p>在定义路由的时候,如下写法:</p><p>use think\Route;</p><p>Route::post(‘hello/:id’,sample/test/hello); //路由post方法</p><p>url地址:http://ServerName/hello/123?name=huihui</p><p>上面路由地址传了两个参数:id=123 name=huihui</p><p>第二种方法:</p><p>用Request方法来获取参数,先要引入Request类</p><p>use think\Request;</p><p>Class Test</p><p>{</p><p> Public function hello(){</p><p> $id= Request::instance()-&gt;param(‘id’);</p><p> $name= Request::instance()-&gt;param(‘name’);</p><p>}</p><p>}</p><p>该方法不区分get,post等http请求类型,都是这样获取参数。</p><p>还可以一次性获取所有参数写法:</p><p>$all = Request::instance()-&gt;param(); $all是个数组。</p><p>还可以区分,如果只想获取问号后面的参数:</p><p>$all =Request::instance()-&gt;get();</p><p>如果只想获取id的参数:</p><p>$all =Request::instance()-&gt;route();</p><p>如果只想获取post传的参数:</p><p>$all =Request::instance()-&gt;post();</p><p>以上附加的三个方法也可以指定具体的参数名,比如:</p><p>$all =Request::instance()-&gt;get(‘name’);</p><p>第三种方法:</p><p>助手函数:</p><p>$all =input(‘param.’); 获取所有的参数</p><p>也有灵活的写法:</p><p>$all =input(‘get.name’);</p><p>$all =input(‘post.age);</p><p>等等。</p>
RangeTime:0.007844s
RangeMem:196.71 KB
返回顶部 留言