如何在Laravel中合并两个Request

时间:2015-12-13 20:59:29

标签: php laravel laravel-5 http-post laravel-5.1

在我的Laravel(5.1)项目中,我需要创建一个验证表单的请求。

但是对于这个请求,我想合并两个不同的请求:

第一个请求:

class AdvertisementRequest extends Request {

    public function authorize()
    {

        return true;
    }

    public function rules()
    {
        return [
            'ads_type' => 'required|numeric|in:0,1',
            'category' => 'required|numeric|exists:categories,id',
            'title' => 'required|alpha_num|max:45',
            'description' => 'required|alpha_num|max:2000',
            'price' => 'required|numeric',
        ];
    }

}

第二个请求:

class UserRegisterRequest extends Request {

    public function authorize()
    {

        return true;
    }

    public function rules()
    {
        $rules = [
            'form_type' => 'required|numeric|in:0,1',
            'user_type' => 'required|numeric|in:0,1',
            'phone' => 'required|phone_number',
            'region' => 'required|numeric|exists:regions,id',
            'department' => 'required|numeric|exists:departments,code',
            'postal_code' => 'required|postal_code',
            'city' => 'alpha|max:45',
            'id_city' => 'required|numeric|exists:cities,id',
            'last_name' => 'required|alpha_sp|max:45',
            'first_name' => 'required|alpha_sp|max:45',
            'pseudo' => 'required|alpha|max:45|unique:users',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|alpha_num|min:6|max:45',
        ];

        return $rules;
    }
}

我想创建第三个请求,将其他两个结合起来:

class UserAdvertisementRegisterRequest extends Request {

    public function authorize()
    {

        return true;
    }

    public function rules()
    {
        $rules = [
            'ads_type' => 'required|numeric|in:0,1',
            'category' => 'required|numeric|exists:categories,id',
            'title' => 'required|alpha_num|max:45',
            'description' => 'required|alpha_num|max:2000',
            'price' => 'required|numeric',

            'form_type' => 'required|numeric|in:0,1',
            'user_type' => 'required|numeric|in:0,1',
            'phone' => 'required|phone_number',
            'region' => 'required|numeric|exists:regions,id',
            'department' => 'required|numeric|exists:departments,code',
            'postal_code' => 'required|postal_code',
            'city' => 'alpha|max:45',
            'id_city' => 'required|numeric|exists:cities,id',
            'last_name' => 'required|alpha_sp|max:45',
            'first_name' => 'required|alpha_sp|max:45',
            'pseudo' => 'required|alpha|max:45|unique:users',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|alpha_num|min:6|max:45',
        ];

        return $rules;
    }
}

如果没有重复我的代码,是否有任何解决方案?

抱歉我的英文不好:/。

提前感谢您的回复!

2 个答案:

答案 0 :(得分:7)

最简单的方法是以下列方式在 UserAdvertisementRegisterRequest :: rules 方法中生成验证规则的合并列表:

class UserAdvertisementRegisterRequest extends Request { 
  public function rules()
  {
    return array_merge(
      with(new AdvertisementRequest)->rules(), 
      with(new UserRegisterRequest)->rules()
    );
  }
}

答案 1 :(得分:0)

我试图通过扩展另一个请求并尝试将规则与父/子请求之间的逻辑结合起来来实现合并。我对GET和POST也有不同的规则/格式,所以我的父请求类有一个函数可以根据我的变换器期望的数据传递数据。

但最终将事物解耦更好,我发现可以将多个请求对象注入控制器。每个请求都将包含相同的值,但每个请求都将根据自己的规则验证请求。对于我的变换器,我只传递变换器期望的请求版本,因此它可以调用扩展的get函数并从那里进行转换。

public function store(
    StoreEventRequest          $storeEventRequest, 
    UserListRequest            $userListRequest, 
    UserListRequestTransformer $transformer
) {
    $eventDetails     = $storeEventRequest->all();
    $userListIterator = $transformer->requestToUserList($userListRequest);
    ...
}