Laravel 5 RouteCollection.php 行 201 中的 MethodNotAllowedHttpException:

Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 201:

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

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

调用时出现以下错误:

MethodNotAllowedHttpException in RouteCollection.php line 201

<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

注:

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

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

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

提示:

Read more on here并尝试通过比较方法和路由来调试这些东西。

默认情况下,laravel 5 中不包含表单外观。您应该通过

安装它
composer require "illuminate/html":"5.0.*"

并在 app.php 中更新。

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

您的表单数据 posting 没有 post 路由,请对两个 http 谓词(get 和 post)使用路由匹配功能。使用这个

路由::匹配(['get', 'post'], '/admin', 'student@admin');

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

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

我就是这样做的。

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

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

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。您可以查看 this video on Laracasts 了解 Form Facade 是什么以及您应该如何使用它。

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

在路线中web.php 您的代码是

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

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

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

从 Laracast 学习本教程可能会有帮助,
https://laracasts.com/series/laravel-from-scratch-2017/episodes/16