2016-03-04 19 views
0

我正在使用Form Request类来验证传递到我的控制器中的数据。Laravel一起使用Policies和FormRequest类

此外,我使用Policies,以确定是否当前用户被允许show/update/destroy等问题的对象。

如果我使用的政策,这是否意味着我可以简单地使用:

public function authorize() 
{ 
    return true; 
} 

我的请求类中?或者我应该做两次检查/以不同的方式写他们?

如果有人可以对此有所了解,那就太好了。

谢谢。

+0

是的,如果您使用的是策略,并且不需要其他检查,则您可以简单地返回true。 –

回答

0

见\照亮\确认\ ValidatesWhenResolvedTrait

<?php 

namespace Illuminate\Validation; 

use Illuminate\Contracts\Validation\ValidationException; 
use Illuminate\Contracts\Validation\UnauthorizedException; 

/** 
* Provides default implementation of ValidatesWhenResolved contract. 
*/ 
trait ValidatesWhenResolvedTrait 
{ 
    /** 
    * Validate the class instance. 
    * 
    * @return void 
    */ 
    public function validate() 
    { 
     $instance = $this->getValidatorInstance(); 

     if (! $this->passesAuthorization()) { 
      $this->failedAuthorization(); 
     } elseif (! $instance->passes()) { 
      $this->failedValidation($instance); 
     } 
    } 

    /** 
    * Get the validator instance for the request. 
    * 
    * @return \Illuminate\Validation\Validator 
    */ 
    protected function getValidatorInstance() 
    { 
     return $this->validator(); 
    } 

    /** 
    * Handle a failed validation attempt. 
    * 
    * @param \Illuminate\Validation\Validator $validator 
    * @return mixed 
    */ 
    protected function failedValidation(Validator $validator) 
    { 
     throw new ValidationException($validator); 
    } 

    /** 
    * Determine if the request passes the authorization check. 
    * 
    * @return bool 
    */ 
    protected function passesAuthorization() 
    { 
     if (method_exists($this, 'authorize')) { 
      return $this->authorize(); 
     } 

     return true; 
    } 

    /** 
    * Handle a failed authorization attempt. 
    * 
    * @return mixed 
    */ 
    protected function failedAuthorization() 
    { 
     throw new UnauthorizedException; 
    } 
} 

和\照亮\基金会\ HTTP \ FormRequest

/** 
* Determine if the request passes the authorization check. 
* 
* @return bool 
*/ 
protected function passesAuthorization() 
{ 
    if (method_exists($this, 'authorize')) { 
     return $this->container->call([$this, 'authorize']); 
    } 

    return false; 
} 

它只检查返回的结果,并决定是否继续当请求解决。它不通过策略或任何中间件或某物。这种奇怪。