I've tried defining a custom route name for one of my APIs and since then, the API Doc displays that route twice. Any ideas why?
Here's the definition of my API:
/**
* #ApiDoc(
* description = "Sends the support email to the HelpDesk address",
* statusCodes = {
* 204 = "Returned when successful",
* 400 = "Returned when the parameters are incorrect",
* 401 = "Returned when the token is invalid",
* 500 = "Returned when there's an internal server error"
* },
* input="AppBundle\Form\Type\SupportEmailType"
* )
* #Post("/support-requests")
* #Rest\View ()
*/
public function postSupportAction(Request $request)
and here's how the route shows up in my doc:
And this is my routing.yml file:
# app/config/routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
NelmioApiDocBundle:
resource: "#NelmioApiDocBundle/Resources/config/routing.yml"
prefix: /api/doc
user:
type: rest
resource: AppBundle\Controller\UserController
From the looks of it the only thing that comes to mind as having the potential to do this is the first part of your routing.yml
try removing this from your routing.yml
app:
resource: "#AppBundle/Controller/"
type: annotation
I think this code, and the separated definition of the user route makes nelmio see the route twice. I had a similar problem some time ago and I think this was the reason. Sorry for the amount of questions I had to ask but i needed to see the full picture.
Hope this helps,
Alexandru Cosoi
Related
i have website in Symfony 4.2 with 3 prefixed locales:
annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
prefix:
en: '/en'
de: '/de'
cz: '/cz'
and with route annotations in controllers:
/**
* #Route({"en": "references"}, name="reference")
* #Route({"cz": "reference"}, name="reference")
* #Route({"de": "referenzen"}, name="reference")
*/
Now i need cancel deutch locale and all /de/route adresses redirect to /en/route.
Is here any easy way to do it?
I want to use nelmio for symfony-project, but it doesn't work.
It always says: No operations defined in spec!
I also try the example on https://symfony.com/doc/current/bundles/NelmioApiDocBundle/index.html
Whats's wrong? Any ideas?
routing.yml
app.swagger_ui:
path: /api/doc
methods: GET
defaults: { _controller: nelmio_api_doc.controller.swagger_ui }
config.yml
nelmio_api_doc:
areas:
path_patterns: # an array of regexps
- ^/api(?!/doc$)
host_patterns:
- ^api\.
Controller
/**
* #Route("/api/test", methods={"GET"})
* #SWG\Response(
* response=200,
* description="Returns the rewards of an user"
* )
* #SWG\Parameter(
* name="order",
* in="query",
* type="string",
* description="The field used to order rewards"
* )
*/
public function testAction()
{
}
composer.json
"symfony/symfony": "3.4.*",
"nelmio/api-doc-bundle": "3.2.1",
Just remove
host_patterns:
- ^api\.
and set your virtual host in
documentation:
host: symfony.localhost
The assets normally are installed by composer if any command event (usually post-install-cmd or post-update-cmd) triggers the ScriptHandler::installAssets script. If you have not set up this script, you can manually execute this command:
php bin/console assets:install --symlink
The problem is in config.yml path patterns. If you remove the config(all nelmio_api_doc) or change the path patterns will work. Example:
nelmio_api_doc:
areas:
default:
path_patterns: [ /api/ ]
I am working on a project using the API Platform.
I would like to have the documentation of the UI secured with basic authentication, while some of the routes should be publicly available.
For example:
/**
* An ordered dish.
*
* #ApiResource(
* attributes={"access_control"="is_granted('ROLE_USER')"},
* collectionOperations={
* "get"={"access_control"="is_granted('IS_AUTHENTICATED_ANONYMOUSLY')"},
* "post"={"access_control"="is_granted('ROLE_ADMIN')"}
* },
* itemOperations={
* "get"={"access_control"="is_granted('IS_AUTHENTICATED_ANONYMOUSLY')"}
* }
* )
*
* #ORM\Entity
* #ORM\Table(name="uitgekookt_dish")
*/
class Dish
When I view the docs in the browser, i can simply access /api/dishes, because of the IS_AUTHENTICATED_ANONYMOUSLY. I see other get/post methods available there as well (though some of the post methods are secured).
However, the docs should not be available at all publicly. How do I ensure, in Symfony 4, that I seperate my security config for docs and ajax requests?
Not sure if I get it right but you want to restrict /api/doc (or whatever url you are using for docs) so add it to access_control in security.yaml
security:
access_control:
- { path: ^/api/doc, roles: [ROLE_ADMIN] }
First, disable Swagger UI that is loaded for every page, and register it as a known location as pointed out in this docs entry:
# app/config/routes.yaml
api_platform:
# ...
enable_swagger_ui: false
# app/config/routes.yaml
swagger_ui:
path: /api/docs
controller: api_platform.swagger.action.ui
Then, as pointed out by #zajca, add an access control route to secure this page, and the underlying JSON and Hydra endpoints:
security:
access_control:
- { path: ^/api/docs, roles: [ROLE_ADMIN] }
I have an EventApiController class that looks like the following
Class EventApiController
{
public function getAction($event_id)
{
// ...
}
public function putAction($event_id)
{
// ...
}
}
Using the Friends of Symfony bundle's route generate i am expecting the route to look like
CalendarBundle_get_events [GET] /api/v1/events/{event_id}.{_format}
CalendarBundle_put_events [PUT] /api/v1/events/{event_id}.{_format}
However it seems like the Route generator automatically adds a post fix /api to all of the routes so the route looks like. And the documentation does not show this as the expected behaviour as well.
CalendarBundle_get_events_api [GET] /api/v1/events/{event_id}/api.{_format}
CalendarBundle_put_events_api [PUT] /api/v1/events/{event_id}/api.{_format}
Does anyone know how to get rid of the /api post fix from the generated link? I am using FOS/ResutBundle version 1.3.1
My config.yml for fos_rest
fos_rest:
routing_loader:
default_format: json
include_format: true
view:
view_response_listener: true
And the routing.yml looks like this in my Bundle
event_api:
type: rest
resource: "#CalendarBundle/Controller/EventsApiController.php"
prefix: /api/v1
name_prefix: CalendarBundle_
It's the Api in EventApiController that gets detected as route resource by FOSRestBundle.
You can override the resource name like this in order to prevent the _api route name and /api urls:
use FOS\RestBundle\Routing\ClassResourceInterface;
/**
* #RouteResource("Event")
*/
Class EventApiController implements ClassResourceInterface
{
Simple case. In my all app I'm using Route annotation driver, defined in routing.yml as follows:
MyBundle:
resource: "#MyBundle/Controller/"
type: annotation
prefix: /someprefix
Some action in MyBundle's Ctrl controller looks like:
#Route("/{page}/{status}", name="default_action", defaults={"page" = 1, "status" = "ok"}, requirements={"page" = "\d+"})
public function defaultAction($page, $status) {...}
Now I want to make this action - default action when visiting my web page. I cannot use just #Route("/") because I'm prefixed. So I'm adding to routing.yml:
_welcome:
pattern: /
defaults: { _controller: MyBundle:Ctrl:default }
And there is where problem starts - Symfony2 is calling default controllers action not from annotation but just from action and I get error:
Controller "...:defaultAction()" requires that you provide a value for the "$page" argument (because there is no default value or because there is a non optional argument after this one).
So simply Symfony2 is not obtaining default values from #Route annotation.
Question is: how to call route for _welcome that is aware of #Route?
You've missed defaults in yml settings, should look like this:
_welcome:
pattern: /
defaults: { _controller: MyBundle:Ctrl:default, page: 1, status: ok }