laravel创建请求不起作用

时间:2015-03-17 14:42:54

标签: php laravel

你可能会看到我对laravel很新。我遇到了一个似乎无法看到我已经制作的新课程的问题......

首先我跑了......

php artisan make:request CreateSongRequest

反过来在app / Http / Requests /

中生成了一个CreateSongRequest.php文件

内容......

<?php namespace App\Http\Requests;

use App\Http\Requests\Request;

class CreateSongRequest extends Request {

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [

            //
        ];
    }

}

在我的控制器中,我有以下方法的表格帖子......

public function store(CreateSongRequest $request, Song $song) {


    $song->create($request->all());

    return redirect()->route('songs_path');
}

当我提交表单时,我收到以下错误......

RouteDependencyResolverTrait.php第53行中的ReflectionException:类App \ Http \ Controllers \ CreateSongRequest不存在

2 个答案:

答案 0 :(得分:9)

您需要在控制器顶部添加:

use App\Http\Requests\CreateSongRequest;

答案 1 :(得分:0)

试试这个..它有效..

public function store(Request $request, Song $song)
{

   $this->validate($request, [
      'title' => 'required',
      'slug' => 'required|unique:songs,slug',
   ]);

   $song->create($request->all());
   return redirect()->route('songs_path');

}

来源:http://laravel.com/docs/5.1/validation