Laravel 4方法不允许HttpException
当我提交下面详述的表格时,我得到了MethodNotAllowedHttpException。在我看来,这条路线是正确的,并且在语法上与其他正常工作的post路线相同。控制器方法存在,但即使如此,我认为异常仍发生在请求到达控制器之前,因为laravel错误页面左侧的第4项表示HandlerRoutingException,紧跟在第3项之后,第3项表示FindLote。我很确定我没有像在Laravel4中那样使用restful路由,但这是因为我在学习Laravel3教程,并在学习时将语法更新为4,但正如我所说的,其他路由工作正常,所以我无法理解为什么这个路由不工作 模板Laravel 4方法不允许HttpException,laravel,laravel-4,Laravel,Laravel 4,当我提交下面详述的表格时,我得到了MethodNotAllowedHttpException。在我看来,这条路线是正确的,并且在语法上与其他正常工作的post路线相同。控制器方法存在,但即使如此,我认为异常仍发生在请求到达控制器之前,因为laravel错误页面左侧的第4项表示HandlerRoutingException,紧跟在第3项之后,第3项表示FindLote。我很确定我没有像在Laravel4中那样使用restful路由,但这是因为我在学习Laravel3教程,并在学习时将语法更新为4,
@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
控制器
<?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();
}
}
}
?>
因为您应该使用route::controller
方法定义路线
,即
Route::controller('ask', 'QuestionsController');
而控制器方法
应该以它所响应的http谓词
作为前缀,例如,您可以使用后创建
,而使用后创建
,因此它看起来不像是一个Restful
控制器
您使用的是
public$restful=true
在您的控制器中,这不在Laravel-4
中使用,并且public$restful=true代码>可能会导致问题,因此请删除这一行。我相信定义public$restful=true代码>是如何在Laravel 3中完成的。在Laravel 4中,您在路由中定义了一个restful控制器,如下所示:
Route::controller('ask', 'QuestionsController');
然后,为了定义函数,您不会使用下划线来分隔它们。您必须像这样使用camel case:
public function getIndex()
{
// go buck wild...
}
public function postCreate()
{
// do what you do...
}
多亏了你们两位,我将返回并用laravel 4方法正确地重新编写我的路线,看看我的进展如何。
public function getIndex()
{
// go buck wild...
}
public function postCreate()
{
// do what you do...
}