为什么我在资源丰富的路由上获得MethodNotAllowedHttpException?

时间:2016-04-29 15:02:36

标签: php laravel laravel-5

我在我设置的资源丰富的路由上不断收到MethodNotAllowedHttpException错误。当我运行php artisan route:list时,补丁/ put请求存在,我的表单引用jobs.update并传递id。我对所有应用程序的其他模块使用相同的过程而没有这样的问题。任何异常可能来自哪些想法?

我的路线档案:

// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

//Homepage...
Route::get('/', function () { return view('homepage'); });

// Resourceful modules...
Route::resource('conferences', 'ConferencesController');
Route::resource('pages', 'PagesController');
Route::resource('jobs', 'JobsController');
Route::resource('admin/menu', 'MenuItemsController');



// Actions...
Route::put('admin/menu/{id}/promote', [
    'as' => 'admin.menu.promote', 'uses' => 'MenuItemsController@promote'
]);
Route::put('admin/menu/{id}/demote', [
    'as' => 'admin.menu.promote', 'uses' => 'MenuItemsController@demote'
]);
Route::get('{parent}/{slug}', 'PagesController@show');
Route::get('{slug}', 'PagesController@showSlug');

我的更新方法:

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $job = $this->job->find($id);
        $job->update($request->all());
        return redirect('jobs.index');
    }

我的表格:

        <form action="{{ route('jobs.update', $job->id) }}" method="POST" class="col s12" enctype="multipart/form-data">
        {!! csrf_field() !!}
        <div class="row">
            <div class="input-field col s12 m6">
                <input type="text" name="position" id="position" class="validate" placeholder="Position" value="{{ old('position') ? old('position') : (isset($job) ? $job->position : '')}}">
                <label for="position" class="col-sm-3 control-label">Position</label>
            </div>
            <div class="input-field col s12 m6">
                <select name="type" id="type">
                    @if($job->type == "full")
                        <option value="" disabled selected>Type</option>
                        <option value="full" selected>Full-time</option>
                        <option value="part">Part-time</option>
                        <option value="other">Other</option>
                    @elseif($job->type == "part")
                        <option value="" disabled selected>Type</option>
                        <option value="full">Full-time</option>
                        <option value="part" selected>Part-time</option>
                        <option value="other">Other</option>
                    @elseif($job->type == "other")
                        <option value="" disabled>Type</option>
                        <option value="full">Full-time</option>
                        <option value="part">Part-time</option>
                        <option value="other" selected>Other</option>
                    @else
                        <option value="" disabled selected>Type</option>
                        <option value="full">Full-time</option>
                        <option value="part">Part-time</option>
                        <option value="other">Other</option>
                    @endif
                </select>
                <label>Type</label>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12 m6">
                <input type="text" name="reports_to" id="reports_to" class="validate" placeholder="Reports to" value="{{ old('reports_to') ? old('reports_to') : (isset($job) ? $job->reports_to : '')}}">
                <label for="reports-to" class="col-sm-3 control-label">Reports to</label>
            </div>
            <div class="input-field col s12 m6">
                <input type="text" name="office_department" id="office_department" class="validate" placeholder="Reports in office" value="{{ old('office_department') ? old('office_Departmnet') : (isset($job) ? $job->office_department : '')}}">
                <label for="office_department" class="col-sm-3 control-label">Reports in office</label>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12 m6">
                <input type="date" class="datepicker" name="start_date" id="start_date" class="validate" value="{{ old('start_date') ? old('start_date') : (isset($job) ? $job->start_date : '')}}">
                <label for="start-date" class="col-sm-3 control-label">Start date</label>
            </div>
            <div class="input-field col s12 m6">
                <input type="date" class="datepicker" name="application_due_date" id="application_due_date" class="validate" value="{{ old('application_due_date') ? old('application_due_date') : (isset($job) ? $job->application_due_date : '')}}">
                <label for="application_due_date" class="col-sm-3 control-label">Application Due Date</label>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12 m6">
                <input type="text" name="city" id="city" class="validate" placeholder="City" value="{{ old('city') ? old('city') : (isset($job) ? $job->city : '')}}">
                <label for="city" class="col-sm-3 control-label">City</label>
            </div>
            <div class="input-field col s12 m6">
                <input type="text" name="state" id="state" class="validate" placeholder="State" value="{{ old('state') ? old('state') : (isset($job) ? $job->state : '')}}">
                <label for="state" class="col-sm-3 control-label">State</label>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12 m6">
                <input type="text" name="institution" id="institution" class="validate" placeholder="Institution" value="{{ old('institution') ? old('institution') : (isset($job) ? $job->institution : '')}}">
                <label for="institution" class="col-sm-3 control-label">Institution</label>
            </div>
            <div class="input-field col s12 m6">
                <input type="text" name="salary_range" id="salary_range" class="validate" placeholder="Salary Range" value="{{ old('salary_range') ? old('salary_range') : (isset($job) ? $job->salary_range : '')}}">
                <label for="institution" class="col-sm-3 control-label">Salary Range</label>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12 m12">
                <p class="col-sm-3 control-label">Job Description</p>
                <textarea name="job_description" id="job_description" class="mce validate">{{ old('job_description') ? old('job_description') : (isset($job) ? $job->job_description : '')}}</textarea>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12 m12">
                <p class="col-sm-3 control-label">Job Duties</p>
                <textarea name="job_duties" id="job_duties" class="mce validate" >{{ old('job_duties') ? old('job_duties') : (isset($job) ? $job->job_duties : '')}}</textarea>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12 m12">
                <p class="col-sm-3 control-label">Qualifications</p>
                <textarea name="qualifications" id="qualifications" class="mce validate" >{{ old('qualifications') ? old('qualifications') : (isset($job) ? $job->qualifications : '')}}</textarea>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12 m12">
                <p class="col-sm-3 control-label">Benefits</p>
                <textarea name="benefits" id="benefits" class="mce validate" >{{ old('benefits') ? old('benefits') : (isset($job) ? $job->benefits : '')}}</textarea>
            </div>
        </div>
        <div class="row">
            <div class="input-field col s12 m12">
                <p class="col-sm-3 control-label">Application Procedure</p>
                <textarea name="application_procedure" id="application_procedure" class="mce validate" >{{ old('application_procedure') ? old('application_procedure') : (isset($job) ? $job->application_procedure : '')}}</textarea>
            </div>
        </div>
        <div class="row">
            <div class="input-field">
                <button class="btn waves-effect waves-light col s12 m3" type="submit" name="action">Submit
                    <i class="material-icons right">send</i>
                </button>
            </div>
        </div>
    </form>

3 个答案:

答案 0 :(得分:3)

documentation所示,资源控制器的update操作需要PUTPATCH而不是POST操作。

但HTML表单不支持PUT,PATCH或DELETE操作。您可以根据伪造的documentation绕过它:

<form action="{{ route('jobs.update', $job->id) }}" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

答案 1 :(得分:1)

默认情况下,您无法将表单提交给路线; PATCHPUTDELETE

相反,Laravel允许您将请求视为其中一个请求:

您需要在表单中添加隐藏的输入:

<input type="hidden" name="_method" value="PUT">

答案 2 :(得分:1)

尝试在表单中添加html隐藏标记。

<input name="_method" type="hidden" value="PUT">