解释它的一种方法是引用您所指的 HTTP 动词 GET。
对于返回 id 为 1 的帖子的 GET 请求,您将有两种选择:
/post/{id}
使用此方法(一种宁静的约定),您需要将变量作为参数传递给控制器操作以访问它。
public function view($id)
{
$post = Post::find($id);
...
}
/post?id=1
使用此方法,您可以在 url 中将 id 作为查询字符串参数传递。然后在控制器中访问请求中的值。
public function view(Request $request)
{
$post = Post::find($request->input('id'));
...
}
现在假设您要创建一个新的Post,它通常是对/post 端点的HTTP 动词POST 请求,您可以在该端点使用Request 访问表单的有效负载。
public function create(Request $request)
{
$post = Post::create($request->only('description'));
}
现在假设您要更新当前的 Post,这通常是一个 HTTP 动词 PUT 请求到 /post/{id} 端点,您将在其中通过参数获取模型,然后使用请求更新数据。
public funciton update(Request $request, $id)
{
$post = Post::find($id);
$post->update($request->only('description'));
}
因此,有时您也会将控制器参数与请求结合使用。但通常控制器参数用于您需要访问的路由中的单个项目。