英文:
Laravel route difference between {id} vs {tag}
问题
我是 Laravel 新手,如果问题很傻,请原谅。我看过一个文档,他们使用了以下路由:
对于 GET 请求:
Route::get("tags/{id}","TagsController@show");
对于 PUT 请求:
Route::put("tags/{tag}","TagsController@update");
这两者之间有什么区别和好处呢?我理解第一个,但对于 PUT 路由有点困惑。
英文:
I am new in Laravel pardon me if question is silly. I have seen a doc where they used
For get request
Route::get("tags/{id}","TagsController@show");
For put request
Route::put("tags/{tag}","TagsController@update");
What is the difference and benefit between this ? I understood 1st one, confusion on put route.
答案1
得分: 3
以下是翻译好的部分:
没有真正的区别,因为它只是一个参数名称,但如果您在路由中有多个参数,就需要一种区分参数的方式,例如嵌套资源控制器:
Route::get('articles/{article}/comments/{comment}', 'ArticleCommentController@show');
显然,你不能只使用 {id}
作为文章和评论参数的名称。因此,最好使用模型的“slug”版本作为参数名称,即使在你的路由中只有一个参数:
Route::get('articles/{article}', 'ArticleController@show');
你还可以使用路由模型绑定。如果在控制器操作中为参数名称添加了类型提示,Laravel 将尝试在 URL 中查找具有给定类的主键的实例。
在第二个示例代码中的路由,如果你有一个控制器如下...
class ArticleController extends Controller
{
public function show(Article $article)
{
//
}
}
...并请求 /articles/123,那么 Laravel 将尝试查找具有主键 123 的 Article
实例。
路由模型绑定非常好,因为它可以减少控制器中的许多 find
/ findOrFail
方法调用。在大多数情况下,你可以将控制器操作简化为一行代码:
class ArticleController extends Controller
{
public function show(Article $article)
{
return view('article.show', compact('article'));
}
}
英文:
There’s no real difference as it’s just a parameter name, but you’d need some way to differential parameters if you had more than one in a route, i.e. a nested resource controller:
<!-- language-all: lang-php -->
Route::get('articles/{article}/comments/{comment}', 'ArticleCommentController@show');
Obviously you couldn’t use just {id}
for both the article and comment parameters. For this reason, it’s best to use the “slug” version of a model for a parameter name, even if there’s just one in your route:
Route::get('articles/{article}', 'ArticleController@show');
You can also use route model binding. If you add a type-hint to your controller action for the parameter name, Laravel will attempt to look up an instance of the given class with the primary key in the URL.
Given the route in the second code example, if you had a controller that looked like this…
class ArticleController extends Controller
{
public function show(Article $article)
{
//
}
}
…and you requested /articles/123, then Laravel would attempt to look for an Article
instance with the primary key of 123.
Route model binding is great as it removes a lot of find
/ findOrFail
method calls in your controller. In most instances, you can reduce your controller actions to be one-liners:
class ArticleController extends Controller
{
public function show(Article $article)
{
return view('article.show', compact('article'));
}
}
答案2
得分: 2
通常情况下,除非您为路由参数定义了自定义绑定,否则没有实际差异。通常,这些绑定在 RouteServiceProvider
中定义,如文档中的示例所示:
public function boot()
{
parent::boot();
Route::model('tag', App\Tag::class);
}
当您以这种方式绑定 tag
时,您的控制器操作可以通过模型解析来使用该变量:
public function update(Tag $tag) {
// $tag 根据URL中传递的标识符进行解析
}
通常,模型会自动绑定,因此不需要手动进行绑定,但如果您手动执行绑定,可以自定义解析逻辑。
英文:
Generally there's no practical difference unless you define a custom binding for a route parameter. Typically these bindings are defined in RouteServiceProvider
as shown in the example in the docs
public function boot()
{
parent::boot();
Route::model('tag', App\Tag::class);
}
When you bind tag
this way then your controller action can use the variable via model resultion:
public function update(Tag $tag) {
// $tag is resolved based on the identifier passed in the url
}
Usually models are automatically bound so doing it manually doesn't really need to be done however you can customise resolution logic if you do it manually
答案3
得分: 0
普通方式
Route::get("tags/{id}","TagsController@show");
function($id)
{
$tag = Tag::find($id);
dd($tag); // 标签
}
使用路由模型绑定
Route::put("tags/{tag}","TagsController@update");
function(Tag $tag) // 标签模型绑定
{
dd($tag); // 标签
}
参考链接 https://laravel.com/docs/5.8/routing#implicit-binding
英文:
Normal way
Route::get("tags/{id}","TagsController@show");
function($id)
{
$tag = Tag::find($id);
dd($tag); // tag
}
With route model bindings
Route::put("tags/{tag}","TagsController@update");
function(Tag $tag) // Tag model binding
{
dd($tag); // tags
}
ref link https://laravel.com/docs/5.8/routing#implicit-binding
答案4
得分: 0
查看这个:路由模型绑定
使用 id,Laravel 将从路由中获取 id,它将是标签的 id,它是整数。
function show($id) {
$tag = Tag::find($id);
}
使用 tag,Laravel 会自动解析在路由或控制器操作中定义的 Eloquent 模型,其类型提示的变量名与路由段名称匹配。
在 URL 中,您的标签参数是整数,但在控制器操作中 $tag
将是一个模型对象:
function action(Tag $tag) {
$tag->name;
}
所以您不需要在控制器操作中通过 Eloquent 获取 $tag
。您只需指定它来自模型 Tag $tag
。
它会自动完成这个过程。
英文:
Check this out: Route model bindings
Use id, Laravel will get the id from route, and it will be the tag's id, it is integer.
function show($id) {
$tag = Tag::find($id);
}
Use tag, Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name.
In URL, your tag parameter is integer, however in your controller action $tag
will be a model object:
function action(Tag $tag) {
$tag->name;
}
So you don't need to get the $tag
by eloquent in your controller action. You just need to specify it is From model Tag $tag
It will do it automatically.
答案5
得分: 0
这只是一种约定。你可以随意称呼它。通常,{id}
指的是你表格中的id。一个标签,或类似的,一个slug,是一个字符串值。标签可以是视频类别的 'entertainment',而 'my-trip-to-spain' 是视频描述的slug。
你必须选择自己感觉舒适的词。这个值将用于在你的数据库中查找所需的记录,以显示视图中的正确请求。同样,你可以使用 video/view/{id}/{slug}
或任何组合。
只要确保你的URL不要太长。因为如果太长,搜索引擎将不会在搜索结果中良好地显示你的网站。要在数据库中保持明确性和逻辑性(对于访问者)之间找到平衡。
英文:
It's just a convention. You can call it all you want. Usually, and {id} refers to the id in your table. A tag, or similarly, a slug, is a string value. A tag could be 'entertainment' for video categories, while 'my-trip-to-spain' is a slug for the description of a video.
You have to chose the words what you are comfortable with. The value will be used to find in your database what record is needed to show the correct request in the view. Likewise you can use video/view/{id}/{slug}
or any combination thereof.
Just make sure your URLs don't get too long. Because search engines won't show your website nicely in search results if you do. Find the balance between the unambiguous (for your database) and logic (for your visitors).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论