Laravel 4 MethodNotAllowed Exception

时间:2014-02-24 10:07:13

标签: php laravel-4

以下是我的控制器,路线和查看文件。

你能帮我解决这个问题吗?

路线档案

Route::controller('account','AccountController' );
Route::controller('course','CourseController' );
Route::get('/', 'HomeController@showIndex');

控制器文件

<?php

class CourseController extends AuthorizedController
{

    protected $whitelist = array(
        'getCourse',
        'postCourse'
    );

    /**
     * Main users page.
     *
     * @access   public
     * @return   View
     */
    public function getIndex()
    {
        // Show the page.
        //
        $course = Course::all();


        return View::make('course/create')->with('course', new Course());
    }

    public function postCourse()
    {


        $rules = array(
            'name' => 'Required',
            'has_branch'  => 'Required',
            'status'      => 'Required',
        );


        $inputs = Input::all();

        // Validate the inputs.
        //
        $validator = Validator::make($inputs, $rules);

以下是我的查看

@extends('layouts.default')

{{-- Web site Title --}}
@section('title')
@parent
:: Account
@stop

{{-- New Laravel 4 Feature in use --}}
@section('styles')
@parent
body {
    background: #f2f2f2;
}
@stop

{{-- Content --}}
@section('content')
<div class="page-header">
    <h1>New Entery</h1>
</div>
<form method="post" action="" class="form-horizontal">

    <!-- CSRF Token -->
    <input type="hidden" name="csrf_token" id="csrf_token" value="{{{ Session::getToken() }}}" />

    <!-- Course Name -->
    <div class="control-group {{{ $errors->has('name') ? 'error' : '' }}}">
        <label class="control-label" for="name">Course Name</label>
        <div class="controls">
            <input type="text" name="name" id="name" value="{{{ Request::old('name', $course->name) }}}" />
            {{ $errors->first('name') }}
        </div>
    </div>
    <!-- ./ course name -->

    <!-- Status -->
    <div class="control-group {{{ $errors->has('status') ? 'error' : '' }}}">
        <label class="control-label" for="status">Status</label>
        <div class="controls">
            <input type="text" name="status" id="status" value="{{{ Request::old('status', $course->status) }}}" />
            {{ $errors->first('status') }}
        </div>
    </div>
    <!-- ./ last name -->

    <!-- Email -->
    <div class="control-group {{{ $errors->has('description') ? 'error' : '' }}}">
        <label class="control-label" for="description">Description</label>
        <div class="controls">
            <input type="text" name="description" id="description" value="{{{ Request::old('description', $course->description) }}}" />
            {{ $errors->first('description') }}
        </div>
    </div>


    <!-- Password -->
    <div class="control-group {{{ $errors->has('has_branch') ? 'error' : '' }}}">
        <label class="control-label" for="has_branch">Has Branch</label>
        <div class="controls">
            <input type="text" name="has_branch" id="has_branch" value="" />
            {{ $errors->first('has_branch') }}
        </div>
    </div>
    <!-- ./ password -->
    <!-- Update button -->
    <div class="control-group">
        <div class="controls">
            <button type="submit" class="btn">Save</button>
        </div>
    </div>
    <!-- ./ update button -->
</form>
@stop

当我点击提交按钮它给了我:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

AuthorizedController的代码

<?php

class AuthorizedController extends BaseController
{
    protected $whitelist = array();

    /**
     * Initializer.
     *
     * @access   public
     * @return \AuthorizedController
     */
    public function __construct()
    {
        parent::__construct();
        // Check if the user is logged in.
        $this->beforeFilter('auth', array('except' => $this->whitelist));
    }
}

2 个答案:

答案 0 :(得分:0)

您的表单操作属性上没有路径:

<form method="post" action="" class="form-horizontal">

另外我建议你使用刀片。使用刀片,您可以更清晰地分离逻辑并查看。

答案 1 :(得分:0)

您在'/'上显示表单。

此表单的方法是post。

但是它指向相同的URL并且您没有'/'路由的POST操作。 我想你要做的是将这个表格发布到CourseController的postCourse动作。

您需要为'/'添加Route :: post或正确地将此表单指向正确的方法。