Api: Cors not executed

Created on 18 May 2017  路  7Comments  路  Source: dingo/api

laravel 5.4
"dingo/api": "1.0.0-beta8",
'cors' =>\Barryvdh\Cors\HandleCors::class,
route/web.php
Route::group(['middleware' => 'cors'], function () { Route::get('users', function () { echo "浣犲ソ1"; }); });
it is ok

route/api.php
$api = app('Dingo\Api\Routing\Router'); $api->version('v1', ['middleware' => 'cors'], function ($api) { $api->get('users', function () { echo "浣犲ソ"; }); });
Cors not executed
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:8080' is therefore not allowed access

All 7 comments

When using Dingo and Lumen (might be true for Laravel also) you should extend HandleCors middleware and add this to handle method:

$this->events->listen(ResponseWasMorphed::class, function (ResponseWasMorphed $event) use ($cors, $request) {
            $cors->addActualRequestHeaders($event->response, $request);
        });

Also, I add my middleware class in config/api.php at middleware config

In response to @catalinux you can get this working in laravel (5.2) by creating a listener for the ResponseWasMorphed event or even add it to the boot method in the EventServiceProvider:

$events->listen(ResponseWasMorphed::class, function (ResponseWasMorphed $event) {
  app(CorsService::class)->addActualRequestHeaders($event->response, request());
});

@shijunti19,can you tell me how you solve this problem?
i add

protected $middlewareGroups = [
    'web' => [
       // ...
    ],

    'api' => [
        // ...
        \Barryvdh\Cors\HandleCors::class,
    ],
];

but cors isn't working.

@allen-hxn

config/api.php
'middleware' => [ 'Barryvdh\Cors\HandleCors' ],

Still doesn't work

<?php

namespace App\Http;

use Tymon\JWTAuth\Middleware\RefreshToken;
use Tymon\JWTAuth\Middleware\GetUserFromToken;
use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Barryvdh\Cors\HandleCors::class,
        \Barryvdh\Cors\HandlePreflight::class
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings'
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'cors'=>\Barryvdh\Cors\HandleCors::class,
        'pre'=>\Barryvdh\Cors\HandlePreflight::class,
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

        'jwt.auth' => GetUserFromToken::class,
        'jwt.refresh' => RefreshToken::class,

        'profile.maxTypes' =>\App\Http\Middleware\Profile\HasBothTypes::class,
        'job.employer' =>\App\Http\Middleware\User\Job\HasEmployerProfile::class,
        'job.freelancer' =>\App\Http\Middleware\User\Job\HasFreelancerProfile::class,
    ];
}

Unless am doing something wrong here

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yanguanglan picture yanguanglan  路  3Comments

cristiammercado picture cristiammercado  路  3Comments

sukh-gill picture sukh-gill  路  3Comments

jdforsythe picture jdforsythe  路  3Comments

Sogl picture Sogl  路  4Comments