Symfony with combination of Basic Routing and REST - symfony

My existing application requires additional RESTful endpoints.
I've added FOSRestBundle with the following configuration.
fos_rest:
param_fetcher_listener: true
view:
mime_types:
json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1', 'application/json;version=1.2']
view_response_listener: 'force'
formats:
xml: false
json: true
templating_formats:
html: false
format_listener:
rules:
- priorities: [json, xml]
- fallback_format: json
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
routing_loader:
default_format: json
include_format: false
The REST requests are fine but the basic routing now doesn't work
With the following error
This page contains the following errors:
error on line 11 at column 92: EntityRef: expecting ';'
Below is a rendering of the page up to the first error.
Basically I just want to combine the basic routing with rest

You can solve this by disabling the format listener for your specific html routes (see http://symfony.com/doc/current/bundles/FOSRestBundle/format_listener.html#disabling-the-format-listener-via-rules)
So, add a rule for the format listener in your config.yml like this:
format_listener:
rules:
- { path: '^/my-html-routes', stop: true }
- { path: '^/api', priorities: ['json', 'xml'], fallback_format: json }

Related

Unable change Swagger UI location path

I made an api with api-platform.
I tried to edit the swagger_ui path '/' to '/docs' according to api-platform documentation and allow '/' redirect to custom twig i made.
The swagger_ui documentation is available in /docs path but also still available in '/' path instead of my custom twig file.
Here is my config :
app/config/packages/api_platform.yaml
api_platform:
mapping:
paths: ['%kernel.project_dir%/src/Entity']
enable_swagger_ui: true
enable_re_doc: true
enable_docs: true
app/config/routes/api_platform.yaml
api_platform:
resource: .
type: api_platform
prefix: /
app/config/route.yaml
swagger_ui:
path: /docs
controller: api_platform.swagger.action.ui
hello-world:
path: /
controller: App\Controller\HelloController::index
Thanks by advance for your answer, if i haven't be clear, just le me know :)
Try changing
api_platform:
mapping:
paths: ['%kernel.project_dir%/src/Entity']
enable_swagger_ui: true
enable_re_doc: true
enable_docs: true
with
api_platform:
mapping:
paths: ['%kernel.project_dir%/src/Entity']
enable_swagger_ui: true
enable_re_doc: true
enable_docs: true
and pay attention to indentation in other files ...
This seems to be broken for now. There's a PR to fix the issue inside Api Platform but it hasn't been merged yet: https://github.com/api-platform/core/pull/2749

Silverstripe Restfulapi configuration

I try the Restfulapi addon for silverstripe 4 and my configuration is:
integre\About:
api_access: true
integre\Home:
api_access: true
Image:
api_access: true
File:
api_access: true
integre\Theatre:
api_access: true
# RestfulAPI config
Colymba\RESTfulAPI\RESTfulAPI:
authentication_policy: false
access_control_policy: 'ACL_CHECK_CONFIG_AND_MODEL'
cors:
Enabled: true
Allow-Origin: '*'
Allow-Headers: '*'
Allow-Methods: 'OPTIONS, POST, GET, PUT, DELETE'
Max-Age: 86400
but when i try http://localhost/integre/api/Theatre/1 i receive
{
"code": 400,
"message": "Model does not exist. Received 'Theatre'."
}
how to fix this?
Your problem is that you're using a namespaced class and not correctly configuring it to be used in the API, so it's not resolving correctly. Looking at DefaultQueryHandler, you need to define a class name map for this:
Colymba\RESTfulAPI\QueryHandlers\DefaultQueryHandler:
models:
Theatre: integre\Theatre
This tells the query handler to load integre\Theatre when it is asked for a Theatre model. Note that your Image and File references in your configuration are also missing their namespaces.

Symfony2 FOSRestBundle overrides default application

I'm using FOSRestBundle to manage my api. I already have a running sf2 application, and i want to allow third person to access some of my application features. I configured my api, and it works as expected, i can consume my api route with success
for example :
GET http://my.domain.ldt/api/v1/users
My Api only handle json format, here is my fos_rest configuration :
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
view_response_listener: 'force'
exception_wrapper_handler: My\ApiBundle\Handlers\ApiExceptionWrapperHandler
formats:
json : true
failed_validation: HTTP_BAD_REQUEST
templating_formats:
html: false
xml: false
routing_loader:
default_format: json
include_format: false
exception:
enabled: true
service:
view_handler: my.view_handler
services:
my.json_handler:
class: My\ApiBundle\Handlers\JsonHandler
my.view_handler:
parent: fos_rest.view_handler.default
calls:
- ['registerHandler', [ 'json', ["#my.json_handler", 'createResponse'] ] ]
As i said, my Api works well, but i face a major problem : When i try to access to the main application from my web browser, ( http://my.domain.ldt/, or http://my.domain.ldt/login), i get the following response instead of my classic web page :
An Exception was thrown while handling: No matching accepted Response format could be determined
Why my fos_rest conf applies on my main website ? Is it possible to only set the api conf for the api routes ? Did i miss something ?
The problem is that you forgot to define rules for FOSRestBundle's format listener.
In fact I'm not sure you need this listener as it seems you use json as the default format. The format listener will try to match the Accept header and extract the current request format based on it. So except if you want to support other formats than json for your api, you can just not use it.
In case you want to fix it instead of removing it, you have to update your config with something like:
fos_rest:
format_listener:
enabled: true
rules:
- { path: '^/', priorities: ['json', 'xml', 'html], fallback_format: 'json' }
Of course you can change this rule to have a different rule for your api:
fos_rest:
format_listener:
enabled: true
rules:
- { path: '^/api', fallback_format: 'json' }
- { path: '^/', fallback_format: 'html' }

FosElasticaBundle: how to dump the actual JSON passed to ElasticSearch?

I am using FosElasticaBundle in a Symfony project. I configured my mappings but I get exception "expected a simple value for field [_id] but found [START_OBJECT]]".
I'd like to see the actual JSON created by FosElasticaBundle so I can directly test it against my ElasticSearch server, and understand more about the exception.
According to FosElastica documentation, everything should be logged when debug mode is enabled (i.e. in DEV environment) but I can't see this happening; I only see Doctrine queries, but no JSON.
How can I dump the JSON created by FosElasticaBundle?
Update: mappings
# FOSElasticaBundle
fos_elastica:
clients:
default: { host: %elasticsearch_host%, port: %elasticsearch_port%, logger: false }
indexes:
app:
types:
user:
mappings:
name: ~
surname: ~
persistence:
driver: orm
model: AppBundle\Entity\User
provider: ~
listener: ~
finder: ~
I think you should only set your logger to true instead of false
fos_elastica:
clients:
default:
host: %elasticsearch_host%
port: %elasticsearch_port%
logger: true <---- set true here
...

415 Unsupported Media Type

I followed this link
when I run this command to see if the Get method works very well I have this error displayed in my terminal ERROR 415: Unsupported Media Type.
I'm going to see my source code :
1- First I set my config file :
#app/config/config.yml
sensio_framework_extra:
view:
annotations: false
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
view:
view_response_listener: 'force'
2- After i just fix my routing file :
#app/config/routing.yml
minn_ads_api:
resource: "#MinnAdsAPIBundle/Resources/config/routing.yml"
type: rest
3- I have also set the routing file in my *AdsAPIBundle :*
#AdsAPIBundle/Resources/config/routing.yml
brend:
type: rest
resource: Minn\AdsAPIBundle\Controller\BrendController
4- Under my AdsAPIBundle I just insert the function :
#src/BundleApi/Controller/BrendController.php
/**
* #Rest\View
* #Rest\Get("/api/brend/{id}", requirements={"id" = "\d+"}, defaults={"id" = 1})
*/
public function getAction($id) {
$repo = $this->getDoctrine()->
getManager()->
getRepository("MinnAdsBundle:Brend");
$brend = $repo->find($id);
return array('brend' => $brend);
}
Maybe you need to add a default format:
# app/config/config.yml
fos_rest:
routing_loader:
default_format: json

Resources