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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:39:43  来源:igfitidea点击:

laravel throwing MethodNotAllowedHttpException

routinglaravellaravel-4

提问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 GETroute.

您收到该错误是因为您要发布到GET路线。

I would split your routing for validateinto a separate GETand POSTroutes.

我想分裂您的路由validate到一个单独的GETPOST路线。

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 GETrequest but the form is probably sending a POSTrequest. 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 POSTbut actually you have to perform PATCHTo fix this add

问题是您正在使用POST但实际上您必须执行PATCH以修复此添加

<input name="_method" type="hidden" value="PATCH">

Just after the Form::modelline

就在这Form::model条线之后

回答by Dan

I encountered this problem as well and the other answers here were helpful, but I am using a Route::resourcewhich takes care of GET, POST, and other requests.

我遇到了这个问题,以及这里的其他答案是有帮助的,但是我用的是Route::resource这需要照顾GETPOST以及其他请求。

In my case I left my route as is:

就我而言,我按原样离开了路线:

Route::resource('file', 'FilesController');

And simply modified my form to submit to the storefunction 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 POSTroute. 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 MethodNotAllowedHttpExceptionhappens when

通常MethodNotAllowedHttpException发生在

route method does not match.

路由方法不匹配。

Suppose you define POSTrequest route file, but you sending GETrequest to the route.

假设您定义了POST请求路由文件,但是您向GET路由发送请求。

回答by itachi

<?php echo Form::open(array('action' => 'MemberController@validateCredentials')); ?>

by default, Form::open()assumes a POSTmethod.

默认情况下,Form::open()假定一个POST方法。

you have GETin your routes. change it to POSTin the corresponding route.

你有GET你的路线。将其更改为POST在相应的路径中。

or if you want to use the GETmethod, then add the methodparam.

或者如果您想使用该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 POSTrequest over HTTPto a server where I had set up Nginxto redirect all requests to port 80to port 443where I was serving the app over HTTPS.

就我而言,我将POST请求发送HTTP到我已设置Nginx为将所有请求重定向到端口80443我通过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 80and 443respectively.

向正确的端口发出请求直接解决了问题。就我而言,我所要做的就是将http://请求 URL替换为 ,https://因为我分别使用了默认端口80443

回答by Slimane MEHARZI

Generally, there is a mistake in the HTTP verb used eg:

通常,使用的 HTTP 动词存在错误,例如:

Calling PUTroute with POSTrequest

PUTPOST请求的调用路由