mardi 20 décembre 2016

Can I refactor out an "unused" $request variable in a controller?

Let's say I'm building a small application, where a small part of it is responsible for sending an email when a contact form is submitted. I only want to do that if the contact form passes some simple validation.

In the following controller the ContactRequest $request parameter is unused inside the method, although Laravel used the type-hinting to automatically apply the ContactRequest logic to the request.

Is it possible to achieve the same thing without leaving an unused variable in the controller method?


// Route
Route::post( 'contact', 'PageController@submitContactForm' );


// PageController
public function submitContactForm( ContactRequest $request ) {
    sendContactFormEmail();

    return redirect()->back();
}


// ContactRequest
public function authorize() {
    return hasNotSubmittedContactFormRecently();
}

public function rules() {
    return [ 'message' => 'required' ];
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire