Ceci est une ancienne révision du document !
php artisan make:request StoreBlogPost
On peut coder directement une autorisation et rediriger ou on veut.
/** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return \Auth::user()->has_role('ADMIN'); } /** * Get the response for a forbidden operation. */ public function forbiddenResponse() { return redirect('/')->with('flash_error',"Seul ADMIN peut..."); }
Pour personnaliser le format du retour d’erreur
use Illuminate\Contracts\Validation\Validator; // protected function formatErrors(Validator $validator) { return $validator->errors()->all(); }
Pour forcer la réponse en json si on a été authentifié via un token
public function response(array $errors) { if (Auth::guard('api')->check()) { return response()->json($errors, 422); } return $this->redirector->to($this->getRedirectUrl()) ->withInput($this->except($this->dontFlash)) ->withErrors($errors, $this->errorBag); }
Des rules plus complexe
use Illuminate\Validation\Rule; // public function rules() { $list_ids=List::whereIn('user_id',Auth::id())->pluck('id')->toArray(); return [ "p.*" => [ "required", Rule::in( $list_ids ), ], ]; }