Laravel 通过 API 嵌套 URL 发布

huangapple go评论78阅读模式
英文:

Laravel posting via API nested url

问题

我使用嵌套控制器的API资源,我的URL看起来像:

GET /api/projects
POST /api/projects
...
GET /api/projects/:uuid/employees
POST /api/projects/:uuid/employees

我还使用请求来保存每个端点的数据(也用于验证)。

问题是如何提交数据以保存新的员工(姓名、电子邮件等),并验证并保存URL中的project_id值?

请求规则:

public function rules(): array
{
    return [
        'project_id' => ['required'],
        'name' => ['required', 'max:30'],
        'email' => ...,
    ];
}

我的提交数据只包含nameemail值,我想从URL中获取/设置project_id的值,这是否可能?

英文:

I using API resources with nested controllers, my urls look like:

GET /api/projects
POST /api/projects
...
GET /api/projects/:uuid/employees
POST /api/projects/:uuid/employees

I using also Request to save data per endpoint (for validation too).

The question is how to post data to save new employee (name, email, etc.) and validate and save value for project_id from URL?

Request rules:

public function rules(): array
{
    return [
        'project_id' => ['required'],
        'name' => ['required', 'max:30'],
        'email' => ...,
    ];
}

My posted data has just name and email values and I would like get/set value for project_id from URL, it is possible?

答案1

得分: 1

你可以使用 prepareForValidation

protected function prepareForValidation() {
  return $this->merge([
    'project_id' => request()->route('uuid'), 
  ]);
}
英文:

You can use prepareForValidation

protected function prepareForValidation() {
  return $this->merge([
    'project_id' => request()->route('uuid'), 
  ]);
}

答案2

得分: 1

可以,您可以将project_id合并或添加到现有请求中。

$project_id = $request->route('uuid');
//添加到请求中
$request->request->add(['project_id' => $project_id]);
//或者
$request->merge(['project_id' => $project_id]);
英文:

Yes, you can merge or add project_id to an existing request.

  $project_id = $request->route('uuid');
  //Add to request
  $request->request->add(['project_id' => $project_id]);
  //OR
  $request->merge(['project_id' => $project_id])

答案3

得分: 0

不需要验证路由参数是否必需。没有该参数的请求根本不会到达该端点。相反,如果您想要检查数据库中是否存在项目,可以使用路由模型绑定。

Route::get("/api/projects/{project:uuid}/employees")
Route::post("/api/projects/{project:uuid}/employees")

现在在控制器中,Laravel 将搜索具有该 UUID 的项目并提供给您,或者抛出 404 错误。

class EmployeeContoller
{
  public function store(StoreEmployeeRequest $request, Project $project)
  {
    //
  }
}
英文:

no need to validate that a route parameter is required. a request without that parameter would not be reaching that endpoint at all. Instead if you want to check that the project exists in you db you can use route model bindings.

Route::get("/api/projects/{project:uuid}/employees")
Route::post("/api/projects/{project:uuid}/employees")

now in controller, laravel will search for a project with that uuid and provide it to you or throw a 404.

class EmployeeContoller
{
  public function store(StoreEmployeeRequest $request, Project $project)
  {
    //
  }
}

huangapple
  • 本文由 发表于 2023年8月10日 14:14:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873040.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定