ThinkPHP5.1 閉包路由 直接輸出資料 不需要定義控制器
阿新 • • 發佈:2019-02-06
# /home/myth/www/think/route/route.php
Route::get('hello', function () {
return 'hello,world!';
}); // http://contoso.org/hello 閉包路由
Route::get('hello/:name', function ($name) {
return 'Hello,' . $name;
}); // http://contoso.org/hello/jack 閉包路由定義支援引數傳遞
Route::rule('hi/:name', function (think\Request $request, $name) {
$method = $request->method();
return '[' . $method . '] Hi,' . $name;
}); // http://contoso.org/hi/jack 閉包中使用依賴注入$request
Route::get('print/:name', function (think\Response $response, $name) {
return $response
->data('Print,' . $name)
->code(200)
->contentType('text/plain');
}); // http://contoso.org/print/jack 在路由閉包中指定響應物件輸出
Route::get('hello', function () {
return 'hello,world!';
}); // http://contoso.org/hello 閉包路由
Route::get('hello/:name', function ($name) {
return 'Hello,' . $name;
}); // http://contoso.org/hello/jack 閉包路由定義支援引數傳遞
Route::rule('hi/:name', function (think\Request $request, $name) {
$method = $request->method();
return '[' . $method . '] Hi,' . $name;
}); // http://contoso.org/hi/jack 閉包中使用依賴注入$request
Route::get('print/:name', function (think\Response $response, $name) {
return $response
->data('Print,' . $name)
->code(200)
->contentType('text/plain');
}); // http://contoso.org/print/jack 在路由閉包中指定響應物件輸出