您的位置:首页 > 编程语言 > PHP开发

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

2017-12-02 12:47 495 查看
# /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    在路由闭包中指定响应对象输出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐