twitter-bootstrap Laravel 4 抛出 MethodNotAllowedHttpException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24941897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 21:11:23  来源:igfitidea点击:

Laravel 4 throwing MethodNotAllowedHttpException

phptwitter-bootstraplaravel-4

提问by Don't Panic

I am making a simple registration form that then submits data to my database. However, the problem that I am running into is that the code that should submit the info to the database is not working.

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

Here's the form that I made using Twitter Bootstrap

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

This is the route that I am passing to the action field in my form:

这是我传递到表单中的操作字段的路径:

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

And this is the create.php file that the route (above) is supposed to load which takes care of the database submission

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

However, Laravel doesn't like this and is throwing this error at me: Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException Open: /Users/brendanbusey/Desktop/php_site/laravelSite/bootstrap/compiled.php

然而,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)

I have double and triple checked to make sure that I am using the names and not the id's from my html form when I'm sending the data via POST and all my names from the columns in my database match the ones in my code. Any help would be greatly appreciated!

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

回答by Don't Panic

You need to have two routes defined, one for GET and one for POST. If you want to use the same adding_to_table url, it should be something like

您需要定义两条路由,一条用于 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
});

Maybe I'm not understanding you correctly, but it looks like you are using View::make()to try to run your db insert code. I believe View::make()is just intended to render blade templates, so I don't think this will work. Instead you could put that type of thing into a controller method, or even directly into the post route closure. To access the submitted values, you should use Input::get('firstname')etc. In the laravel docs hereit explains that

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

You do not need to worry about the HTTP verb used for the request, as input is accessed in the same way for all verbs.

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

回答by lowerends

You need to change the form opening line in your HTML to this:

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

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

In your routes.phpfile, you need to have this:

在你的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"),
        )
    );
});

回答by Marios Fakiolas

Change

改变

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

to retrieve inputs.

检索输入。

回答by Ankit kumar

Just add {{ csrf_field() }}below your form, like this:

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

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

回答by Venkateswarlu Dande

in Create.blade.php

在 Create.blade.php 中

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

in Controller :-

在控制器中:-

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

NOTE : check model with all fields are fillable are not .

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