Silverstripe Restfulapi configuration - silverstripe

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.

Related

symfony 5.2 unrecognized option 'http_version' under framework.http_client

I'm trying to implement async HTTP client with Symfony and amphp/http-client, in symfony docs said thatto enable it for HTTP I need to add http_version: '2.0'option to config and I did it:
framework:
http_client:
http_version: '2.0'
scoped_clients:
site:
base_uri: '%env(URL)%'
headers:
x-access-token: '%env(ACCESS_TOKEN)%'
but it's not working and I'm getting error Unrecognized option "http_version" under "framework.http_client". Available options are "default_options", "enabled", "max_host_connections", "mock_response_factory", "scoped_clients".
Any idea what is wrong?? Thanks in advance!
Looks like the docs might need to be updated a bit. Under a 5.2 app:
bin/console config:dump-reference framework http_client
# HTTP Client configuration
http_client:
enabled: true
# The maximum number of connections to a single host.
max_host_connections: ~
default_options:
# Associative array: header => value(s).
headers:
# Prototype
name: ~
# The maximum number of redirects to follow.
max_redirects: ~
# The default HTTP version, typically 1.1 or 2.0, leave to null for the best version.
http_version: ~
So http_version goes under default_options
# config/packages/framework.yaml
framework:
http_client:
default_options:
http_version: '2.0'
bin/console debug:config framework http_client
Current configuration for "framework.http_client"
=================================================
default_options:
http_version: '2.0'
headers: { }
resolve: { }
retry_failed:
enabled: false
retry_strategy: null
http_codes: { }
max_retries: 3
delay: 1000
multiplier: 2
max_delay: 0
jitter: 0.1
enabled: true
scoped_clients: { }

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

CORS errors with Symfony 4 & Nelmio CORS Bundle

I've been trying to figure out why I'm getting CORS issues with my Symfony 4 API application I've just deployed to my Apache server and can't make any sense of the issue.
config/packages/nelmio_cors.yaml
nelmio_cors:
defaults:
origin_regex: true
allow_origin: ['%env(CORS_ALLOW_ORIGIN)%']
allow_methods: ['GET', 'OPTIONS', 'POST', 'PUT', 'PATCH', 'DELETE']
allow_headers: ['Content-Type', 'Authorization']
max_age: 3600
paths:
'^/': ~
.env
...
CORS_ALLOW_ORIGIN=/*/
...
All responses from requests I make from my localhost front-end application to that API contain no Access-Control-Allow-Origin header, and I get the standard error;
Access to XMLHttpRequest at 'http://my-api.com/foo' from origin
'http://localhost:4200' has been blocked by CORS policy: Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource.
No special headers are being sent and for now I've set the allowed origin regex to "all" so I can't work out what is causing issue here. I've even checked within the cache to ensure the origin is being correctly pulled from the env variables, which it is. If other context/file content is required to assist please let me know!
I always try to be a bit more specific for allowing CORS like:
CORS_ALLOW_ORIGIN=^http://(.*:8080|localhost:4200)$
what you could try if you really want to enable all origins would be something like:
CORS_ALLOW_ORIGIN=^.*$
Your problem is that you've opted to use a regular expression (origin_regex: true) but not provided valid pattern.
If you want to use origin_regex: true then you should specify a valid pattern such as .* or ^.*$.
If you don't want to use a regular expression then omit the origin_regex setting (or set it to false) and just use * as your CORS_ALLOW_ORIGIN value.
I've resolved the issue, and although on the surface it appeared to be related to the CORS configuration, it was actually misconfiguration of the project on the server.
TL;DR is that the project was missing a .htaccess file which I didn't require in development due to using Valet - following the instructions here resolved the issue.
Why do you need nelmio?
You can have simple event listener(on kernel.event_subscriber) adding these headers.
namespace App\EventListener\HttpKernel;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class CorsSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => 'onResponse'
];
}
public function onResponse(FilterResponseEvent $filterResponseEvent)
{
$response = $filterResponseEvent->getResponse();
$response->headers->set('Access-Control-Allow-Origin', '*');
}
}
Register it as kernel.event_subscriber
app.http_kernel.cors_subscriber:
class: App\EventListener\HttpKernel\CorsSubscriber
tags:
- { name: kernel.event_subscriber }

Possibility that a bundle is not detected in a Symfony project?

I work on Symfony 2.3 and I installed MobileDetectBundle that works on this symfony version:
(in my composer.json)"suncat/mobile-detect-bundle": "0.10.3"
(in packagist)https://packagist.org/packages/suncat/mobile-detect-bundle#v0.10.3
I configured the bundle accordingly
AppKernel.php
new SunCat\MobileDetectBundle\MobileDetectBundle(),
config_mobile.yml
mobile_detect:
redirect:
full:
is_enabled: true # default false
host: http://page.com # with scheme (http|https), default null, url validate
status_code: 301 # default 302
action: redirect # redirect, no_redirect, redirect_without_path
mobile:
is_enabled: true
host: http://m.page.com
status_code: 301
action: redirect
tablet:
is_enabled: true
host: http://t.page.com
status_code: 301
action: redirect
switch_device_view:
save_referer_path: true
service:
mobile_detector: mobile_detect.mobile_detector.default
device_view_class: SunCat\MobileDetectBundle\Helper\DeviceView
request_listener_class: SunCat\MobileDetectBundle\EventListener\RequestListener
extension_class: SunCat\MobileDetectBundle\Twig\Extension\MobileDetectExtension
in my service.yml
services:
mobile_detector: #mobile_detect.mobile_detector.default
in app_mobile.php
$kernel = new AppKernel('mobile', false);
The bundle works with the mobiledetect classes. Both of them are present in my vendor, so I guess that the installation was done properly
I restarted my server, PHP, cleared the cache but still it doesn't show me my redirection in the URL.
Is it possible in some cases that Symfony doesn't recognize a bundle? Does that happens depending on the version you are?
Try this :
mobile_detect:
redirect:
mobile: ~
tablet: ~
switch_device_view: ~

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' }

Resources