![](/img/trans.png)
[英]What is the purpose of the authorize method in a Request class in Laravel?
[英]What is the purpose of creating a custom request class in laravel 5?
我下載了一個在Laravel 5.0中創建的簡單應用程序。 我在Http \\ Requests下找到了一些文件,例如
HTTP \\請求\\ Request.php
<?php namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
HTTP \\請求\\管理\\ PhotoRequest.php
<?php namespace App\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
class PhotoRequest extends FormRequest {
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'language_id' => 'required|integer',
'photo_album_id' => 'required|integer',
];
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
}
這些課程的目的是什么?它是如何生效的?
您希望擁有瘦控制器以獲得更好的可維護性。 當您擁有大量具有大量驗證規則的表單字段時,您的控制器將變得臃腫。 因此,您需要移動模型中的所有數據邏輯,請求類中的驗證邏輯等。
您可以在此處閱讀有關單一責任原則的更多信
例如, rules()
方法,當您提交表單時,它將檢查字段是否經過驗證。 在控制器中,您可以使用
function postForm(PhotoRequest $request){
// Your Codes. You don't need to validate the data
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.