Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Laravel中间件适用于单个路由而不适用于路由组_Laravel_Laravel 5_Cross Domain_Cors_Middleware - Fatal编程技术网

为什么Laravel中间件适用于单个路由而不适用于路由组

为什么Laravel中间件适用于单个路由而不适用于路由组,laravel,laravel-5,cross-domain,cors,middleware,Laravel,Laravel 5,Cross Domain,Cors,Middleware,我添加了一个CORS中间件,它可以很好地用于单路由:- Route::get('example', ['middleware' => 'cors', function(){ return Response::json(array('name' => 'Steve Jobs', 'company' => 'Apple')); }]); Route::group(['middleware' => 'cors'], function () { Route::group

我添加了一个CORS中间件,它可以很好地用于单路由:-

Route::get('example', ['middleware' => 'cors', function(){
    return Response::json(array('name' => 'Steve Jobs', 'company' => 'Apple'));
}]);
Route::group(['middleware' => 'cors'], function () {
Route::group(['prefix' => 'api'], function()
{
    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
});
});
但当我将其应用于路由组时,控制台中会出现错误:-

(index):1 XMLHttpRequest cannot load http://jotdot.mysite.com/api/authenticate. 
Response to preflight request doesn't pass access control 
check: No 'Access-Control-Allow-Origin' header is present on 
the requested  resource. Origin 'http://jotdotfrontend.mysite.com' is
therefore not allowed access.
我的路线组:-

Route::get('example', ['middleware' => 'cors', function(){
    return Response::json(array('name' => 'Steve Jobs', 'company' => 'Apple'));
}]);
Route::group(['middleware' => 'cors'], function () {
Route::group(['prefix' => 'api'], function()
{
    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
});
});
Cors.php

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;
    return $next($request);
}
}
我正在跟踪并尝试在不同的域上设置后端和前端

谢谢你可以试试这个:

Route::group(['prefix' => 'api', 'middleware' => 'cors'], function()
{
    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
});
试着这样,

Route::group([ 'prefix' => 'api', 'middleware' => 'App\Http\Middleware\CORS'], function() {
     Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
     Route::post('authenticate', 'AuthenticateController@authenticate');

});

您的原始标头不在标头数组中。不要使用header()设置原始标头,而是尝试将其与其他访问控制标头一起添加,如下所示:

// ALLOW OPTIONS METHOD
$headers = [
    'Access-Control-Allow-Origin'=> '*',
    'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
    'Access-Control-Allow-Headers'=> 'Content-Type, X-Auth-Token, Origin'
];

问题是基于路由的中间件从未应用于自动生成的基于
资源的
路由的
选项
请求('原因
路由::资源(…)
不为
选项
方法生成路由?)

因此,将您的
cors
中间件放在
app/Http/Kernel.php
的全局中间件组中,就在
VerifyCsrfToken
中间件之前:

/**
 * The application's global HTTP middleware stack.
 *
 * @var array
 */
protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Ankh\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\CORS::class, // here it is
    \App\Http\Middleware\VerifyCsrfToken::class,
];
并将其从
routes.php
中完全删除,如:

// Route::group(['middleware' => 'cors'], function () {
Route::group(['prefix' => 'api'], function() {
    Route::resource('authenticate', 'AuthenticateController', ['only' => ['index']]);
    Route::post('authenticate', 'AuthenticateController@authenticate');
});
//});

如何将中间件添加到组内的各个路由?@StacyJ我认为问题在于,当您尝试发送POST请求时,Laravel将验证
CsrfToken
。看一看。您需要在
第20行
Kernel.php
文件中禁用此中间件来解决此问题。