Hi I would like to version my API. So I would like t prefix my roote with a version. like that?
# config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
prefix:
name: '{version}'
It works bit I would like to add a requirement to check the version.
Like:
requirements:
version: v1|v2
I don't find any documentation talking about that. Is there a good way to do it?
In the FOSRestBundle there is a complete chapter about versioning.
https://symfony.com/doc/master/bundles/FOSRestBundle/versioning.html
The FOSBundle has an option for versioning.
#app/config/config.yml
fos_rest:
versioning:
enabled: true
resolvers:
query: true # Query parameter: /users?version=v1
custom_header: true # X-Accept-Version header
media_type: # Accept header
enabled: true
regex: '/(v|version)=(?P<version>[0-9\.]+)/'
Related
I upgraded my FOSUserBundle to 2.4 from 2.1 while I was upgrading my project to Symfony 3.4 from 2.8.
With the same code that worked before, and this yml file:
# app/config/routing.yml
api_request_backend:
type: rest
prefix: /api
resource: "#AppBundle/Resources/config/default.yml"
-
# AppBundle/Resources/config/default.yml
api:
type: rest # This resource will have RESTful routes
prefix:
resource: "#AppBundle\Controller\ApiController"
name_prefix: api_
apiV2:
type: rest # This resource will have RESTful routes
prefix: /v2
resource: "#AppBundle\Controller\ApiV2Controller"
name_prefix: api_v2_
api_user:
type: rest # This resource will have RESTful routes
prefix:
resource: "#AppBundle\Controller\ApiUserController"
name_prefix: api_
I receive this error:
Exception thrown when handling an exception
(Symfony\Component\Config\Exception\FileLoaderLoadException: The file
"/var/www/project/src/AppBundle/Resources/config/default.yml" does
not contain valid YAML in
/var/www/project/src/AppBundle/Resources/config/default.yml (which
is being imported from "/var/www/project/app/config/routing.yml").
Make sure there is a loader supporting the "rest" type.)
Where am I wrong? I also tried to downgrade FOSRestBundle to 2.3.1 (I read this here) but nothing changes.
For newest (>3.0) you must change the route type from rest to annotation.
The issue is the invalid YAML. The following works:
# app/config/routing.yml
api_request_backend:
type: rest
prefix: /api
resource: '#AppBundle/Resources/config/default.yml'
and
# AppBundle/Resources/config/default.yml
api:
type: rest # This resource will have RESTful routes
resource: '#AppBundle\Controller\ApiController'
name_prefix: api_
thanks to xabbuh for the fix
I need some help, I was working with mysql and doctrine and all was perfect, but now I'm using Auroradb which uses two instances (reader and writer).
At first I tried to use two entity managers, one for writing and other for reading, but I got a problem with SyliusRbacBundle.
so, is there another way to use aurora and doctrine?????
UPDATE 1
this is the error that I get after using Daniel's config
A new entity was found through the relationship 'Litigon\UserBundle\Entity\User#authorizationRoles' that was not configured to cascade persist operations for entity: SuperAdministrador. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example #ManyToOne(..,cascade={"persist"}).
so, if I merge the default entity manager as a lot of people suggest, I get problems with aurora 'cause of the other manager is for the reader instance and then when flushing aurora says that isn't allowed to write.
You need to specify where the models or entities actually live in doctrine config, also is important to notice that Sylius models are usually located on the component and not in the bundle. Finally, but not least important, can only have one connection with auto mapping:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
loggable:
type: annotation
alias: Gedmo
prefix: Gedmo\Loggable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
FOSUserBundle:
type: xml
dir: Resources/config/doctrine-mapping
prefix: FOS\UserBundle\Model
SyliusRbacBundle:
type: xml
dir: Resources/config/doctrine/model
prefix: Sylius\Component\Rbac\Model
SyliusResourceBundle: ~
OtherBundle: ~
writer:
connection: writer
mappings:
loggable:
type: annotation
alias: Gedmo
prefix: Gedmo\Loggable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity"
FOSUserBundle:
type: xml
dir: Resources/config/doctrine-mapping
prefix: FOS\UserBundle\Model
SyliusRbacBundle:
type: xml
dir: Resources/config/doctrine/model
prefix: Sylius\Component\Rbac\Model
SyliusResourceBundle: ~
I've installed the NelmioApiDocBundle for my new API-oriented project in Symfony and I can't get rid of the .{_format} suffix that this bundle adds to all my endpoint URLs.
This is how it looks:
My API does not support the _format as suffix. It does support it as a query parameter or by request headers. Because of that, if I try to do a request to this endpoint from the NelmioApiDocBundle sandbox, it gets a 404 error response.
This is my current config.yml section regarding nelmio:
yml
nelmio_api_doc:
name: My API doc
sandbox:
enabled: true
endpoint: null
accept_type: application/json
body_format:
formats: null
default_format: json
request_format:
formats:
json: application/json
xml: null
method: accept_header
default_format: json
authentication:
name: bearer
delivery: query
cache:
enabled: false
file: '%kernel.cache_dir%/api-doc.cache'
I've figured it out elsewhere on stackoverflow. It seems that my problem was not related to NelmioApiDocBundle, but to FOSRestBundle. I've had to change only one FOSRest setting in config.yml:
fos_rest:
routing_loader:
include_format: false
I've found the solution here
I am using the current version of Symfony and want to define a route with a specific subdomain. This is what I have:
xyz:
resource: "#XYZ/Controller/"
type: annotation
prefix: /
host: "xyz.symfony.dev"
I want to have symfony.dev variable, only xyz as a subdomain is a static value. How can I realize this? I don't get it in the documentation.
I'm not good with regex but this answer your question:
xyz:
resource: "#XYZ/Controller/"
type: annotation
prefix: /
host: "xyz.{domain}.{ltd}"
requirements:
domain: "([a-z0-9-])+"
ltd: "([a-z])+"
EDIT: Regex added
I want to handle the diferents versions of a resource with the request header 'Accept'
===>
GET /customer/123 HTTP/1.1
Accept: application/vnd.company.myapp.customer-v3+json
<===
HTTP/1.1 200 OK
Content-Type: application/vnd.company.myapp-v3+json
{"customer":
{"name":"Neil Armstrong"}
}
I want to read the Accept header, extract the version and initialize the serializer with this version!
This is possible with FOSRestBundle?
I know I can listen kernel.request, inspect the 'Accept' header and extract the requested version from the MIME type but I donĀ“t know how to set the serializer with this value...
I also read FOSRestBundle doc and I know that you can add new mime types in config:
view.mime_types: {'json': ['application/vnd.company.myapp.customer-v1+xml', 'application/vnd.company.myapp.customer-v2+xml']}
I need to add here all the possible mime types (with different versions)?
Any help will be appreciated!
This won't help you for the time being, but there are opened issues regarding API Versioning.
https://github.com/FriendsOfSymfony/FOSRestBundle/pull/632
This is now possible, see the documentation.
Your config.yml file would look something like this (use whatever regex you'd like).
fos_rest:
versioning:
enabled: true
resolvers:
media_type: # Accept header
enabled: true
regex: '/v(?P<version>[0-9\.]+)\+(json|xml)/'
guessing_order:
- media_type # Not needed if there's only one
format_listener: # Must enable format_listener for versioning in Accept headers
enabled: true
rules: # Catch all routes to use this rule
- { path: '*', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: false }
view:
mime_types: # If there are lots of versions, can make this more dynamic. Check docs.
json: ['application/vnd.company.myapp.customer-v1+xml', 'application/vnd.company.myapp.customer-v2+xml', 'application/vnd.company.myapp.customer-v3+json']