【问题标题】:Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 201:RouteCollection.php 第 201 行中的 Laravel 5 MethodNotAllowedHttpException:
【发布时间】:2015-09-22 14:10:33
【问题描述】:

我的项目中有许多 php 文件:

admin.blade.php:此文件包含管理表单。

调用时显示如下错误:

RouteCollection.php 第 201 行中的 MethodNotAllowedHttpException

<h2>Please Log In To Manage</h2>
<form id="form1" name="form1" method="post" action="<?=URL::to('/admin')?>">
   <input type="hidden" name="_token" value="{{ csrf_token() }}">
   User Name:<br />
   <input name="username" type="text" id="username" size="40" />
   <br /><br />
   Password:<br />
   <input name="password" type="password" id="password" size="40" />
   <br />
   <br />
   <br />
   <input type="submit" name="button" id="button" value="Log In" />
</form>

route.php 中,进行了此调用:

Route::get('/admin',array('uses'=>'student@admin'));

这是student.php中的函数

public function admin()
{
    return View::make('student.admin');
    $validator = Validator::make($data = Input::all() , User::rules());
    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }
    else
    {
        $check = 0;
        $check = DB::table('admin')->get();
        $username = Input::get('username');
        $password = Input::get('password');
        if (Auth::attempt(['username' => $username, 'password' => $password]))
        {
            return Redirect::intended('/');
        }
        return Redirect::back()->withInput()->withErrors('That username/password combo does not exist.');
    }
}

我不太了解创建管理区域,我只是想创建它。

【问题讨论】:

  • 您在表单中使用post 方法,但在路由中使用get 方法!
  • 我应该更改表格吗??
  • 改变你的路线,看看会发生什么;)
  • 把你的路由改成 post , Route::post('/admin',array('uses'=>'student@admin'));
  • 当我同时发布到路由和表单时,它显示相同的错误,如果我同时到达,那么我在提交后一次又一次地来到同一页面。

标签: php laravel laravel-5 laravel-routing


【解决方案1】:

我就是这样做的。

Routes.php

Route::get('/admin', 'UsersController@getAdminLogin');
Route::get('/admin/dashboard', 'UsersController@dashboard');
Route::post('/admin', 'UsersController@postAdminLogin');

admin_login.blade.php

{!! Form::open(['url' => '/admin']) !!}
    <div class="form-group">
        {!! Form::label('email', 'Email Id:') !!}
        {!! Form::text('email', null, ['class' => 'form-control input-sm']) !!}
    </div>
    <div class="form-group">
        {!! Form::label('password', 'Password') !!}
        {!! Form::password('password', ['class' => 'form-control input-sm']) !!}
    </div>
    <div class="form-group">
        {!! Form::submit('Login', ['class' => 'btn btn-primary btn-block']) !!}
    </div>
{!! Form::close() !!}

dashboard.blade.php

<h4 class="text-center">
    Welcome {{ Auth::user()->full_name }}
</h4>

UsersController.php

/**
 * Display the admin login form if not logged in,
 * else redirect him/her to the admin dashboard.
 *
 */
public function getAdminLogin()
{
    if(Auth::check() && Auth::user()->role === 'admin')
    {
        return redirect('/admin/dashboard');
    }
    return view('admin_login');
}

/**
 * Process the login form submitted, check for the
 * admin credentials in the users table. If match found,
 * redirect him/her to the admin dashboard, else, display
 * the error message.
 *
 */
public function postAdminLogin(Request $request)
{
    $this->validate($request, [
        'email'    => 'required|email|exists:users,email,role,admin',
        'password' => 'required'
    ]);

    $credentials = $request->only( 'email', 'password' );

    if(Auth::attempt($credentials))
    {
        return redirect('/admin/dashboard');
    }
    else
    {
        // Your logic of invalid credentials.
        return 'Invalid Credentials';
    }
}

/**
 * Display the dashboard to the admin if logged in, else,
 * redirect him/her to the admin login form.
 *
 */
public function dashboard()
{
    if(Auth::check() && Auth::user()->role === 'admin')
    {
        return view('admin.dashboard');
    }
    return redirect('/admin');
}

您的代码:

routes.php,你只有1条路线,即,

Route::get('/admin',array('uses'=>'student@admin'));

并且没有声明post 方法,因此,MethodNotAllowedHttpException

此外,在您的控制器中,您首先返回视图,然后处理根本不起作用的表单。您首先需要处理表单,然后返回视图。

public function admin(){
    // Won't work as you are already returning the view
    // before processing the admin form.
    return \View::make(students.admin);
    // ...
}

正如@Sulthan 所建议的,您应该使用Form Facade。您可以在Laracasts 上查看this video,了解Form Facade 是什么以及如何使用它。

【讨论】:

  • 感谢您的回答。不错的解决方案
【解决方案2】:

您在表单中使用 post 方法,但在路由中使用 get 方法。

所以,在你的路由中将方法更改为post

注意:

我建议您使用 Laravel 的默认表单打开方式,如下所示,这始终是最佳做法

{!! Form::open(array('url' => 'foo/bar')) !!}

{!! Form::close() !!}

提示:

Read more on here 并尝试通过比较方法和路由来调试此类事情。

默认情况下,表单外观不包含在 laravel 5 中。你应该安装它

composer require "illuminate/html":"5.0.*"

并在 app.php 中更新。

我已经写了一个blog,简要介绍了这个安装。

【讨论】:

  • laravel 5 默认不包含表单外观,需要安装 laravelcollective 包
  • @NehalHasnayeen 感谢提醒我会在答案中更新它:)
  • @deepsingh 你收到return Input::all();了吗?
  • RouteCollection.php 第 201 行中的 MethodNotAllowedHttpException:通过发布到路由并使用表单 facde 。它再次显示这个
  • 你确定你在正确的形式和路线上,还尝试将方法更改为get(只是盲目的方式)
【解决方案3】:

在路由中 web.php 你的代码是

Route::get('/admin',array('uses'=>'student@admin')); 

这是错误的。 实际上在 POST 方法中提交数据它的数据数组,所以你需要通过 post 而不是 get 路由。 所以正确的代码是

Route::post('/admin',array('uses'=>'student@admin'));

按照 Laracast 中的本教程操作可能会有所帮助,
https://laracasts.com/series/laravel-from-scratch-2017/episodes/16

【讨论】:

    【解决方案4】:

    routes.php 中,将Route::get 替换为Route::post

    【讨论】:

      【解决方案5】:

      您的表单数据发布没有发布路径,请对 http 动词(获取和发布)使用路径匹配功能。用这个

      Route::match(['get', 'post'], '/admin', 'student@admin');

      您还需要更改您的管理方法,

      public function admin(Request $request){
          if($request->isMethod('get')){
              return \View::make('student.admin');
          } else {
          // your validation logic
          }
      }
      

      【讨论】:

      • 谢谢..通过使用匹配..错误消失了..但提交按钮后返回管理员登录页面而不是查看页面......我该怎么办跨度>
      • 因为你在管理方法的第一行返回登录视图,你应该使用2种不同的方法或使用条件来检查请求类型,然后处理它
      • 我的意思是它必须去主页......但它不会去那里
      • 那么我应该怎么处理那条线..我没有得到..我应该删除那条线还是把它放在其他地方..或者告诉我那个plz的编辑版本。
      • 类 'App\Http\Controllers\User' 未找到.. 这是什么@Nehal hasayeen
      猜你喜欢
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 2015-10-29
      • 2017-10-12
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      相关资源
      最近更新 更多