Implementation of closure routing
Closure definition
We can use closures to define some routes with special needs without executing the controller's operation method, for example :
Route::get('hello', function () { return 'hello,world!'; });
Parameter passing
Parameter passing is supported when the closure is defined, for example:
Route::get('hello/:name', function ($name) { return 'Hello,' . $name; });
The name of the dynamic variable defined in the rule routing is Parameter names in closure functions, in no particular order.
Therefore, if the URL address we visit is:
http://serverName/hello/thinkphp
, the result output by the browser is:
Hello,thinkphp
Dependency Injection
You can use dependency injection in closures, for example:
Route::rule('hello/:name', function (Request $request, $name) { $method = $request->method(); return '[' . $method . '] Hello,' . $name; });