My Request Class Exists - But I Keep Being Told That There's No Such Thing! Why?

Bruce Gust
Bruce Gust used Ask the Experts™
on
Here's my Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Page;
use App\Http\Resources\Page as PageResource; //for Vue.js display

class PageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
	 
	 public function __construct() {
        $this->middleware('checkRole: admin');
    }
	
	public function insertPage() {
        return view('admin.insertPage');
    }
		
	
    public function index()
    {
        //get pages
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreatePage $request)
    {
        //
		$page = $request->isMethod('put') ? Page::findOrFail($request->page_id) : new Page; //ternary IF statement determining whether or not this is a new Page or not
		
		$page->id = $request->input('page_id');
		$page->title=$request->input('title');
		$page->body=$request->input('body');
		
		if($page->save()) {
			//return new PageResource($page);
			return redirect('/display/page/{id}')->with('success', 'Page has been successfully created!');
		}
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
		$page=Page::findOrFail($id);
		return new PageResource($page);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Open in new window


Here's my CreatePage Request:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CreatePage 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 [
            'name' => 'required', 
			'body' => 'required'
        ];
    }
}

Open in new window


I keep getting this error:

ReflectionException
Class App\Http\Controllers\CreatePage does not exist
http://nomas.site/admin/create/page

I've been google-ing for the last half hour and I can't find an answer.

What am I missing?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Analyst Developer
Distinguished Expert 2018
Commented:
You need to import the class in your controller like :

use App\Http\Requests\CreatePage;

Else the compiler will think that the class is in the same directory of the current file (the controller), So that why it is looking for the class in the path: "App\Http\Controllers\CreatePage".

Sample Inside the code :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Page;
use App\Http\Resources\Page as PageResource; //for Vue.js display
use App\Http\Requests\CreatePage;

class PageController extends Controller
....

Open in new window

Bruce GustPHP Developer

Author

Commented:
Thank you Zakaria! Since posting this question I was able to figure it out but you reinforced that discovery!

Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial