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/ ]
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 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 was able to setup Sonata Admin with translated entities using Gedmo Doctrine Extensions:
# Doctrine Extensions Configuration
stof_doctrine_extensions:
default_locale: '%locale%'
orm:
default:
timestampable: true
blameable: true
translatable: true
# Sonata Translation Configuration
sonata_translation:
locales: [en, fr, it]
default_locale: '%locale%'
gedmo:
enabled: true
However every time I create a new entity, the translatable fields in the other languages starts empty.
English language selected:
Italian language selected:
It becomes very difficult to translate items if I don't know what they are in English.
Is there an option so that when I create an entity in English it populates also the entities in the other languages with the same content?
You should add fallback option to fields you need:
/**
* #var string
*
* #Assert\NotBlank()
*
* #Gedmo\Translatable(fallback=true)
*
* #ORM\Column(type="string", length=255)
*/
private $title;
I guess You forget to load the event listener service.
For each Gedmo extension (Timestampable, Blameable, Translatable etc. ) you have to register service in your service container.
For Translatable:
## app/config/services.yml
services:
## ...
gedmo.listener.translatable:
class: Gedmo\Translatable\TranslatableListener
tags:
- { name: doctrine.event_subscriber, connection: default }
calls:
- [ setAnnotationReader, [ #annotation_reader ] ]
- [ setDefaultLocale, [ %locale% ] ]
- [ setTranslationFallback, [ false ] ]
Running bin/console doctrine:schema:update --force again and again, it always output 39 queries executed even after clearing cache/restarting php-fpm. So it always execute the same SQL requests again and again...
The SQL looks likes (from --dump-sql)
ALTER TABLE apply_queue CHANGE cv_id cv_id INT DEFAULT NULL, CHANGE team_id team_id INT DEFAULT NULL;
....
And a lot of lines similar to this.
The ApplyQueue class look like:
/**
* AppBundle\Entity\ApplyQueue.
*
* #ORM\Entity(repositoryClass="AppBundle\Entity\ApplyQueueRepository")
* #ORM\Table(name="apply_queue")
*/
class ApplyQueue
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Cv")
* #ORM\JoinColumn(name="cv_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $cv;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Team")
* #ORM\JoinColumn(name="team_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $team;
...
}
DBAL config :
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
types:
json: Sonata\Doctrine\Types\JsonType
review_status: AppBundle\Model\Enum\ReviewStatusEnumType
mapping_types:
review_status: string
The structure of the table:
This is an "old" project that has been upgraded to Symfony 3.4 and MariaDB 10.2.
Thanks
MariaDB 10.2 is not yet supported by Doctrine, causing this problem.
See linked PR : https://github.com/doctrine/dbal/pull/2825
I had similar problem few weeks ago. In my case it was that after update
--force
it shows some column must be set not null on
--dump-sql.
Again and again. I don't remember so good what was problem but it was something like this:
I had for same column set to be null and to have default value. Without doctrine that could even work somehow, but doctrine expects tables to be designed good.
What I want to say check your table. There is probably some error of this type. Goodluck!
check your database table type - i had a problem where the database type didnt support the feature doctrine trying to set.
Maybe run the sql on the database server and see if it 'works' / changes anything, then re-run
In your app/config/config.yml, check if doctrine cache is enabled as below :
doctrine:
orm:
metadata_cache_driver: redis
If so, you will have to flush redis cache:
php bin/console redis:flushdb --client=CLIENT_NAME -n
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