diff --git a/api-payroll/composer.json b/api-payroll/composer.json index a2aa1c6..e22099e 100644 --- a/api-payroll/composer.json +++ b/api-payroll/composer.json @@ -16,7 +16,8 @@ "slim/slim": "^3.1", "slim/php-view": "^2.0", "monolog/monolog": "^1.17", - "respect/validation": "^1.1" + "respect/validation": "^1.1", + "tuupola/cors-middleware": "^0.5.2" }, "require-dev": { "phpunit/phpunit": ">=4.8 < 6.0" diff --git a/api-payroll/composer.lock b/api-payroll/composer.lock index f615ac9..467315f 100644 --- a/api-payroll/composer.lock +++ b/api-payroll/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "677ee7ce2e986cfa3ab0df77d78e0a4c", - "content-hash": "fddce0c9f8dd9b23d45f6d6e4b4b6310", + "hash": "93a9656f4e6eb0e25be1bad59ac6f487", + "content-hash": "a3fc18885cc45d2733b77fa2081bdc72", "packages": [ { "name": "container-interop/container-interop", @@ -116,6 +116,61 @@ ], "time": "2017-06-19 01:22:40" }, + { + "name": "neomerx/cors-psr7", + "version": "v1.0.13", + "source": { + "type": "git", + "url": "https://github.com/neomerx/cors-psr7.git", + "reference": "2556e2013f16a55532c95928455257d5b6bbc6e2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/neomerx/cors-psr7/zipball/2556e2013f16a55532c95928455257d5b6bbc6e2", + "reference": "2556e2013f16a55532c95928455257d5b6bbc6e2", + "shasum": "" + }, + "require": { + "php": ">=5.6.0", + "psr/http-message": "^1.0", + "psr/log": "^1.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "phpunit/phpunit": "^5.7", + "scrutinizer/ocular": "^1.1", + "squizlabs/php_codesniffer": "^3.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Neomerx\\Cors\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "neomerx", + "email": "info@neomerx.com" + } + ], + "description": "Framework agnostic (PSR-7) CORS implementation (www.w3.org/TR/cors/)", + "homepage": "https://github.com/neomerx/cors-psr7", + "keywords": [ + "Cross Origin Resource Sharing", + "Cross-Origin Resource Sharing", + "cors", + "neomerx", + "psr-7", + "psr7", + "w3.org", + "www.w3.org" + ], + "time": "2018-05-23 16:10:11" + }, { "name": "nikic/fast-route", "version": "v1.3.0", @@ -599,6 +654,56 @@ "shim" ], "time": "2018-04-26 10:06:28" + }, + { + "name": "tuupola/cors-middleware", + "version": "0.5.2", + "source": { + "type": "git", + "url": "https://github.com/tuupola/cors-middleware.git", + "reference": "db69d8e67b99570b16e8cd5f78c423ed1167cb21" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tuupola/cors-middleware/zipball/db69d8e67b99570b16e8cd5f78c423ed1167cb21", + "reference": "db69d8e67b99570b16e8cd5f78c423ed1167cb21", + "shasum": "" + }, + "require": { + "neomerx/cors-psr7": "^1.0", + "php": "^5.5 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.8", + "squizlabs/php_codesniffer": "^2.5", + "zendframework/zend-diactoros": "^1.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Tuupola\\Middleware\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mika Tuupola", + "email": "tuupola@appelsiini.net", + "homepage": "http://www.appelsiini.net/", + "role": "Developer" + } + ], + "description": "PSR-7 CORS Middleware", + "homepage": "https://github.com/tuupola/cors-middleware", + "keywords": [ + "cors", + "middleware", + "slim" + ], + "time": "2016-08-12 13:12:58" } ], "packages-dev": [ diff --git a/api-payroll/src/middleware.php b/api-payroll/src/middleware.php index 116a0ff..6898ac4 100644 --- a/api-payroll/src/middleware.php +++ b/api-payroll/src/middleware.php @@ -2,3 +2,21 @@ // Application middleware // e.g: $app->add(new \Slim\Csrf\Guard); + +// Enable cors +$app->add(new \Tuupola\Middleware\Cors([ + "origin" => ["*"], + "methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"], + "headers.allow" => ["Accept", "Content-Type"], + "headers.expose" => [], + "credentials" => false, + "cache" => 0, + "logger" => $container['logger'], + "error" => function ($request, $response, $arguments) { + $data["status"] = "error"; + $data["message"] = $arguments["message"]; + return $response + ->withHeader("Content-Type", "application/json") + ->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); + } +])); \ No newline at end of file