Class does not exist CORS Standard

Asked

Viewed 221 times

0

I’m trying to implement a Cors middleware on the Laravel, but you’re saying my class doesn’t exist:

"message": "Class App Http Middleware CORS does not exist", "Exception": "Reflectionexception", "file": "/var/www/repositorio/jcontrole2_backend/vendor/Laravel/framework/src/Illuminate/Container/Container.php", "line": 767,

I created this CORS.php file in App/Http/Middleware/Cors.php:

use Closure;

class CORS {

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        header("Access-Control-Allow-Origin: *");

        // ALLOW OPTIONS METHOD
        $headers = [
            'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Headers'=> 'Content-Type, X-Auth-Token, Origin'
        ];
        if($request->getMethod() == "OPTIONS") {
            // The client-side application can set only headers allowed in Access-Control-Allow-Headers
            return Response::make('OK', 200, $headers);
        }

        $response = $next($request);
        foreach($headers as $key => $value)
            $response->header($key, $value);
        return $response;
    }

}

In my kernel.php:

protected $routeMiddleware = [
        'cors' => 'App\Http\Middleware\CORS'
];

And then on my routes I tried to apply this middleware on a group of routes:

$this->group(['middleware' => 'cors', ['prefix' => 'api']], function() {
  Route::post('admin/tipo-produto', 'TipoProdutosController@create')->name('cadastrar_tipo_produto')->middleware('checarPermissaoTela');

  Route::put('admin/tipo-produto/{id}', 'TipoProdutosController@update')->name('editar_tipo_produto')->middleware('checarPermissaoTela');

  Route::delete('admin/tipo-produto/{id}', 'TipoProdutosController@delete')->name('deletar_tipo_produto')->middleware('checarPermissaoTela');

  Route::get('admin/tipo-produto', 'TipoProdutosController@search')->name('recuperar_tipos_produtos');
});

  • As you have created a new class, it may be that Laravel is not "seeing" this class, at the root of your project try to execute the following commands: Dump-autoload or php Artisan dump-autoload they must update.

  • when I gave Popup dump-autoload returned Parse error: syntax error, Unexpected '?' in C: Users jsoftwares1 Desktop backend vendor Laravel framework src Illuminate Foundation helpers.php on line 242

  • Gave this error but it worked, you want to add your comment as response?

  • I added my message in response, I’m glad it worked.

  • Although my middleware keeps giving Cors error, this answer fixed the class problem not found

  • Create a new question about the error that is occurring informing the version of Laravel, the error and its source code, I believe that surely someone must have passed by it

Show 1 more comment

1 answer

2


As you have created a new class, it may be that Laravel is not "seeing" this class, at the root of your project try to execute the following commands:

Dump-autoload

or

php Artisan dump-autoload

they should update your project’s class mapping file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.