Laravel 4抛出MethodNotAllowedHttpException

1 php twitter-bootstrap laravel-4

我正在制作一个简单的注册表单,然后将数据提交到我的数据库.但是,我遇到的问题是应该将信息提交到数据库的代码不起作用.

这是我使用Twitter Bootstrap制作的表单

<!DOCTYPE html>
<html>
<body>
    <div class="modal-body">
        <div class="well">
            <div id="myTabContent" class="tab-content">
                <div class="tab-pane active in" id="login">
                    <form method="POST" action='/adding_to_table' class="form-horizontal">
                        <fieldset>
                            <div id="legend">
                                <legend class="">Create Your Account</legend>
                            </div>
                            <div class="control-group">
                                <!-- Username -->
                                <label class="control-label"  for="firstname">First Name</label>
                                <div class="controls">
                                    <input type="text" id="first_name" name="firstname" placeholder="" class="input-xlarge">
                                </div>
                            </div>

                            <div class="control-group">
                                <!-- Username -->
                                <label class="control-label"  for="lastname">Last Name</label>
                                <div class="controls">
                                    <input type="text" id="last_name" name="lastname" placeholder="" class="input-xlarge">
                                </div>
                            </div>

                            <div class="control-group">
                                <!-- Username -->
                                <label class="control-label"  for="email">E-mail</label>
                                <div class="controls">
                                    <input type="text" id="e_mail" name="email" placeholder="" class="input-xlarge">
                                </div>
                            </div>

                            <div class="control-group">
                                <!-- Username -->
                                <label class="control-label"  for="username">Username</label>
                                <div class="controls">
                                    <input type="text" id="username" name="username" placeholder="" class="input-xlarge">
                                </div>
                            </div>


                            <div class="control-group">
                                <!-- Password-->
                                <label class="control-label" for="password">Password</label>
                                <div class="controls">
                                    <input type="password" id="password" name="password" placeholder="" class="input-xlarge">
                                </div>
                            </div>

                            <div class="control-group">
                                <!-- Button -->
                                <div class="controls">
                                    <button class="btn btn-primary">Submit</button>
                                </div>
                            </div>
                        </fieldset>
                    </form>                
                </div>
            </div>
        </div>
    </div>

<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
</body>
<html>
Run Code Online (Sandbox Code Playgroud)

这是我在表单中传递给action字段的路径:

  Route::get('/adding_to_table', function()
  {
      return View::make('create');
  });
Run Code Online (Sandbox Code Playgroud)

这是应该加载路由(上面)的create.php文件,它负责数据库提交

  DB::table('user_info')->insert(
    array("First_name" => Input::post('firstname'),
          "Last_name" => Input::post("lastname"),
          "E-mail" => Input::post("email"),
          "Username" => Input::post("username"),
          "Password" => Input::post("password"),
        )
    );

echo "Successfully entered user information into data table";
Run Code Online (Sandbox Code Playgroud)

但是,Laravel不喜欢这个并向我抛出这个错误:Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException打开:/Users/brendanbusey/Desktop/php_site/laravelSite/bootstrap/compiled.php

        }))->bind($request);
        } else {
        $this->methodNotAllowed($others);
       }
   }
  protected function methodNotAllowed(array $others)
  {
      throw new MethodNotAllowedHttpException($others);
  }
  protected function check(array $routes, $request, $includingMethod = true)
Run Code Online (Sandbox Code Playgroud)

当我通过POST发送数据并且我的数据库中的所有名称与我的代码中的列匹配时,我进行了双重和三重检查,以确保我使用的是名称,而不是我的html表单中的id.任何帮助将不胜感激!

Don*_*nic 6

您需要定义两个路由,一个用于GET,一个用于POST.如果你想使用相同的adds_to_table网址,它应该是这样的

Route::get('/adding_to_table', function() {
    // code to display your form initially
});
Route::post('/adding_to_table', function() {
    // code to process the form submission
});
Run Code Online (Sandbox Code Playgroud)

也许我没有正确理解你,但看起来你正View::make()试图运行你的数据库插入代码.我相信View::make()只是为了渲染刀片模板,所以我认为这不会起作用.相反,你可以将这种类型的东西放入控制器方法,甚至可以直接放入后期封闭.要访问提交的值,您应该使用Input::get('firstname')等.在这里的laravel文档中,它解释了这一点

您无需担心用于请求的HTTP谓词,因为所有谓词都以相同的方式访问输入.