config.yml:
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'
fos_rest:
routing_loader:
default_format: json
view:
view_response_listener: force
force_redirects:
html: true
format_listener:
rules:
- { path: '^/api', fallback_format: json }
- { path: '^/', fallback_format: html }
param_fetcher_listener: force
body_listener: true
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Symfony\Component\HttpKernel\Exception\NotFoundHttpException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
But html is returned:
...<h2><span>2/2</span> <abbr title="Symfony\Component\HttpKernel\Exception\NotFoundHttpException">NotFoundHttpException</abbr>: No route found for "GET /api/user/jFvms0rp%20"</h2>...
How to turn it to json?
I resolved the same problem :
this is my config :
fos_rest:
body_listener: true
view:
view_response_listener: force
format_listener:
rules:
- { path: '^/', fallback_format: json }
Hope it's helpful
Related
I have tried the symfony4 with fos bundle rest api versioning and it url routing is working , but api versioning is not working. Please find my below code and i have missed out anything.
My Url :
example.com/v2/api/guesttest
example.com/v1/api/guesttest
when we accessing the above urls always returning the same set of reults v1.
Routes.yml
guest_api:
path: /{version}/api/guesttest
controller: App\Controller\TestController::test1
condition: "request.attributes.get('version') == 'v1'"
requirements:
version: 'v1|v2'
fos_rest.yaml
fos_rest:
param_fetcher_listener: true
allowed_methods_listener: true
routing_loader:
include_format: false
body_listener: true
format_listener:
rules:
- { path: ^/api, prefer_extension: false, fallback_format: json, priorities: [ json ] }
- { path: ^/, prefer_extension: true, fallback_format: html, priorities: [ html ] }
versioning:
enabled: true
default_version: v1
TestController.php
/**
* #Version("v1")
* #NamePrefix("v1")
*
*/
class TestController extends FOSRestController
{
public function test1()
{
return new Response("success....!");
exit;
}
}
jms_serializer.yaml
jms_serializer:
visitors:
xml:
format_output: '%kernel.debug%'
metadata:
cache: file
debug: "%kernel.debug%"
file_cache:
dir: "%kernel.cache_dir%/serializer"
auto_detection: true
Reference links:
github
Symfony-verioning
I am trying to setup the symfony3 codebase with FOS REST Bundle and I end up getting no route found error
AppBundle\Resources\config\api-routing.yml
check:
type: rest
resource: "#AppBundle/Controller/CheckController.php"
app\config\routing.yml
app:
type: rest
prefix: /
resource: '#AppBundle/Resources/config/api-routing.yml'
controller
namespace AppBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\Version;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use Symfony\Component\HttpFoundation\Request;
/**
* #RouteResource("Check")
* #Version("v1")
*/
class CheckController extends FOSRestController
{
public function getAction(Request $request)
{
$data = "Ok";
return new JsonResponse($data);
}
}
http://127.0.0.1:8000/v1/app/check/ return no route
I would like to integrate the versioning of rest api route. Can you share what cud be the error and how can I correct this ?
fos_rest:
body_listener: true
versioning:
enabled: true
resolvers:
query:
enabled: true
parameter_name: version
format_listener:
rules:
- { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
param_fetcher_listener: true
view:
view_response_listener: 'force'
formats:
json: true
Here is working example.
app\config\routng.yml
app:
type: rest
resource: "#AppBundle/Resources/config/routing.yml"
prefix: /api
AppBundle\Resources\config\routing.yml
rest_my:
type: rest
prefix: /v1
resource: "#AppBundle/Controller/MyRestController.php"
name_prefix: api_1_
Active route will be:
http://127.0.0.1:8000/api/v1/<MyRestControllerAction>
or for second controller
AppBundle\Resources\config\routing.yml
rest_user:
type: rest
prefix: /v1/user
resource: "#AppBundle/Controller/UserRestController.php"
name_prefix: api_1_
Active route will be:
http://127.0.0.1:8000/api/v1/user/<UserRestControllerAction>
I hope I help you... Chears.
Update #1:
Here is my complete fos_rest configuration for json format into config.yml file:
fos_rest:
routing_loader:
default_format: json
include_format: false
body_converter:
enabled: true
body_listener:
decoders:
json: fos_rest.decoder.json
format_listener:
enabled: true
rules:
- { path: '^/api/v1/', priorities: ['json'], fallback_format: json, prefer_extension: true}
- { path: '^/', priorities: ['json', 'html'], fallback_format: 'html' }
serializer:
serialize_null: true
view:
view_response_listener: 'force'
templating_formats:
html: false
param_fetcher_listener: 'force'
If you need more then that you have full documentation for fos_rest
http://symfony.com/doc/current/bundles/FOSRestBundle/configuration-reference.html
I am developing an application using Symfony2 with fos-restbundle. I would like to create some API routes and also some regular routes (exactly one for AngularJS front-end). This is my fos_rest configuration (and a few configuration lines from sensio):
sensio_framework_extra:
view: { annotations: false }
router: { annotations: true }
request: { converters: true }
fos_rest:
routing_loader:
default_format: json
include_format: true
param_fetcher_listener: force
body_listener: true
allowed_methods_listener: true
view:
view_response_listener: 'force'
formats:
json: true
xml: true
format_listener:
rules:
- { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
access_denied_listener:
json: true
As you can see i have view_response_listener enabled and view annotations disabled. I can't find the way to define "regular" (not REST) route (and view) for index action (neccesary for AngularJS). Keep getting an error:
ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException: "No matching accepted Response format could be determined" at C:\wamp\www\CRMProject\vendor\friendsofsymfony\rest-bundle\EventListener\FormatListener.php line 69
I would appreciate any help with this.
You can add additional rule for your index page(for example):
format_listener:
rules:
- { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
- { path: '^/', priorities: [ 'text/html', '*/*'], fallback_format: html, prefer_extension: true }
Read docs about format listener: http://symfony.com/doc/current/bundles/FOSRestBundle/format_listener.html
As suggested in the official docs you can also disable the format listener for the "normal" part of the site (not the APIs):
Often when integrating this Bundle with existing applications, it
might be useful to disable the format listener for some routes. In
this case it is possible to define a rule that will stop the format
listener from determining a format by setting stop to true as a rule
option. Any rule containing this setting and any rule following will
not be considered and the Request format will remain unchanged.
# app/config/config.yml
fos_rest:
format_listener:
enabled: true
rules:
- { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: false }
- { path: '^/', stop: true } # Available for version >= 1.5
the problem I am having is that all the error pages I configure in fos_rest only work in debug mode.
FOS\RestBundle\Controller\ExceptionController
The exception controller finds my custom Twig error pages from http://symfony.com/doc/current/cookbook/controller/error_pages.html correctly only when debug is enabled.
When I however disable debug mode, I am getting an exception thrown in https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Controller/ExceptionController.php#L104
Twig is complaining that it's 404 even though it already figured out it's mapped.
My config:
fos_rest:
disable_csrf_role: ROLE_USER
routing_loader:
default_format: json
include_format: false
service:
exception_handler: fos_rest.view.exception_wrapper_handler
exception:
enabled: true
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Symfony\Component\HttpKernel\Exception\NotFoundHttpException': 404
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Symfony\Component\Security\Core\Exception\AccessDeniedException': 403
'Symfony\Component\Security\Core\Exception\DisabledException': 403
'Doctrine\DBAL\Exception\ServerException': 500
codes:
messages:
'Symfony\Component\Translation\Exception\NotFoundResourceException': false
'Symfony\Component\Security\Core\Exception\AccessDeniedException': true
'Symfony\Component\Security\Core\Exception\DisabledException': true
'Doctrine\DBAL\Exception\ServerException': false
body_listener: true
view:
view_response_listener: true
formats:
json: true
xml: false
html: true
templating_formats:
json: false
xml: false
html: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
format_listener:
rules:
- priorities: [json, html]
- fallback_format: json
- { path: '^/css', priorities: [ 'text/css', '*/*'], fallback_format: css, prefer_extension: true }
- { path: '^/api', priorities: [ 'json', 'text/html'], fallback_format: json, prefer_extension: false }
- { path: '^/', priorities: [ 'text/html', '*/*'], fallback_format: json, prefer_extension: false }
I've looked at everywhare but could not find a reference to this:
After configuring the HWIOAuthBundle and get a few problems solved I got the following error:
( ! ) Fatal error: Uncaught exception 'Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException' with message 'The service "hwi_oauth.resource_owner.google" has a dependency on a non-existent service "session".' in E:\Servidor\Wamp\wamp\www\DEVELOPMENT\magnetics\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass.php on line 64
I'm using Symfony 2.6 and here are my configuration file:
imports:
- { resource: parameters.yml }
- { resource: security.yml }
framework:
#esi: ~
translator: { fallback: %locale% }
secret: %secret%
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: %kernel.debug%
form: ~
csrf_protection: false
validation: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_proxies: ~
session: false
fragments: ~
hwi_oauth:
# name of the firewall in which this bundle is active, this setting MUST be set
firewall_name: secured_area
resource_owners:
google:
type: google
client_id: "977681365085-3pb"
client_secret: "0IvhJ-DL7"
scope: "https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
#options:
#access_type: offline
# Twig Configuration
twig:
debug: %kernel.debug%
strict_variables: %kernel.debug%
exception_controller: 'FOS\RestBundle\Controller\ExceptionController::showAction'
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
bundles: [ MagneticsAdminBundle ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: %kernel.root_dir%/Resources/java/compiler.jar
#yui_css:
# jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
# Doctrine Configuration
doctrine:
dbal:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: %kernel.root_dir%/data/data.db3
# path: %database_path%
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
# Swiftmailer Configuration
swiftmailer:
transport: %mailer_transport%
host: %mailer_host%
username: %mailer_user%
password: %mailer_password%
spool: { type: memory }
sensio_framework_extra:
view: { annotations: false }
fos_rest:
disable_csrf_role: ROLE_API
param_fetcher_listener: true
view:
mime_types:
json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1']
view_response_listener: 'force'
formats:
xml: true
json: true
templating_formats:
html: true
format_listener:
rules:
- { path: ^/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true }
media_type:
version_regex: '/(v|version)=(?P<version>[0-9\.]+)/'
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
allowed_methods_listener: true
access_denied_listener:
json: true
body_listener: true
fos_http_cache:
cache_control:
rules:
# the controls section values are used in a call to Response::setCache();
-
match:
path: ^/notes
methods: [GET, HEAD]
headers:
cache_control: { public: true, max_age: 15, s_maxage: 30 }
last_modified: "-1 hour"
vary: [Accept-Encoding, Accept-Language]
I've found similar problems but none of the solutions worked for me, anyone has an idea?
You have sessions disabled in your config
session: false
I don't know this for sure, but I'm going to guess that the session service isn't created when sessions are disabled at the framework level.
You can check by debugging the container
$ php app/console container:debug
Or more targeted
$ php app/console container:debug session