创建嵌套控制器对象laravel时出现禁止错误

时间:2015-04-15 14:18:09

标签: php laravel-5

我正在接受关于https://laracasts.com/series/laravel-5-fundamentals的教程 然后我开始深入研究本教程https://www.flynsarmy.com/2015/02/creating-a-basic-todo-application-in-laravel-5-part-4/

中的嵌套控制器

我有一个类似的逻辑项目有一个假设。

所以我设置了我的嵌套路线

Route::resource('project','ProjectsController');
Route::resource('project.hypothesis','HypothesisController'); 

然后创建了一个表单,用于将假设添加到项目

{!! Form::model(new App\Hypothesis, ['route' => ['project.hypothesis.store', $project->id]]) !!}
    @include ('hypothesis.form',['submitButtonText'=>'create']);
{!! Form::close() !!}

我还创建了一个带有基本验证规则的 HyphothesisRequest

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class HyphothesisRequest extends Request {

/**
 * Determine if the user is authorized to make this request.
 *
 * @return bool
 */
public function authorize()
{
    return false;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'description' =>'required'
    ];
}
}

现在根据我控制器上面的教程我已经

public function store(Project $project, HyphothesisRequest $request)
{
    $this->validate($request);
    $h = new Hypothesis;
    $h->description = $request->description;
    $h->project_id = $project->id;
    $h->save();

    return Redirect::route('project.show', $project->id);

}

问题是,当 HyphothesisRequest $ request 作为参数传递时,我从laravel获得一个禁止页面。当我删除它时,它会转到所需的页面,但没有验证。

我处于基本水平,所以请耐心等待:)

1 个答案:

答案 0 :(得分:2)

尝试更改

public function authorize()
{
    return false;
}

public function authorize()
{
    return true;
}