Laravel 4 MethodNotAllowedhttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18793313/
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 4 MethodNotAllowedhttpException
提问by Mark
I am get a MethodNotAllowedHttpException when I submit the form detailed below. The route appears correct to me and is syntactically the same as other post routes that are working just fine. The controller method exists but even so I think the exception is occuring before the request gets to the controller as item 4 on the left of the laravel error page says handleRoutingException right after item 3 which says findRoute. I am pretty sure I am not using restful routing the way you should in laravel 4 but that is because the tutorial I am following a laravel 3 tutorial and updating hte syntax to 4 as I go but like I said the other routes work fine so I can't figure out why this one isn't.
当我提交下面详述的表单时,我收到 MethodNotAllowedHttpException。这条路线对我来说是正确的,并且在语法上与其他工作正常的邮政路线相同。控制器方法存在,但即便如此,我认为异常发生在请求到达控制器之前,因为 Laravel 错误页面左侧的第 4 项在第 3 项之后说 findRoute。我很确定我没有像在 laravel 4 中那样使用 restful 路由,但那是因为我正在遵循 laravel 3 教程并在我进行时将 hte 语法更新为 4 但就像我说的其他路由工作正常所以我无法弄清楚为什么这个不是。
Template
模板
@extends('layouts.default')
@section('content')
<div id="ask">
<h1>Ask a Question</h1>
@if(Auth::check())
@include('_partials.errors')
{{ Form::open(array('ask', 'POST')) }}
{{ Form::token() }}
<p>
{{ Form::label('question', 'Question') }}
{{ Form::text('question', Input::old('question')) }}
{{ Form::submit('Ask a Question') }}
</p>
{{ Form::close() }}
@else
<p>
<p>Please login to ask or answer questions.</p>
</p>
@endif
</div><!-- end ask -->
@stop
Route
路线
Route::post('ask', array('before'=>'csrf', 'uses'=>'QuestionsController@post_create'));
Controller
控制器
<?php
class QuestionsController extends BaseController {
public $restful = true;
protected $layout = 'layouts.default';
public function __construct()
{
$this->beforeFilter('auth', array('post_create'));
}
public function get_index() {
return View::make('questions.index')
->with('title', 'Make It Snappy Q&A - Home');
}
public function post_create()
{
$validation = Question::validate(Input::all());
if($validation->passes()) {
Question::create(array(
'question'=>Input::get('question'),
'user_id'=>Auth::user()->id
));
return Redirect::Route('home')
->with('message', 'Your question has been posted.');
} else {
return Redirect::Route('register')->withErrors($validation)->withInput();
}
}
}
?>
采纳答案by searsaw
I believe defining public $restful = true;
is how it was done in Laravel 3. In Laravel 4 you define a restful controller in your routes like so:
我相信定义public $restful = true;
是在 Laravel 3 中是如何完成的。在 Laravel 4 中,您可以在路由中定义一个安静的控制器,如下所示:
Route::controller('ask', 'QuestionsController');
Then to define the functions you would not use an underscore to separate them. You must use camel case like so:
然后定义函数,你不会使用下划线来分隔它们。您必须像这样使用骆驼案例:
public function getIndex()
{
// go buck wild...
}
public function postCreate()
{
// do what you do...
}
回答by The Alpha
For RESTful Controllersyou should define the route
using Route::controller
method, i.e.
对于RESTful 控制器,您应该定义route
usingRoute::controller
方法,即
Route::controller('ask', 'QuestionsController');
and controller methods
should be prefixed with http verb
that it responbds to, for example, you may use postCreate
and you have post_create
instead, so it doesn't look like a Restful
controller.
并且controller methods
应该以http verb
它响应的前缀为前缀,例如,您可以使用postCreate
并且您拥有post_create
,因此它看起来不像Restful
控制器。
You are using public $restful = true;
in your controller, this is not being used in Laravel-4
, and public $restful = true;
may causing the problem, so remove this line.
您正在public $restful = true;
控制器中使用,但未在 中使用Laravel-4
,public $restful = true;
可能会导致问题,因此请删除此行。