Laravel 5:Route Collection.php 第 201 行中的方法不允许 Http 异常:

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

Laravel 5 : Method Not Allowed Http Exception in Route Collection.php line 201:

phplaravellaravel-5

提问by Gammer

I have a sign-up form and getting the sign-up form values in routes.php. My rout.phpcode is :

我有一个注册表单并在routes.php. 我的rout.php代码是:

Route::get('/', function () {

    return view('login');
});

Route::get('/index', function(){
    return view('index');
});

Route::get('/register', function(){
    return view('register');
});

Route::post('/register',function(){
    $user = new \App\User;
    $user->username = input::get('username');
    $user->email  = input::get('email');
    $user->password = Hash::make(input::get('username'));
    $user->designation = input::get('designation');
    $user->save();

});

The form action is to index.php and also have hidden field for csrf_token():

表单动作是 index.php 并且还有 csrf_token() 的隐藏字段:

<form action="index" method="post">         
<input type="hidden" name="_token" value="{{ csrf_token() }}">

The error is : Method Not Allowed Http Exception in Route Collection.php line 201:

错误是: Method Not Allowed Http Exception in Route Collection.php line 201:

回答by KristofM

You have correctly registered a route with the post method on the page /register, but in the form you do a post to the index route. Change

您已经使用页面 /register 上的 post 方法正确注册了一条路由,但是在表单中您对索引路由进行了发布。改变

   <form action="index" method="post">         
   <input type="hidden" name="_token" value="{{ csrf_token() }}">

to

   <form action="register" method="post">         
   <input type="hidden" name="_token" value="{{ csrf_token() }}">

to send the post values to the register route instead of the index route. You don't return a view or redirect at the end of your register function so I would either add return view('index');or return redirect('index');as last line of your register function to redirect the user to the index page (or just return the index view)

将 post 值发送到寄存器路由而不是索引路由。您不会在注册函数的末尾返回视图或重定向,因此我会添加return view('index');return redirect('index');作为注册函数的最后一行以将用户重定向到索引页面(或仅返回索引视图)

Alternatively you can change the index route to accept post values:

或者,您可以更改索引路由以接受帖子值:

Route::post('/index', function(){
    return view('index');
});