수색…


소개

사용자 정의 요청 (또는 양식 요청)은 제어기 메소드를 시작하기 전에 요청을 인증 하고 유효성을 검증 하려는 경우에 유용합니다.

각각의 행동이 서로 다른 일련의 검증 (또는 인증) 규칙을 가지고있는 동안 레코드를 생성 하고 업데이트 하는 두 가지 실용적인 용도를 생각할 수 있습니다.

양식 요청을 사용하는 것은 간단합니다. 메소드에서 요청 클래스를 유형 힌트해야합니다.

통사론

  • PHP 장인 확인 : name_of_request 요청

비고

요청은 컨트롤러에서 검증을 분리 할 때 유용합니다. 또한 요청이 승인되었는지 확인할 수 있습니다.

요청 만들기

php artisan make:request StoreUserRequest

php artisan make:request UpdateUserRequest

참고 : FormRequests는 app/Http/Requests/ 폴더에 저장되므로 StoreUser 또는 UpdateUser ( 요청 부록 없음)와 같은 이름을 사용할 수도 있습니다.

양식 요청 사용

사용자 예제로 계속 진행할 수 있습니다 (스토어 메소드 및 업데이트 메소드가있는 컨트롤러가있을 수 있음). FormRequests를 사용하려면 특정 요청에 대해 유형 힌트를 사용하십시오.

...

public function store(App\Http\Requests\StoreRequest $request, App\User $user) { 
    //by type-hinting the request class, Laravel "runs" StoreRequest 
    //before actual method store is hit

    //logic that handles storing new user 
    //(both email and password has to be in $fillable property of User model
    $user->create($request->only(['email', 'password']));
    return redirect()->back();
}

...

public function update(App\Http\Requests\UpdateRequest $request, App\User $users, $id) { 
    //by type-hinting the request class, Laravel "runs" UpdateRequest 
    //before actual method update is hit

    //logic that handles updating a user 
    //(both email and password has to be in $fillable property of User model
    $user = $users->findOrFail($id);
    $user->update($request->only(['password']));
    return redirect()->back();
}

유효성 검사 후 리디렉션 처리

때로는 양식을 제출 한 후 사용자가 리디렉션되는 위치를 결정하기 위해 일부 로그인을 원할 수 있습니다. 양식 요청은 다양한 방법을 제공합니다.

기본적으로 Request $redirect , $redirectRoute$redirectAction 선언 된 3 개의 변수가 있습니다.

이 세 가지 변수 위에 getRedirectUrl() 리디렉션 할 수 있습니다.

할 수있는 일을 설명하는 샘플 요청이 아래에 나와 있습니다.

<?php namespace App;

use Illuminate\Foundation\Http\FormRequest as Request;

class SampleRequest extends Request {

    // Redirect to the given url
    public $redirect;

    // Redirect to a given route
    public $redirectRoute;

    // Redirect to a given action
    public $redirectAction;


    /**
     * Get the URL to redirect to on a validation error.
     *
     * @return string
     */
    protected function getRedirectUrl()
    {

        // If no path is given for `url()` it will return a new instance of `Illuminate\Routing\UrlGenerator`

        // If your form is down the page for example you can redirect to a hash
        return url()->previous() . '#contact';

        //`url()` provides several methods you can chain such as

        // Get the current URL
        return url()->current();

        // Get the full URL of the current request
        return url()->full();

        // Go back
        return url()->previous();

        // Or just redirect back
        return redirect()->back();
    }


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [];
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow