3

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.

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


}
?>

2 Answers 2

1

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:

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...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks to you both I will go back and re write my routes correctly with the laravel 4 methods and see how I get on.
1

For RESTful Controllers you should define the route using Route::controller method, i.e.

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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.