【问题标题】:MethodNotAllowedHttpException in RouteCollection.php in Laravel 5Laravel 5 中 RouteCollection.php 中的 MethodNotAllowedHttpException
【发布时间】:2017-11-07 07:27:24
【问题描述】:

routes.php

Route::get('/',array('uses'=>'student@index'));
Route::get('/view',array('uses'=>'student@view'));
Route::post('/save',array('uses'=>'student@save'));

这是代码,我正在处理表单,当我提交表单时,它显示此错误:

RouteCollection.php 第 201 行中的 MethodNotAllowedHttpException:

student.php

class student extends Controller  {

    public function index()
     {   
         //return 'hello world';

        return \View::make('student.index');
      }
          public function view()
     {
        return \View::make('student.view');
      }

          public function save()
     {
        //return \View::make('student.view');

        $validation= array(
                      'first_name'=>'required',

                      'email'=>'required'

                          );
        $v1=validator::make(Input::all(),$validation);
        if( $v1->fails())
        {
        return Redirect::to('view')->withErrors($v1);
        }
        else
        { $poststudent=Input::all();
          $data = array('first_name'=>$poststudent['first_name'],
                         'last_name'=>$poststudent['last_name'],
                         'email'=>$poststudent['email'],
                         'interested'=>$poststudent['interested'], 
                         'skills'=>$poststudent['skills']);

        $check=0;
        $check=DB::table('students')->insert($data);

        if($check > 0)
        {
        return Redirect::to('/');
        }
        else
        {
        return Redirect::to('/view');
        }

        }



      }

        }

表格是这样的:

<form action="<?=URL::to('/save')?>" methed="POST">
<div class="form-group">
 <label for= "first_name"> FIRST NAME </label>
<input name="FIRST NAME" type="text" value="" class="form-control" id="first       name"/>
</div>

我被困在这里了。

【问题讨论】:

  • 您是否检查过以确保&lt;form&gt; 是使用/save 路由呈现的?你检查过路由文件确实运行了吗?可能是放错地方了。
  • 啊哈! methed 是一个拼写错误 - 应该是 method。此外,您的 &lt;input /&gt; 唯一 id 中有空格,这是无效的。
  • 谢谢兄弟。它的工作...伟大的眼睛...方法拼错了
  • 如果你没有使用 Laravel 的表单构建器,你必须自己包含 CSRF 令牌。请参阅 Laracasts 上的此条目
  • 确保你的控制器中有use Input 外观。将use Illuminate\Support\Facades\Input; 放在控制器的顶部。

标签: php laravel laravel-5


【解决方案1】:

好吧,如果您在 Apache 服务器上,则需要在 httpd.conf 中配置您允许的 HTTP 方法。

将此行添加到您的httpd.conf&lt;Directory XXXX&gt; 标记中:

AllowMethods GET POST OPTIONS

【讨论】:

  • 你能再解释一下吗?
  • 嗯,这是服务器发送给你的 laravel 应用程序的一种错误,所以 Laravel 会引发异常以不被阻塞,
【解决方案2】:
  1. 您需要命名路线:请参阅我的回复:https://stackoverflow.com/a/47147452/5550606
  2. 请将 {{ csrf_field() }} 放在 &lt;form&gt;... 标记之后。否则你会得到一个 TokenMissmatch 异常。

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2015-10-29
    • 1970-01-01
    • 2017-10-12
    • 2017-03-13
    相关资源
    最近更新 更多