Phil
Phil

Reputation: 319

MethodNotAllowedHttpException generated using return back()->withInput() in Laravel 5.4

I've searched the forums and have seen many similar issues but none that seem to address my concern. I believe this is different because:

  1. Form validation is not being used at this point
  2. The form method does not seem to be related (just 1 post action)
  3. The routes are not wrapped in web middleware

Here's what the application is supposed to be doing:

  1. A user (with or without Authentication) views a public page with form (display_event)
  2. The user selects a specific ticket for ordering and is directed to a 2nd form (register_step1)
  3. The user then fills out demographic info for as many tickets as are being ordered
  4. The processing step, if the email address used is of a valid user (in DB) should return to the form in step 2 & 3, populate the fields and flash a message. Otherwise it would perform the save() actions required. (register_step2)

The relevant routes from web.php are here:

    Route::get('/events/{event}', 'EventController@show')->name('display_event');
    Route::post('/register/{event}', 'RegistrationController@showRegForm')->name('register_step1');
    Route::post('/register/{event}/create', 'RegistrationController@store')->name('register_step2');

The relevant portions of the RegistrationController.php are here:

public function showRegForm (Request $request, $id) {
    // Registering for an event from /event/{id}
    $ticket        = Ticket::find(request()->input('ticketID'));
    $quantity      = request()->input('quantity');
    $discount_code = request()->input('discount_code');
    $event         = Event::find($ticket->eventID);
    return view('v1.public_pages.register', compact('ticket', 'event', 'quantity', 'discount_code'));
}

And:

public function store (Request $request) {

    $event = Event::find(request()->input('eventID'));
    if(Auth::check()) {
        $this->currentPerson = Person::find(auth()->user()->id);
    }

    // set up a bunch of easy-reference variables from request()->input()

    $email = Email::where('emailADDR', $checkEmail)->first();

    if(!Auth::check() && $email === null) {
        // Not logged in and email is not in database; must create
        $person               = new Person;
        // add person demographics from form

    } elseif(!Auth::check() && $email !== null) {
        // Not logged in and email is in the database;
        // Should force a login -- return to form with input saved.

        flash("You have an account that we've created for you. 
               Please attempt to login and we'll send you a password to your email address.", 'warning');

        return back()->withInput();

    } elseif(Auth::check() && ($email->personID == $this->currentPerson->personID)) {
        // the email entered belongs to the person logged in; ergo in DB
        $person         = $this->currentPerson;
        // add person demographics from form

    } elseif(Auth::check() && ($email->personID != $this->currentPerson->personID)) {
        // someone logged in is registering for someone else in the DB
        $person         = Person::find($email->personID);
        // add person demographics from form

    } else {
        // someone logged in is registering for someone else NOT in the DB
        $person               = new Person;
        // add person demographics from form
    }

    // do more stuff...
    $reg  = new Registration;  (set up a registration record)
}

Upvotes: 0

Views: 267

Answers (1)

Phil
Phil

Reputation: 319

I took the advice indicated in @apokryfos's comment and changed the form parsing-then-display script from a POST to a get.

redirect()->back() is, apparently, always a method=get and that was the cause of the MethodNotAllowedHttpException. In my ~2 weeks using Laravel, I hadn't yet come across that fact.

Upvotes: 2

Related Questions