laravel 抛出 MethodNotAllowedHttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19760585/
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
laravel throwing MethodNotAllowedHttpException
提问by spacemonkey
I am trying to get something very basic running. I am used to CI and now learning Laravel 4, and their docs are not making it easy! Anyways, I am trying to create a login form and just make sure that data is posted successfully by printing it in the next form. I am getting this exception:
我正在尝试运行一些非常基本的东西。我习惯了 CI,现在正在学习 Laravel 4,他们的文档并不容易!无论如何,我正在尝试创建一个登录表单,并通过在下一个表单中打印来确保数据成功发布。我收到此异常:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
and my MemberController.php:
和我的 MemberController.php:
public function index()
{
if (Session::has('userToken'))
{
/*Retrieve data of user from DB using token & Load view*/
return View::make('members/profile');
}else{
return View::make('members/login');
}
}
public function validateCredentials()
{
if(Input::post())
{
$email = Input::post('email');
$password = Input::post('password');
return "Email: " . $email . " and Password: " . $password;
}else{
return View::make('members/login');
}
}
and routes has:
和路线有:
Route::get('/', function()
{
return View::make('hello');
});
Route::get('/members', 'MemberController@index');
Route::get('/validate', 'MemberController@validateCredentials');
and finally my view login.php has this form direction:
最后我的视图 login.php 有这个表单方向:
<?php echo Form::open(array('action' => 'MemberController@validateCredentials')); ?>
Any help will be greatly appreciated.
任何帮助将不胜感激。
回答by hayhorse
You are getting that error because you are posting to a GET
route.
您收到该错误是因为您要发布到GET
路线。
I would split your routing for validate
into a separate GET
and POST
routes.
我想分裂您的路由validate
到一个单独的GET
和POST
路线。
New Routes:
新路线:
Route::post('validate', 'MemberController@validateCredentials');
Route::get('validate', function () {
return View::make('members/login');
});
Then your controller method could just be
那么你的控制器方法可能只是
public function validateCredentials()
{
$email = Input::post('email');
$password = Input::post('password');
return "Email: " . $email . " and Password: " . $password;
}
回答by Blessing
My suspicion is the problem lies in your route definition.
我怀疑问题在于您的路线定义。
You defined the route as a GET
request but the form is probably sending a POST
request. Change your route definition.
您将路由定义为GET
请求,但表单可能正在发送POST
请求。更改您的路线定义。
Route::post('/validate', 'MemberController@validateCredentials');
It's generally better practice to use named routes (helps to scale if the controller method/class changes).
使用命名路由通常是更好的做法(如果控制器方法/类发生变化,有助于扩展)。
Route::post('/validate', array(
'as' => 'validate',
'uses' => 'MemberController@validateCredentials'
));
In the form use the following
在表格中使用以下内容
<?php echo Form::open(array('route' => 'validate')); ?>
回答by Eli
The problem is the you are using POST
but actually you have to perform PATCH
To fix this add
问题是您正在使用POST
但实际上您必须执行PATCH
以修复此添加
<input name="_method" type="hidden" value="PATCH">
Just after the Form::model
line
就在这Form::model
条线之后
回答by Dan
I encountered this problem as well and the other answers here were helpful, but I am using a Route::resource
which takes care of GET
, POST
, and other requests.
我遇到了这个问题,以及这里的其他答案是有帮助的,但是我用的是Route::resource
这需要照顾GET
,POST
以及其他请求。
In my case I left my route as is:
就我而言,我按原样离开了路线:
Route::resource('file', 'FilesController');
And simply modified my form to submit to the store
function in my FilesController
并简单地修改我的表单以提交给store
我的函数FilesController
{{ Form::open(array('route' => 'file.store')) }}
This fixed the issue, and I thought it was worth pointing out as a separate answer since various other answers suggest adding a new POST
route. This is an option but it's not necessary.
这解决了这个问题,我认为值得指出一个单独的答案,因为其他各种答案都建议添加一条新POST
路线。这是一个选项,但不是必需的。
回答by minitechi
That is because you are posting data through a get method.
那是因为您通过 get 方法发布数据。
Instead of
代替
Route::get('/validate', 'MemberController@validateCredentials');
Try this
尝试这个
Route::post('/validate', 'MemberController@validateCredentials');
回答by kousher alam pranto
Typically MethodNotAllowedHttpException
happens when
通常MethodNotAllowedHttpException
发生在
route method does not match.
路由方法不匹配。
Suppose you define POST
request route file, but you sending GET
request to the route.
假设您定义了POST
请求路由文件,但是您向GET
路由发送请求。
回答by itachi
<?php echo Form::open(array('action' => 'MemberController@validateCredentials')); ?>
by default, Form::open()
assumes a POST
method.
默认情况下,Form::open()
假定一个POST
方法。
you have GET
in your routes. change it to POST
in the corresponding route.
你有GET
你的路线。将其更改为POST
在相应的路径中。
or if you want to use the GET
method, then add the method
param.
或者如果您想使用该GET
方法,则添加method
参数。
e.g.
例如
Form::open(array('url' => 'foo/bar', 'method' => 'get'))
回答by Faruk Omar
I faced the error,
problem was FORM METHOD
我遇到了错误,
问题是FORM METHOD
{{ Form::open(array('url' => 'admin/doctor/edit/'.$doctor->doctor_id,'class'=>'form-horizontal form-bordered form-row-stripped','method' => 'PUT','files'=>true)) }}
It should be like this
应该是这样的
{{ Form::open(array('url' => 'admin/doctor/edit/'.$doctor->doctor_id,'class'=>'form-horizontal form-bordered form-row-stripped','method' => 'POST','files'=>true)) }}
回答by iSWORD
In my case, I was sending a POST
request over HTTP
to a server where I had set up Nginx
to redirect all requests to port 80
to port 443
where I was serving the app over HTTPS
.
就我而言,我将POST
请求发送HTTP
到我已设置Nginx
为将所有请求重定向到端口80
到443
我通过HTTPS
.
Making the request to the correct port directly fixed the problem. In my case, all I had to do is replace http://
in the request URL to https://
since I was using the default ports 80
and 443
respectively.
向正确的端口发出请求直接解决了问题。就我而言,我所要做的就是将http://
请求 URL替换为 ,https://
因为我分别使用了默认端口80
和443
。
回答by Slimane MEHARZI
Generally, there is a mistake in the HTTP verb used eg:
通常,使用的 HTTP 动词存在错误,例如:
Calling PUT
route with POST
request
PUT
带POST
请求的调用路由