[Day 9]Request

首先利用function index来测试Request功能,在终端机上打php artisan route:list,找到取得index的路径。
https://ithelp.ithome.com.tw/upload/images/20210924/20142046QwgotgpMjU.png
在你的Controller.php的档案中找到function index

public function index(Request $request)
    {
        dump($request);
    }

使用dump来看看结果
https://ithelp.ithome.com.tw/upload/images/20210924/201420460MtyfuL9r6.png
可以看到$request是一个物件,里面有很多的属性,

  • request、query里面会包含使用者传给server的资料
  • server里面会存放本身server的一些参数
  • files如果是用上传档案的方式就会将档案放进files里
  • cookie用来存放暂存资料
  • headers会显示request资料的属性

如何在程序中使用$request的资讯

  • 函式all():可以获得使用者传递给server的资料
public function index(Request $request)
    {
        dump($request->all());
    }

https://ithelp.ithome.com.tw/upload/images/20210924/20142046F9lrl720EN.png

  • 函式query():取得网址列上的参数
    使用HTTP post资料不会在网址列上显示,就可以使用query()
public function index(Request $request)
    {
        dump($request->query());
    }

https://ithelp.ithome.com.tw/upload/images/20210924/20142046pwimgak38s.png

  • 函式path():可以获得网址的路径
public function index(Request $request)
    {
        dump($request->path());
    }

https://ithelp.ithome.com.tw/upload/images/20210924/20142046E2CgUrxlKf.png

  • 函式input(' '):可以获得特定资讯
public function index(Request $request)
    {
        dump($request->input('fruit'));
    }

有存在的参数
https://ithelp.ithome.com.tw/upload/images/20210924/20142046mVMIXl2BTw.png
不存在的参数:null
https://ithelp.ithome.com.tw/upload/images/20210924/20142046YCMAzu8Oc1.png
添加不存在参数:在後面直接添加资讯即可

public function index(Request $request)
    {
        dump($request->input('color','red'));
    }

https://ithelp.ithome.com.tw/upload/images/20210924/20142046Cgyn11jj8J.png


<<:  [Day09] 选择困难再度发作之主题挑选

>>:  Day 9 - 利用路由协议来组 SD-WAN 网路

[Day 25] Edge Impulse + BLE Sense实现手势动作辨识(下)

=== 书接上回 [Day 24] Edge Impulse + BLE Sense实现手势动作辨识...

[Day 26] review 一下我们的程序,谈谈 DSL 和 DAO 的差异

前面讲了很多 Kotlin Exposed 框架使用的方式。 今天来讲点观念性的东西,谈谈 Expo...

Day2-不只都是英文名称 docker 和 k8s的关系

在前一章提到k8s是一种容器编排平台,用於管理容器化的应用程序。 而既然提到容器,那自然得提一下容器...

Day12回圈(Ⅱ)

像是我们很常碰到的成绩也可以用if/else回圈来操作,假设90分以上是A,80分以上是B,…到60...

D-10 AoP ? autofac ? DynamicProxy

Service怎麽做到像MiddleWare一样的东西 在网页程序中可以透过MiddleWare来做...