使用Laravel 5.4中的return back() - > withInput()生成MethodNotAllowedHttpException

时间:2017-03-02 06:10:02

标签: php forms

我已经搜索了论坛并看到了很多类似的问题,但似乎没有一个问题可以解决我的问题。我相信这是不同的,因为:

  1. 此时未使用表单验证
  2. 表单方法似乎没有关系(只有1个帖子后)
  3. 路由未包含在Web中间件
  4. 这是应用程序应该做的事情:

    1. 用户(带或不带身份验证)查看带有表单(display_event)
    2. 的公共页面
    3. 用户选择特定故障单进行排序,并定向到第二个表单(register_step1)
    4. 然后,用户会根据订购的票数填写人口统计信息
    5. 处理步骤,如果使用的电子邮件地址是有效用户(在DB中),则应返回步骤2中的表格& 3,填充字段并闪烁消息。否则它将执行所需的save()操作。 (register_step2)
    6. web.php的相关路线如下:

          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');
      

      RegistrationController.php的相关部分在这里:

      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'));
      }
      

      并且:

      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)
      }
      

1 个答案:

答案 0 :(得分:2)

我接受了@ apokryfos评论中指出的建议,并将表单解析后显示脚本从POST更改为get。

redirect() - > back()显然总是一个method = get,这是MethodNotAllowedHttpException的原因。在使用Laravel的~2周内,我还没有遇到过这个事实。