Class App \ Http \ Controllers \ Requests \ ArticleRequest不存在Laravel5.3

时间:2016-11-20 07:14:38

标签: php laravel

其实我是php和Laravel5.3的新手,当我想设置表单请求时我有这个问题(做一个简单的博客页面)

Route.php第339行中的ReflectionException:Class App \ Http \ Controllers \ Requests \ ArticleRequest不存在

这是我的控制器代码(Filename:ArticlesControllers.php):

 <?php

    namespace App\Http\Controllers;
    //namespace App\Http\Controllers;
    use App\Http\Requests\ArticleRequest;
    use App\Article;
    use Carbon\Carbon;
    use Illuminate\Http\Request;

    class ArticlesControllers extends Controller
    {
        //
        public function index(){
            $articles = Article::latest()->get();
            //return 'articles';
            return view('articles.index')->with('articles',$articles);
        }

        public function show($id){
            $article = Article::find($id);
    //        if(is_null($article)){
    //            abort(404);
    //        }
            //dd($artilce);
            return view('articles.show',compact('article'));
        }

        public function create(){
            return view('articles.create');
        }

        public function store(Requests\ArticleRequest $request){
            //dd($request->all());
            //接受post过来的数据
            //存入数据库
            //重定向
            $input=$request->all();

            //$input['published_at']=Carbon::now();
            Article::create($input);
            return redirect('/articles');
        }
    }

请求文件代码:(文件名:ArticleRequest.php在路径请求中)

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ArticleRequest extends FormRequest
{
    /**
     * 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 [
            'title'=>'required|min:3',
            'content'=>'required',
            'published'=>'require'
        ];
    }
}

My Route / Web.php是:

   <?php

    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */

    //Route::get('/','SiteController@index');


    Route::get('/', function () {
        return view('welcome');
    });

    Route::get('/articles','ArticlesControllers@index');
    Route::get('/articles/create','ArticlesControllers@create');
    Route::get('/articles/{id}','ArticlesControllers@show');


    Route::post('/articles/store','ArticlesControllers@store');

我如何摆脱这个令人讨厌的问题,我一直在StackOverFlow上搜索,但几乎所有的答案都可能无法解决....

2 个答案:

答案 0 :(得分:0)

将其改为此以使其正常工作:

public function store(ArticleRequest $request)

答案 1 :(得分:0)

您已经在顶部导入了该课程

use App\Http\Requests\ArticleRequest

无需使用完整的类名作为参数 只需使用

public function store(ArticleRequest $request)

public function store(\App\Http\Requests\ArticleRequest $request)