Symfony 5 app serving as an API platform built with NelmioApiDocBundle.
- Install packages
composer install
- Start the server at http://localhost:8888/
./start_server.sh
- Interactive API documentation (Swagger UI) available at http://localhost:8888/api/doc
Use Event Subscribers for setting up before/after filters.
Authentication should be implemented in the middleware. Use JWT tokens sent as a Bearer
in the header or hidden inside a cookie.
In the middleware check for the referrer
of the request whether it matches allowed URL addresses specified in the CORS_ALLOW_ORIGIN
property located either in /public/.htaccess
' or in /config/packages/nelmio_cors.yaml
.
Handle exceptions using Event Listeners.
Enable allow_credentials
if you are sending JWT tokens in the header or as a cookie.
/config/packages/nelmio_cors.yaml
:
nelmio_cors:
defaults:
origin_regex: true
allow_credentials: true
# allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
allow_origin: ['^https://(domain1.com|domain2.com)$']
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['Content-Type', 'Authorization']
expose_headers: ['Link']
max_age: 3600
paths:
'^/': null
After installation symfony/apache-pack
package creates a .htaccess
file inside your /public
folder.
.htaccess
should always contain these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(pdf|js|ico|gif|jpg|png|css|rar|zip|tar|svg\.gz)$ index.php [L]
Expose your documentation as both JSON and YAML swagger compliant - /config/packages/nelmio_api_doc.yaml
:
app.swagger_json:
path: /api/doc.json
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger_json }
app.yaml_yaml:
path: /api/doc.yaml
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger_yaml }
Latest NelmioApiDocBundle (v4.4) uses Swagger-PHP Annotation which is based on OpenApi v^3.0 specs.
Caching can be disabled in /config/services.yaml
:
framework:
cache:
app: cache.adapter.null
system: cache.adapter.null
services:
cache.adapter.null:
class: Symfony\Component\Cache\Adapter\NullAdapter
arguments: [~] # a trick to avoid arguments errors on compile-time
Probably a cache problem. Empty the /var/cache
folder.