Skip to content

Commit 595fa6f

Browse files
committed
Allow origin frontend
1 parent b879fc6 commit 595fa6f

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
"ext-dom": "*",
5151
"tatevikgr/rss-feed": "dev-main as 0.1.0",
5252
"psr/simple-cache": "^3.0",
53-
"symfony/expression-language": "^6.4"
53+
"symfony/expression-language": "^6.4",
54+
"nelmio/cors-bundle": "^2.4"
5455
},
5556
"require-dev": {
5657
"phpunit/phpunit": "^10.0",

src/DependencyInjection/PhpListRestExtension.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use InvalidArgumentException;
99
use Symfony\Component\Config\FileLocator;
1010
use Symfony\Component\DependencyInjection\ContainerBuilder;
11+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1112
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1213
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1314

@@ -16,7 +17,7 @@
1617
*
1718
* @author Oliver Klee <oliver@phplist.com>
1819
*/
19-
class PhpListRestExtension extends Extension
20+
class PhpListRestExtension extends Extension implements PrependExtensionInterface
2021
{
2122
/**
2223
* Loads a specific configuration.
@@ -35,4 +36,34 @@ public function load(array $configs, ContainerBuilder $container): void
3536
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
3637
$loader->load('services.yml');
3738
}
39+
40+
/**
41+
* @param ContainerBuilder $container
42+
*
43+
* @return void
44+
*/
45+
public function prepend(ContainerBuilder $container): void
46+
{
47+
$frontendBaseUrl = $container->getParameter('app.frontend_base_url');
48+
49+
$container->prependExtensionConfig('nelmio_cors', [
50+
'defaults' => [
51+
'allow_origin' => [$frontendBaseUrl],
52+
'allow_methods' => ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
53+
'allow_headers' => ['Content-Type', 'Authorization', 'Origin', 'Accept', 'php-auth-pw'],
54+
'expose_headers' => ['X-Content-Type-Options', 'Content-Security-Policy', 'X-Frame-Options'],
55+
'max_age' => 3600,
56+
],
57+
'paths' => [
58+
'^/api/v2' => [
59+
'origin_regex' => true,
60+
'allow_origin' => [$frontendBaseUrl],
61+
'allow_methods' => ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
62+
'allow_headers' => ['Content-Type', 'Authorization', 'Origin', 'Accept', 'php-auth-pw'],
63+
'expose_headers' => ['X-Content-Type-Options', 'Content-Security-Policy', 'X-Frame-Options'],
64+
'max_age' => 3600,
65+
],
66+
],
67+
]);
68+
}
3869
}

0 commit comments

Comments
 (0)