laravel 创建请求不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29102099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel create request not working
提问by Adrian
You can probably see Im very new to laravel. I have ran into an issue where it cant seem to see the new class I've made...
你可能会看到我对 Laravel 很陌生。我遇到了一个问题,它似乎看不到我创建的新课程......
Firstly I ran....
首先我跑了....
php artisan make:request CreateSongRequest
which in turn generated a CreateSongRequest.php file in app/Http/Requests/
这反过来又在 app/Http/Requests/ 中生成了一个 CreateSongRequest.php 文件
The contents...
内容...
<?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 [
//
];
}
}
In my controller I have the form post to the following method...
在我的控制器中,我将表单发布到以下方法...
public function store(CreateSongRequest $request, Song $song) {
$song->create($request->all());
return redirect()->route('songs_path');
}
When I submit the form, Im getting the following error...
当我提交表单时,我收到以下错误...
ReflectionException in RouteDependencyResolverTrait.php line 53: Class App\Http\Controllers\CreateSongRequest does not exist
RouteDependencyResolverTrait.php 第 53 行中的 ReflectionException:Class App\Http\Controllers\CreateSongRequest 不存在
回答by JoeCoder
You need to add this at the top of your controller:
您需要在控制器顶部添加:
use App\Http\Requests\CreateSongRequest;
回答by Ridwan Pujakesuma
Try this.. It works..
试试这个..它有效..
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');
}