RouteCollection.php 第 201 行中的 Laravel 5 MethodNotAllowedHttpException:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31228750/
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-08-25 22:19:27  来源:igfitidea点击:

Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 201:

phplaravellaravel-5laravel-routing

提问by deep singh

I have a number of php files in my project:

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

admin.blade.php: this files contains the admin form.

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

When called it show the following error:

调用时显示以下错误:

MethodNotAllowedHttpException in RouteCollection.php line 201

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>

In route.php, this call is made:

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

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

This is the function in student.php

这是中的函数 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.');
    }
}

I don't know much about creating an admin area, I am just trying to create it.

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

采纳答案by Saiyan Prince

This is how I do it.

我就是这样做的。

Routes.php

路由.php

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

admin_login.blade.php

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

仪表盘.blade.php

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

UsersController.php

用户控制器.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');
}

Your Code:

您的代码:

In routes.php, you have only 1 route, i.e.,

在 中routes.php,您只有 1 条路线,即

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

And there is no declaration of postmethod, hence, the MethodNotAllowedHttpException

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

Also, in your controller, you are returning the view first and then processing the form which is not going to work at all. You first need to process the form and then return the view.

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

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

As @Sulthan has suggested, you should use Form Facade. You can check out this videoon Laracastsabout what Form Facadeis and how you should use it.

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

回答by Sulthan Allaudeen

You're using postmethod in the form but you're having getmethod in the routes.

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

So, Change the method to postin your routes

因此,将方法更改为post您的路线

Note :

笔记 :

I recommend you to make use of the default form opening of Laravel like the below given which is always the best practise

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

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

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

Tips :

提示 :

Read more on hereand try to debug such things by comparing the methods and routes.

在此处阅读更多内容,并尝试通过比较方法和路由来调试此类事情。

Form facade is not included in laravel 5 by default. You shall install it by

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

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

and updating in the app.php.

并在 app.php 中更新。

I have written a blogwhich gives a breif about this installation.

我写了一篇博客,简要介绍了此安装。

回答by Vrushal Raut

In Routes web.phpYour code is

在路由web.php你的代码是

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

which is wrong. Actually submitting data in POST method its array of data so you need to route through post instead of get. so correct code is

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

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

Follow this tutorial form Laracast it might helpful,
https://laracasts.com/series/laravel-from-scratch-2017/episodes/16

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

回答by daidongsheng

In routes.php, replace Route::getby Route::post.

routes.php,替换Route::getRoute::post

回答by Nehal Hasnayeen

You have no post route for your form data posting , use route match function for both http verb (get & post). use this

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

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

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

Also you need to change your admin method,

您还需要更改管理方法,

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