thinkphp6 路由的学习

1、路由依赖注入

使用Request时应该先导入Request:

use think\Request;

Route::rule('hello/:name', function (Request $request, $name) {
    $method = $request->method();
    return '[' . $method . '] Hello,' . $name;
});

或者是直接使用帮助函数Request()

Route::rule('hello/:name', function ($name) {
    $method = Request()->method();
    return '[' . $method . '] Hello,' . $name;
});

 2、路由响应到对象

Route::get('hello/:name', response()
    ->data('Hello,'.$name)
    ->code(200)
    ->contentType('text/plain'));

 上述代码运行后,报错“name未被定义”,可将其改为:

Route::get('abc/:name', function($name){
    return Response()->data('hello, '.$name)
    ->code(200)
    ->contentType('text/plain');
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值