HTTP 路由

更新时间: 2019-08-01 16:47

基本路由

你可以在 routes/web.php 文件中定义应用程序的全部路由。最基本的 Lumen 路由仅接受 URI 和一个 Closure

$router->get('foo', function () {
    return 'Hello World';
});

$router->post('foo', function () {
    //
});

可供使用的路由方法

我们可以注册路由来响应任何方法的 HTTP 请求:

$router->get($uri, $callback);
$router->post($uri, $callback);
$router->put($uri, $callback);
$router->patch($uri, $callback);
$router->delete($uri, $callback);
$router->options($uri, $callback);

路由参数

必填参数

当然,有时需要在路由中捕获一些 URL 片段。例如,从 URL 中捕获用户的 ID,可以通过定义路由参数来执行此操作:

$router->get('user/{id}', function ($id) {
    return 'User '.$id;
});

也可以根据需要在路由中定义多个参数:

$router->get('posts/{postId}/comments/{commentId}', function ($postId, $commentId) {
    //
});

路由的参数都会被放在「大括号」内。当运行路由时,参数会传递到 Closure 里面。

注意: 路由参数不能包含 - 字符。请用下划线 (_) 代替。

可选参数

您可以通过将部分路由 URI 包含在 [...] 中来定义可选的路由参数。那么像 /foo[bar] 将会匹配到 /foo/foobar。可选参数仅支持放在 URI 的末尾。换句话说,你不能在路由定义的中间位置放置可选参数:

$router->get('user[/{name}]', function ($name = null) {
    return $name;
});

正则表达式约束

你可以通过在路由定义中使用正则表达式来约束路由参数的格式:

$router->get('user/{name:[A-Za-z]+}', function ($name) {
    //
});

命名路由

命名路由可以方便的为特定路由生成 URL 或进行重定向。你可以使用 as 数组键指定名称到路由上:

$router->get('profile', ['as' => 'profile', function () {
    //
}]);

你还可以指定控制器行为的路由名称:

$router->get('profile', [
    'as' => 'profile', 'uses' => 'UserController@ showProfile'
]);

生成指定路由的 URL

为路由指定了名称后,就可以使用全局辅助函数 route 来生成链接或者重定向到该路由:

// Generating URLs...
$url = route('profile');

// Generating Redirects...
return redirect()->route('profile');

如果是有定义参数的命名路由,可以把参数作为 route 函数的第二个参数传入,指定的参数将会自动插入到 URL 中对应的位置:

$router->get('user/{id}/profile', ['as' => 'profile', function ($id) {
    //
}]);

$url = route('profile', ['id' => 1]);

路由组

路由群组允许你共用路由属性,例如:中间件、命名空间,你可以利用路由组统一为多个路由设置共同属性,而不需在每个路由上都设置一次。共用属性被指定为数组格式,当作 $router->group 方法的第一个参数。

为了了解更多路由群组的相关内容,我们可通过几个常用样例来熟悉这些特性。

中间件

要给路由组中所有的路由分配中间件,你可以在 group 属性数组中使 middleware 字段。中间件会依照它们在数组中列出的顺序来运行:

$router->group(['middleware' => 'auth'], function () use ($router) {
    $router->get('/', function () {
        // 使用 Auth 中间件
    });

    $router->get('user/profile', function () {
        // 使用 Auth 中间件
    });
});

命名空间

另一个常见的例子是,指定相同的 PHP 命名空间给控制器群组。可以使用 namespace 参数来指定群组内所有控制器的命名空间:

$router->group(['namespace' => 'Admin'], function() use ($router)
{
    // 使用 "App\Http\Controllers\Admin" 命名空间...

    $router->group(['namespace' => 'User'], function() use ($router) {
        // 使用 "App\Http\Controllers\Admin\User" 命名空间...
    });
});

路由前缀

通过路由群组数组属性中的 prefix,在路由群组内为每个路由指定的 URI 加上前缀。例如,你可能想要在路由群组中将所有的路由 URI 加上前缀 admin

$router->group(['prefix' => 'admin'], function () use ($router) {
    $router->get('users', function ()    {
        // 匹配 The "/admin/users" URL
    });
});

你也可以使用 prefix 参数去指定路由群组中共用的参数:

$router->group(['prefix' => 'accounts/{accountId}'], function () use ($router) {
    $router->get('detail', function ($accountId)    {
        // 匹配 The "/accounts/{accountId}/detail" URL
    });
});

查看更多 Laravel 中文文档 信息

High Performance Python

High Performance Python

Micha Gorelick、Ian Ozsvald / O'Reilly Media / 2014-9-10 / USD 39.99

If you're an experienced Python programmer, High Performance Python will guide you through the various routes of code optimization. You'll learn how to use smarter algorithms and leverage peripheral t......一起来看看 《High Performance Python》 这本书的介绍吧!

Markdown 在线编辑器

Markdown 在线编辑器

Markdown 在线编辑器

HEX CMYK 转换工具

HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具

HEX HSV 转换工具

HEX HSV 互换工具