pier67's avatar

Problem "ReflectionException" class does not exist

Im getting this error and i have previously defined the class on the controller and also on the http requests and i'm still getting this error, i really don't kow what do to, im stuck

0 likes
25 replies
jlrdw's avatar

Maybe you need to run:

composer dump-autoload
pier67's avatar

I've been running dump-autoload and still nothing

pier67's avatar
  • In my controller file i have this:
namespace App\Http\Controllers;

use App\Client;
use Illuminate\Http\Requests;
use App\Http\Requests\SaveClientRequest;
  • In my Request file i have this:
namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class SaveClientRequest extends FormRequest
  • in my route i have this:
Route::resource('clients', 'ClientsController')
    ->parameters(['clientes' => 'client'])
    ->names('clients');
  • I have created all with the artisan commands

  • A weird thing was that i tried to use another class that actually works but using different data and the same error appeared "ReflectionException Class App\Http\Requests\SaveProjectRequest does not exist"

You can notice that i changed the file there and the error is the same

jlrdw's avatar

Have you made sure that route is hitting that controller by using a dd or an echo.

Snapey's avatar

Tell us WHAT file you get the error in and WHAT you were doing at the time.

Please format your code by putting 3 backticks ``` on a line before and after each code block

pier67's avatar

Didn't try that yet, but the route source is working because i just can't insert or update the data

pier67's avatar

I got the error in this file

use App\Http\Requests\SaveClientRequest;

So the class is created here

namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class SaveClientRequest extends FormRequest

I'm also using the class on the controller

public function store(SaveClientRequest $request)
    {
        Client::create($request->validated() );

        return redirect()->route('clients.index')->with('status','El cliente fue creado con éxito');
    }

And

public function update(Client $Client, SaveClientRequest $request)
    {
         $client->update($request->validated() );

        return redirect()->route('clients.show', $project)->with('status','El Cliente fue actualizado con éxito');
    }

My composer.json file is like this

 "autoload": {
        "psr-4": {
            "App\": "app/"
        },

And the Route::source

Route::resource('clients', 'ClientsController')
    ->parameters(['clientes' => 'client'])
    ->names('clients');

So basically i can't add or update using the "SaveClienteRequest" which is a FormRequest

A weird problem is that i tried with another FormRequest that is actually working (insert and update works great there) so i just changed the paremeters and i got the same error "ReflectionException Class App\Http\Requests\SaveProjectRequest does not exist" you can notice that the class is different here and this class is working perfectly in another controller.

Pd: Sorry for not using the format code before.

Sinnbeck's avatar

My guess is that you have a syntax error in the SaveClientRequest file, or the name of the file isn't SaveClientRequest.php

A syntax error means a missing ; or }. If you post the full code for it, we can check

pier67's avatar

Full syntax of SaveClientRequest

<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rule;
use Illuminate\Foundation\Http\FormRequest;

class SaveClientRequest extends FormRequest
{

   public function authorize()
   {
       return true;
   }

   public function rules()
   {
       return [
           'dui' -> ['required', Rule::unique('clients')->ignore( $this->route('client') )
           ],
           'nombre' -> 'required',
           'apellido' -> 'required',
           'direccion' -> 'required',
           'telefono' -> 'required'
       ];
   }

   public function message()
   {
       return [
           'dui.required' => 'El cliente necesita un dui',
           'nombre.required' => 'El cliente necesita un nombre',
           'apellido.required' => 'El cliente necesita un apellido',
           'direccion.required' => 'El cliente necesita una dirección',
           'telefono.required' => 'El cliente necesita un teléfono'
       ];
   }
}
Sinnbeck's avatar

Looks good. And the path + filename to the file? app/Http..?

pier67's avatar

This is the path

use App\Http\Requests\SaveClientRequest;
Sinnbeck's avatar

I am talking about the actual filename and path on the computer (from root directory of laravel)

pier67's avatar
C:\laragon\www\laravel\app\Http\Requests\SaveClientRequest.php

Where i also have the other class that is working

C:\laragon\www\laravel\app\Http\Requests\SaveProjectRequest.php
pier67's avatar

I went to the laravel.log after the error and i found this

[2020-02-27 16:06:26] local.ERROR: Class App\Http\Requests\SaveClientRequest does not exist {"userId":1,"exception":"[object] (ReflectionException(code: 0): Class App\Http\Requests\SaveClientRequest does not exist at C:\laragon\www\laravel\vendor\laravel\framework\src\Illuminate\Routing\RouteSignatureParameters.php:25)
[stacktrace]
jlrdw's avatar

After making this file, you did run

 composer dumpautoload

I don't see a mis-spelling anywhere.

pier67's avatar

Still getting this error

ReflectionException Class App\Http\Requests\SaveClientRequest does not exist

And yes i run that command after creation of the file, actually both, the controller and the class

ajithlal's avatar
ajithlal
Best Answer
Level 18

@sinnbeck is correct. You have a syntax error on your file. @pier67 please rewrite your rules() function. you are using -> instead of =>.

 public function rules()
   {
       return [
           'dui' => ['required', Rule::unique('clients')->ignore( $this->route('client') )
           ],
           'nombre' => 'required',
           'apellido' => 'required',
           'direccion' => 'required',
           'telefono' => 'required'
       ];
   }

Hope this works.

Sinnbeck's avatar

@ajithlal nice catch. Didn't have a code editor (IDE) to test with, only my eyes. A good reason to use an IDE when coding

1 like
pier67's avatar

Thank you so much it works now!

pier67's avatar

Thanks for the recommendation!

erickBanda's avatar

guys I know it´s been a long time since the error happened, but this is a way I found to solve the issue

"Route::apiResource('contactos', \App\Http\Controllers\ContactoController::class);"

I just added the complete route of my controller in the api.php

Please or to participate in this conversation.