🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## **闭包定义** 我们可以使用闭包的方式定义一些特殊需求的路由,而不需要执行控制器的操作方法了,例如: ~~~ Route::get('hello', function () { return 'hello,world!'; }); ~~~ ### **参数传递** 闭包定义的时候支持参数传递,例如: ~~~ Route::get('hello/:name', function ($name) { return 'Hello,' . $name; }); ~~~ 规则路由中定义的动态变量的名称 就是闭包函数中的参数名称,不分次序。 因此,如果我们访问的URL地址是: ~~~ http://serverName/hello/thinkphp ~~~ 则浏览器输出的结果是: ~~~ Hello,thinkphp ~~~ ## **依赖注入** 可以在闭包中使用依赖注入,例如: ~~~ Route::rule('hello/:name', function (Request $request, $name) { $method = $request->method(); return '[' . $method . '] Hello,' . $name; }); ~~~