Код: Выделить всё
$routes->plugin(
'Ark',
['path' => '/ark'],
function (RouteBuilder $routes)
{
$routes->setRouteClass(DashedRoute::class);
$routes->registerMiddleware('cors', function($req, $res, $next) {
$lAllowed = [
'localhost:8081',
'localhost:8082',
'localhost:8087',
'172.16.1.225',
'172.16.1.225:8081',
'172.16.1.225:8082',
'172.16.1.225:8087',
'172.16.1.224',
];
if( $sOrigin = $req->getHeader('origin') )
{
$sOrigin = reset($sOrigin);
$sOrigin = str_replace('https://', '', $sOrigin);
$sOrigin = str_replace('http://', '', $sOrigin);
}
if( empty($sOrigin) )
$sOrigin = $req->host();
/** @var \Cake\Http\ServerRequest $req */
/** @var \Cake\Http\Response $res */
if( in_array($sOrigin, $lAllowed) )
{
//debug( 'Allow' );
$res = $res->cors($req)
->allowOrigin($req->getHeader('origin')) //only one host should be allowed when allow credentials is true
->allowMethods(['GET', 'OPTIONS'])
->allowHeaders(['Content-Type', 'X-CSRF-Token', 'Authorization'])
->allowCredentials()
->maxAge(3600) //1h
->build();
//return immediately for CORS requests
if ( strtoupper($req->getMethod()) === 'OPTIONS' )
{
return $res->withStatus(200, 'You shall pass!!');
}
//debug( $res );
}
return $next($req, $res);
});
$routes->prefix('Api', ['path' => '/api'], function(RouteBuilder $route) {
// Parse specified extensions from URLs
$route->setExtensions(['json']);
//allow external services use this api
$route->applyMiddleware('cors');
$route->prefix('V1', ['path' => '/v1'], function(RouteBuilder $route) {
// Translates to `Controller\Api\V1\` namespace
$lListOfResources = [
//Table names...
];
foreach ($lListOfResources as $sResourceName)
{
$route->resources($sResourceName, [
'map' => [
':id/restore' => ['action' => 'restore', 'method' => ['PUT', 'PATCH']],
'list' => ['action' => 'list', 'method' => 'GET'],
'filter/*' => ['action' => 'filter', 'method' => 'GET'],
]
]);
}
$route->fallbacks();
});
});
//default landing page
$routes->connect('/', ['controller' => 'Pages']);
}
);
Должен ли я добавить логику CORS внутри контроллеров перед фильтром?
cakephp 4.2.12
php 8.0.1
Подробнее здесь: https://stackoverflow.com/questions/769 ... ve-headers