【问题标题】:Laravel 4 throwing MethodNotAllowedHttpExceptionLaravel 4 抛出 MethodNotAllowedHttpException
【发布时间】:2014-09-16 11:48:03
【问题描述】:

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

这是我使用 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>

这是我传递给表单中操作字段的路线:

  Route::get('/adding_to_table', function()
  {
      return View::make('create');
  });

这是路由(上面)应该加载的 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";

但是,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)

当我通过 POST 发送数据并且我数据库中列中的所有名称与我的代码。任何帮助将不胜感激!

【问题讨论】:

    标签: php twitter-bootstrap laravel-4


    【解决方案1】:

    在 Create.blade.php 中

    {!! Form::open(array('route' => 'ControllerName.store','method' => 'POST','files' => true)) !!}
    

    在控制器中:-

    public function store(Request $request)
        {   
            $this->validate($request, [
                'first_name' =>'required',
                'last_name' => 'required',
                'e_mail' =>'required',
                'username' => 'required',
                'password' =>'required',
            ]);
    ModelName::create($request->all());
    return route->(ControllerName.index);
    

    注意:所有字段都可填写的检查模型不是。

    【讨论】:

      【解决方案2】:

      只需在表单下方添加{{ csrf_field() }},如下所示:

      <form method="POST" action='/adding_to_table' class="form-horizontal">
          {{ csrf_field() }}
      

      【讨论】:

        【解决方案3】:

        改变

        Input::post() to Input::get()
        

        检索输入。

        【讨论】:

          【解决方案4】:

          您需要定义两条路由,一条用于 GET,一条用于 POST。如果你想使用相同的adding_to_table url,它应该是这样的

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

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

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

          【讨论】:

          • 我试过了,但是 laravel 然后抛出了这个错误:调用未定义的方法 Illuminate\Http\Request::post()
          【解决方案5】:

          您需要将 HTML 中的表单开头行更改为:

          <form method="POST" action='adding_to_table' class="form-horizontal">
          

          在你的routes.php 文件中,你需要有这个:

          Route::get('adding_to_table', function()
          {
              return View::make('create');
          });
          
          Route::post('adding_to_table', function()
          {
              DB::table('user_info')->insert(
                  array("First_name" => Input::get('firstname'),
                        "Last_name" => Input::get("lastname"),
                        "E-mail" => Input::get("email"),
                        "Username" => Input::get("username"),
                        "Password" => Input::get("password"),
                  )
              );
          });
          

          【讨论】:

          • post 路由是否替换了我在 create.php 中为将数据提交到数据库而编写的代码(参见问题)?
          • 是的。 get 路由用于显示在views/create.blade.php 中定义的create 视图。 post 路由用于处理表单发布。
          猜你喜欢
          • 2013-11-14
          • 2014-07-30
          • 2016-04-23
          • 2013-07-04
          • 2013-09-18
          • 1970-01-01
          • 1970-01-01
          • 2019-05-15
          • 2019-03-12
          相关资源
          最近更新 更多