I am trying to make the Restful API documentation in PHP swagger, what I did before is that I changed the JSON to work out, now I know we can make the JSON by making PHP files and using swagger notation. I did check the Pet.php example and I get the code but I don't know how to execute the file to get the JSON API documentation which I can link with my Swagger UI. I read the documentation but it is so confusing and I don't know how to get through this problem can anyone help, please? Here is the link I study but to no worth.
https://github.com/zircote/swagger-php
Swagger-PHP for generating JSON file for Swagger-UI
Can anyone tell me step-by-step how to generate the API documentation in JSON? I will be very thankful to him thanks.
There are two way to achieve this in swagger-php 2.0.
I.
The first solution is to create a controller or script which will generate the documentation on each request. This is a good solution in a development environment where you want to see quickly the outcome of your changes.
Here is an example of a controller which does this.
<?php
namespace Controllers;
use Swagger\Annotations as SWG;
use Swagger;
/**
* #SWG\Swagger(
* basePath="/path/to/opration/",
* produces={"application/json"},
* swagger="2.0",
* #SWG\Info(
* version="1.0.0",
* title="My API"
* )
* )
*
*/
class Documentation {
const API_PATH = "path/to/my/documented/files/";
public function show(){
$swagger = Swagger\scan(self::API_PATH);
return json_enconde($swagger); //you can echo this in the calling script.
}
}
Note: The example above assumes you installed Swagger-php with Composer and that the calling script include the composer generated autoload file (usually called: vendor/autoload.php).
II.
The first solution consisting of generating a static json API documentation is described here: https://stackoverflow.com/a/21380432/2853903
This solution recommended for production deployment, where you would not want to regenerate the documentation on every request.
Related
I am using l5-swagger and redocly to display API documentation for a Laravel 8 project.
Open API supports:
"x-logo": {
"url": "path_to_your_logo"
},
I have tried:
#OA\X_Logo
#OA\XLogo
#OA\Logo
But I cannot find any mention within l5-swagger on how to mark-up the logo within the comments on the Controller in order for the logo markup above to be generated when running
php artisan l5-swagger:generate
I also cannot find anyone asking this question which seems odd because surely this is a common query to come up?
So I am leaning towards that it isn't currently supported in swagger-php
Searching here for Info/Logo returns no mention of Logo and again
searching here for Info/Logo returns no mention of Logo
Which then makes this a question of how could it be done - it surely would be that difficult to introduce support for it.
So I have tried with this pull-request on swagger-php so that I can then get support added to l5-swagger.
Any help with this would be appreciated, I apologise if this has been asked before! But hopefully we can get it answered and then this will be a question/answer worth having.
EDIT / UPDATE
Thank you #bob-fanger for the correct answer.
It turns out that the x-logo is not part of the OpenAPI spec so probably this is outside of the scope of this library. But that doens't matter as Bob has explained that l5-swagger does support it.
I will try to get a logo example added to their documentation to make it easier to find for everyone going forward.
x- properties are supported in swagger-php, those properties are not part of the openapi spec and are referred to as vendor extensions.
https://zircote.github.io/swagger-php/Getting-started.html#vendor-extensions
/**
* #OA\Info(
* title="Example",
* version="1.0.0",
* x={
* "logo": {
* "url": "path_to_your_logo"
* }
* }
* )
*/
will generate:
openapi: 3.0.0
info:
title: Example
version: 1.0.0
x-logo:
url: path_to_your_logo
Background: I have installed composer installed zircote/swagger-php and have installed openapi-generator-cli with apt-get.
I am not sure whether or not I am attempting to use these tools correctly, however I've been unable to find documentation pointing me in any direction.
I have a controller file with a whole bunch of code in it. I want to test whether or not I can generate a json file from it using open api annotation.
Here's a sample of my code (I've cut out unrelated chunks of it):
<?php
/**
* #OA\Info(title="My First API", version="0.1")
*/
class Apiv1_LocationController extends App_Controller_API
{
/* Publicly exposed attributes and the field type for filtering */
protected $_exported = array('id','created','modified','name','address','phone','external_id','postcode','country','timezone','date_format','lacps','staff_count');
protected $_schematypes = array(
'string' => ['name','address','phone','external_id','postcode','country','timezone','date_format'],
'int' => ['id','staff_count'],
'timestamp' => ['created','modified'],
'complex'=> ['lacps'],
);
{more unrelated code...}
/**
* #OA\Get(
* path="/api/resource.json",
* #OA\Response(response="200", description="An example resource")
* )
*/
public function getAction()
{
{code inside action...}
}
}
The cli command I use:
openapi-generator-cli generate -g php -i <path_to_LocationController>
I get the following error:
[main] INFO o.o.c.ignore.CodegenIgnoreProcessor - No .openapi-generator-ignore file found.
Exception in thread "main" java.lang.RuntimeException: Issues with the OpenAPI input. Possible causes: invalid/missing spec, malformed JSON/YAML files, etc.
This leads me to believe I am using the openapi-generator-cli tool incorrectly, since I wouldn't be expecting to need a JSON or YAML file, I am trying to generate that file.
I'll keep trying, but if someone could help me realized what I'm doing wrong or how I'm misunderstanding the tool, I'd really appreciate it.
So I realized I'd been going about this in entirely the wrong way.
I used the zircote/swagger-php library to generate the JSON file I required with the following command (In the directory where I wanted the JSON to be generated):
./<path_to_vendor_directory>/vendor/zircote/swagger-php/bin/openapi --pattern "*Controller.php" --output <name_of_file>.json --format json <location_to_search_from_recursively>
I'm using this package https://github.com/mpociot/laravel-apidoc-generator to generate api documentation
but i got couple problems:
1- Running requests in the docs always return "unauthenticated" how to configure this in a right way ? i tried to add the login token to apidoc.php in authorization but it didn't work .
some people said i should do this
php artisan api:generate --actAsUserId=1
but it returns
The "--actAsUserId" option does not exist.
2- some request need email as #bodyParam but couldn't find a type of email so i use text which of course fails and return invalid type. how to force type email ?
Thanks in advance, hope i was clear.
try this
/**
* #authenticated <------
* #group MyGrupo
*
*/
class MyController extends Controller{
}
and in your console:
#php artisan apidoc:generate
I'm a former maintainer of laravel-apidoc-generator. The package is no longer supported. Here's the one you should be using: knuckleswtf/scribe.
To pass an auth value in Scribe, you set auth.use_value to the token in your config file. That's it. You can see more in the docs.
The details in the apply.headers section are only used in the HTML docs, not in response calls.
I am new to this stuff but I love it. I made a little REST Api using the Slim Framework. Now I would like to have an ongoing documentation about it. I think swagger is the right choice but I have not found out yet how to integrate it ?
Cheers & thanks for your patience :)
I think you are looking for this project: zircote/swagger-php
Here you'll find how to generate the doc on user request to a URL.
Basically you have to annotate your code with the Swagger Annotations, then create another route in Slim with a code similar to this:
<?php
use Swagger\Swagger;
$swagger = new Swagger('/project/root/top_level');
header("Content-Type: application/json")
echo $swagger->getResource('/pet', array('output' => 'json'));
And it will generate the Swagger API docs on the fly for you.
A short update to the answer of adosaiguas:
When using Slim Framework 4.0 and zircote/swagger-php
one can provide an api endpoint providing the swagger / OpenAPI 3.0 json description using the following code:
use function OpenApi\scan;
/**
* #OA\Get(
* path="/openapi",
* tags={"documentation"},
* summary="OpenAPI JSON File that describes the API",
* #OA\Response(response="200", description="OpenAPI Description File"),
* )
*/
$app->get('/openapi', function ($request, $response, $args) {
$swagger = scan('--PATH TO PROJECT ROOT--');
$response->getBody()->write(json_encode($swagger));
return $response->withHeader('Content-Type', 'application/json');
});
Does PHPCodeSniffer generates HTML Report?
If not? How?
Currently, I can run PHPCodeSniffer but it only produce XML file and displays result in the terminal.
How can I make produce HTML Report like the coverage and unit test report in phpunit.
Currently I use PHPCheckStyle since it produces html report, but i want also to try PHPCodeSniffer to know which is best.
Another solution (but maybe not the simplest one) is to use a continuous integration system that provides PHP code sniffer integration.
For example, phpUnderControl provides a nice interface to see the report.
HTML Reporting is not provided yet. However there is a workaround to get the job done.
You can export the report to XML and read the data with DOM Parser and generate an HTML version on your own. Here is a quick tutorial to get you started.
Writing your own report is pretty trivial actually.
If you are running from CLI you can implement your own report class with a unique method and call it from command ligne. For a report named Xxx :
class PHP_CodeSniffer_Reports_Xxx implements PHP_CodeSniffer_Report
{
/**
* Prints all errors and warnings for each file processed.
*
* Errors and warnings are displayed together, grouped by file.
*
* #param array $report Prepared report.
* #param boolean $showSources Show sources?
* #param int $width Maximum allowed lne width.
* #param boolean $toScreen Is the report being printed to screen?
*
* #return string
*/
public function generate(
$report,
$showSources=false,
$width=80,
$toScreen=true
) {
...
}
}
If your are running from a web server, the PHP_CodeSniffer.getFilesErrors() method gives you a nice array of errors with all you need to produce a report.
HTML Reporting is working for me. Just use according --help:
phpcs src/AppBundle --generator=HTML > index.html
Show the result in index.html