ThinkPHP5.1 闭包路由 直接输出数据 不需要定义控制器

来源:互联网 发布:网络追逃人员查询系统 编辑:程序博客网 时间:2024/06/11 06:01
# /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    在路由闭包中指定响应对象输出
阅读全文
0 0
原创粉丝点击