Fork me on GitHub

laravel5.5路由使用name的好处

使用name的好处

辅助函数 route 可以用于为指定路由生成 URL。命名路由生成的 URL 不与路由上定义的 URL 相耦合。因此,就算路由的 URL 有任何更改,都不需要对 route 函数调用进行任何更改。例如,假设你的应用程序包含以下路由:

Route::get('/post/{post}', function () {
    //
})->name('post.show');

要生成此路由的 URL,可以像这样使用辅助函数 route:

echo route('post.show', ['post' => 1]);

// http://example.com/post/1

将 Eloquent 模型 作为参数值传给 route 方法,它会自动提取模型的主键来生成 URL。

echo route('post.show', ['post' => $post]);
posted @ 2018-02-09 15:54  archer-wong  阅读(806)  评论(0编辑  收藏  举报