laravel throwing MethodNotAllowedHttpException
나는 매우 기본적인 실행을 시도하고 있습니다. 나는 CI에 익숙하고 이제 Laravel 4를 배우고 있으며 그들의 문서는 쉽지 않습니다! 어쨌든 로그인 양식을 만들고 다음 양식에 인쇄하여 데이터가 성공적으로 게시되었는지 확인합니다. 이 예외가 발생합니다.
Symfony \ 구성 요소 \ HttpKernel \ 예외 \ MethodNotAllowedHttpException
내 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');
}
}
경로는 다음과 같습니다.
Route::get('/', function()
{
return View::make('hello');
});
Route::get('/members', 'MemberController@index');
Route::get('/validate', 'MemberController@validateCredentials');
마지막으로 내보기 login.php에는 다음과 같은 형식 방향이 있습니다.
<?php echo Form::open(array('action' => 'MemberController@validateCredentials')); ?>
어떤 도움이라도 대단히 감사하겠습니다.
GET
경로에 게시하기 때문에 해당 오류가 발생 합니다.
나는에 대한 라우팅을 분할 할 validate
별도의로 GET
및 POST
경로.
새로운 경로 :
Route::post('validate', 'MemberController@validateCredentials');
Route::get('validate', function () {
return View::make('members/login');
});
그런 다음 컨트롤러 방법은
public function validateCredentials()
{
$email = Input::post('email');
$password = Input::post('password');
return "Email: " . $email . " and Password: " . $password;
}
The problem is the you are using POST
but actually you have to perform PATCH
To fix this add
<input name="_method" type="hidden" value="PATCH">
Just after the Form::model
line
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.
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')); ?>
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.
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
{{ 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.
That is because you are posting data to a GET
route.
Route::get('/validate', 'MemberController@validateCredentials');
try this
Route::post('/validate', 'MemberController@validateCredentials');
Typically MethodNotAllowedHttpException
happens when
route method does not match.
Suppose you define POST
request route file, but you sending GET
request to the route.
<?php echo Form::open(array('action' => 'MemberController@validateCredentials')); ?>
by default, Form::open()
assumes a POST
method.
you have GET
in your routes. change it to POST
in the corresponding route.
or if you want to use the GET
method, then add the method
param.
e.g.
Form::open(array('url' => 'foo/bar', 'method' => 'get'))
I faced the error,
problem was 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)) }}
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
.
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.
Generally, there is a mistake in the HTTP verb used eg:
Calling PUT
route with POST
request
My problem was not that my routes were set up incorrectly, but that I was referencing the wrong Form
method (which I had copied from a different form). I was doing...
{!! Form::model([ ... ]) !!}
(with no model specified). But I should have been using the regular open
method...
{!! Form::open([ ... ]) !!}
Because the first parameter to model
expect an actual model, it was not getting any of my options I was specifying. Hope this helps someone who knows their routes are correct, but something else is amiss.
I also had the same error but had a different fix, in my XYZ.blade.php
I had:
{!! Form::open(array('ul' => 'services.store')) !!}
which gave me the error, - I still don't know why- but when I changed it to
{!! Form::open(array('route' => 'services.store')) !!}
It worked!
I thought it was worth sharing :)
Laravel sometimes does not support {!! Form::open(['url' => 'posts/store']) !!}
for security reasons. That's why the error has happened. You can solve this error by simply replacing the below code
{!! Form::open(array('route' => 'posts.store')) !!}
Error Code {!! Form::open(['url' => 'posts/store']) !!}
Correct Code {!! Form::open(array('route' => 'posts.store')) !!}
In my case, it was because my form was sending to a route with a different middleware. So it blocked from sending information to this specific route.
well when i had these problem i faced 2 code errors
{!! Form::model(['method' => 'POST','route' => ['message.store']]) !!}
i corrected it by doing this
{!! Form::open(['method' => 'POST','route' => 'message.store']) !!}
so just to expatiate i changed the form model
to open
and also the route where wrongly placed in square braces.
// not done
Route::post('`/posts/{id}`', 'PostsController@store')->name('posts.store');
return redirect('`/posts'`)->with('status','Post was created !');
// done
Route::post('`/posts`', 'PostsController@store')->name('posts.store');
return redirect('`/posts'`)->with('status','Post was created !');
참고URL : https://stackoverflow.com/questions/19760585/laravel-throwing-methodnotallowedhttpexception
'Programing' 카테고리의 다른 글
UNIX에서 터미널 응용 프로그램의 색상 텍스트 (0) | 2020.08.18 |
---|---|
개인 키를 사용하여 .pfx 파일을 키 저장소로 변환하는 방법은 무엇입니까? (0) | 2020.08.18 |
PostMan에서 특정 CURL 시뮬레이션 (0) | 2020.08.18 |
SQL Server 2008의 XML 필드에서 값 선택 (0) | 2020.08.18 |
CSS로 무시 하시겠습니까? (0) | 2020.08.18 |