【发布时间】:2019-10-20 18:28:43
【问题描述】:
我正在 Laravel 中构建一个具有资源控制器的自定义包。对于此示例,资源是 Organization 在此控制器中定义了基本的 index、show、store 等操作。
这是我在控制器中的store 方法:
/**
* Store a newly created Organization in storage.
*
* @param OrganizationStoreRequest $request
* @return JsonResponse
*/
public function store($request): JsonResponse
{
$newOrganization = new Organization($request->all());
if ($newOrganization->save()) {
return response()->json($newOrganization, 201);
}
return response()->json([
'message' => trans('organilations::organisation.store.error')
], 500);
}
我的OrganzationStoreRequest 现在很基础:
class OrganizationStoreRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => 'required'
];
}
}
当在 Laravel 应用程序中使用包时,可以在 API 上调用 store 方法。我的问题是我想让我的包的用户能够用自己的请求覆盖我的OrganizationStoreRequest,因为他们可能必须使用不同的授权或验证方法。
我尝试构建一个中间件并将我自己的实例绑定到OrganizationStoreRequests,但我没有得到我想要的结果。
包的用户是否可以覆盖我的包控制器中的OrganizationStoreRequets?
【问题讨论】:
-
有点难看,但是配置文件标识他们想要使用的 Request::class 然后自定义绑定laravel.com/docs/5.8/…
标签: php laravel dependency-injection package ioc-container