2014-02-24 32 views
0

以下是我的控制器,路由和視圖文件。Laravel 4 MethodNotAllowed異常

你能幫我解決這個問題嗎?

路由文件

Route::controller('account','AccountController'); 
Route::controller('course','CourseController'); 
Route::get('/', '[email protected]'); 

控制器的文件

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

什麼是'AuthorizedController'? – markcial

+0

@markcial,這是我的第一個laravel應用程序,我爲AuthorizedController添加代碼 – krish

回答

0

你不必在你的表單action屬性路徑:

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

而且我會建議你使用刀片。使用刀片,您可以更清晰地分離邏輯並查看。

+0

請看下面@ https://github.com/krishnal/Laravel4-Bootsrap,這裏的帳戶控制器工作完美,它具有相同的代碼 – krish

0

您在'/'上顯示錶格。

此表單的方法是發佈。

但它指向相同的URL,並且您沒有針對'/'路線的POST操作。 我認爲你要做的是將此表單發佈到CourseController的postCourse操作。

您需要爲「/」添加Route :: post或將此表單正確指向正確的方法。

+0

請看@ https ://github.com/krishnal/Laravel4-Bootsrap,這裏帳戶控制器工作完美,它具有相同的代碼 – krish

+0

但方法的名稱是getIndex和postIndex,這裏不是這種情況... 再次閱讀我的答案 – matiit

+0

對不起,我是laraval的新成員,所以我不清楚,但postRegister正在帳戶控制器中工作,它是否與課程控制器中的postCourse一樣?或者你能幫助建議我的解決方案? – krish