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');
});

 

你可能感兴趣的:(thinkphp6 路由的学习)