“Корс Ларавел” Ответ

Laravel 5,8 cors

Create a simple middleware called Cors:
php artisan make:middleware Cors
  
Add the following code to app/Http/Middleware/Cors.php:

public function handle($request, Closure $next)
{
    return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
        ->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Authorization, X-Request-With');
}
You can replace the * with localhost or keep it as it is.

Next step is to load the middleware. Add the following line to the $routeMiddleware array in app/Http/Kernel.php.

'cors' => \App\Http\Middleware\Cors::class, 
And the final step is to use the middleware on the routes to which you want to set the access origin headers. Assuming you are talking about the new api routes in laravel 5.3, the place to do it is app/Providers/RouteServiceProvider.php, inside the mapApiRoutes() function (you can remove or comment the previous code of the function):

    Route::group([
        'middleware' => ['api', 'cors'],
        'namespace' => $this->namespace,
        'prefix' => 'api',
    ], function ($router) {
         //Add you routes here, for example:
         Route::apiResource('/posts','PostController');
    });
Alemhar

Laravel-Cors

composer require fruitcake/laravel-cors
Krissanawat

Laravel Cors включает

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
Shadow

Корс Ларавел

READ: https://laravelquestions.com/2021/02/17/how-to-disable-cors-for-laravel-storage-urls-using-fetch-api/
-------
I have a Laravel backend project and multiple ReactJS applications accessing to it through API calls. I have no problem with those because I configured config/cors.php to recieve requests to ‘api/‘ from any origin. Also tried to add ‘storage/‘ to paths, but it didn’t work:

'paths' => ['api/*', 'storage/*'],

'allowed_methods' => ['*'],

'allowed_origins' => ['*'],

'allowed_origins_patterns' => [],

'allowed_headers' => ['*'],

'exposed_headers' => ['*'],

'max_age' => 0,

'supports_credentials' => false,
I’m using axios to perform the api calls.

My problem is that I’m using Material-UI Dropzone and it uses ‘fetch API’ to get the images from any link. I tried to use a facebook URL and it worked!

Fetch facebook image

But when I try to fetch an image that is on Laravel Storage it gives me this error::

Screenshot of the request to laravel storage

I’ve already link the ‘storage’ to ‘public’ with ‘php artisan storage:link’, everything works fine. The image is returned when I try to access the URL through the browser.

I tried adding {mode: ‘no-cors’} to the fetch method and it returns the image on the request! But doesn’t load it on the component:

Image fetched with no-cors but not shown in dropzone

Why Facebook images are retrieved correctly but not my images from the storage?

Valentino_Rossi

Ответы похожие на “Корс Ларавел”

Вопросы похожие на “Корс Ларавел”

Больше похожих ответов на “Корс Ларавел” по PHP

Смотреть популярные ответы по языку

Смотреть другие языки программирования