简体   繁体   English

是否可以覆盖Laravel包中的注入请求?

[英]Is it possible to override injected Request from a Laravel Package?

I'm building a custom package in Laravel that has a resource Controller. 我正在Laravel中构建一个具有资源控制器的自定义包。 For this example the resource is an Organization In this Controller the basic index , show , store etc actions are defined. 对于此示例,资源是Organization在此Controller中,定义了基本indexshowstore等操作。

This is my store method in the controller: 这是我在控制器中的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);
    }

My OrganzationStoreRequest is pretty basic for now: 我的OrganzationStoreRequest现在非常基本:

class OrganizationStoreRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        return [
            'name' => 'required'
        ];
    }
}

The store method can be called on a API When the package is being used in a Laravel application. 可以在API上调用store方法在Laravel应用程序中使用该包时。 My problem here is that I want to give the users of my package the ability to override my OrganizationStoreRequest with there own requests, as they might have to use different authorize or validation methods. 我的问题是我想让我的包的用户能够用自己的请求覆盖我的OrganizationStoreRequest ,因为他们可能必须使用不同的授权或验证方法。

I've tried building a middleware and Binding my own instance to the OrganizationStoreRequests but I'm not getting the results I want. 我已经尝试构建一个中间件并将我自己的实例绑定到OrganizationStoreRequests但是我没有得到我想要的结果。

Is there anyway for the user of the package to be able to override the OrganizationStoreRequets in the controller of my package ? 无论如何,包的用户是否能够覆盖我的包的控制器中的OrganizationStoreRequets

With the help of fellow developers on Larachat we've come to the following (easy) solution. 在Larachat的开发人员的帮助下,我们得到了以下(简单)解决方案。

When creating an own request we can bind an instance of it to the IoC Container. 在创建自己的请求时,我们可以将它的实例绑定到IoC Container。 For example, creating the OrganizationOverrideRule and extending the original: 例如,创建OrganizationOverrideRule并扩展原始内容:

class OrganizationOverrideRules extends OrganizationStoreRequest
{    
    public function rules(): array
    {
        return [
            'name' => 'required',
            'website' => 'required',
            'tradename' => 'required'
        ];
    }
}

Then in the AppServiceProvider you can bind the new instance like so: 然后在AppServiceProvider您可以像这样绑定新实例:

App::bind(
    OrganizationStoreRequest::class,
    OrganizationOverrideRules::class
);

Then the OrganizationOverrideRules will be used for validation and authorization. 然后, OrganizationOverrideRules将用于验证和授权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM