【问题标题】:Laravel 4 MethodNotAllowedhttpExceptionLaravel 4 MethodNotAllowedhttpException
【发布时间】:2013-09-18 13:18:24
【问题描述】:

当我提交下面详述的表单时,我收到了 MethodNotAllowedHttpException。该路线对我来说似乎是正确的,并且在语法上与其他运行良好的帖子路线相同。控制器方法存在,但即便如此,我认为异常发生在请求到达控制器之前,因为 laravel 错误页面左侧的第 4 项在第 3 项显示 findRoute 之后显示了 handleRoutingException。我很确定我没有像在 laravel 4 中那样使用 restful 路由,但那是因为教程我正在遵循 laravel 3 教程并将 hte 语法更新为 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

路线

Route::post('ask', array('before'=>'csrf', 'uses'=>'QuestionsController@post_create'));

控制器

<?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();
        }
    }


}
?>

【问题讨论】:

    标签: laravel laravel-4


    【解决方案1】:

    对于RESTful Controllers,您应该使用Route::controller 方法定义route,即

    Route::controller('ask', 'QuestionsController');
    

    controller methods 应该以http verb 为前缀,它会响应,例如,您可以使用postCreate 而您有post_create,所以它看起来不像Restful 控制器。

    您在控制器中使用了public $restful = true;Laravel-4 中没有使用,public $restful = true; 可能会导致问题,因此请删除此行。

    【讨论】:

      【解决方案2】:

      我相信定义 public $restful = true; 是在 Laravel 3 中完成的。在 Laravel 4 中,您可以像这样在路由中定义一个 restful 控制器:

      Route::controller('ask', 'QuestionsController');
      

      然后要定义函数,您不会使用下划线来分隔它们。您必须像这样使用驼峰式案例:

      public function getIndex()
      {
          // go buck wild...
      }
      
      public function postCreate() 
      {
          // do what you do...
      }
      

      【讨论】:

      • 感谢你们俩,我会回去用 laravel 4 方法正确地重新编写我的路线,看看我的进展如何。
      猜你喜欢
      • 2013-07-04
      • 2014-09-16
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 2019-04-15
      • 2015-09-11
      相关资源
      最近更新 更多