Closed pascalf22 closed 2 years ago
The code is trying to use reflection to collect details about the PHP code and cannot find the class \BlogsModel
.
I would guess that there is a namespace missing somewhere. Does the API endpoint actually work or at least load?
Hello ! Thank you for your reply ! Yes, my class (end point) works perfectly !
In a browser, if i try to reach my end point to get blog article : http://refonte.local/api/webapp/v1/blogs/1 it works perfectly and i get blog articles titles...
here my api/webapp/v1/Models/BlogsModel.php :
<?php
/**
* @OA\Info(title="My First API", version="0.1")
*/
class BlogsModel {
public $dbcon;
public $http_method;
public $config;
public $userId;
/**
* @OA\Get(
* path="/api/resource.json",
* @OA\Response(response="200", description="An example resource")
* )
*/
public function getBlogs($pageno=1)
{
...
}
public function readArticle($id)
{
...
}
}
?>
I suppose my class is OK ? but it's working when i do : http://refonte.local/api/webapp/v1/blogs/1 or http://refonte.local/api/webapp/v1/blog/267
thanks a lot !
Hello !
Ok it is working now... now i get :
{
"openapi": "3.0.0",
"info": {
"title": "My First API",
"version": "0.1"
},
"paths": {
"/api/resource.json": {
"get": {
"operationId": "82c2269a247163a711afe70f585c6866",
"responses": {
"200": {
"description": "An example resource"
}
}
}
}
}
}
but i had to add this in my /api/documentation/api.php :
spl_autoload_register('autoloader');
function autoloader(string $name) {
if (file_exists('../webapp/v1/Controllers/'.$name.'.php')){
require_once '../webapp/v1/Controllers/'.$name.'.php';
}
else if (file_exists('../webapp/v1/Models/'.$name.'.php')){
require_once '../webapp/v1/Models/'.$name.'.php';
}
}
The final /api/documentation/api.php file is :
<?php
spl_autoload_register('autoloader');
function autoloader(string $name) {
if (file_exists('../webapp/v1/Controllers/'.$name.'.php')){
require_once '../webapp/v1/Controllers/'.$name.'.php';
}
else if (file_exists('../webapp/v1/Models/'.$name.'.php')){
require_once '../webapp/v1/Models/'.$name.'.php';
}
}
require($_SERVER['DOCUMENT_ROOT']."/api/vendor/autoload.php");
$openapi = \OpenApi\Generator::scan([$_SERVER['DOCUMENT_ROOT'].'/api/webapp/v1/Models']);
header('Content-Type: application/json');
echo $openapi->toJSON();
?>
Is that normal i had to add this ?
Thanks so much !
EDIT: Ah, I should have read all of your comment. ..
The code fails at this line:
if (!class_exists($fqdn) && !interface_exists($fqdn) && !trait_exists($fqdn) && (!function_exists('enum_exists') || !enum_exists($fqdn))) {
$analysis->context->logger->warning('Skipping unknown ' . $fqdn);
with $fqdn
being \BlogModel
. That means class_exists()
cannot resolve it. That, in turn, means the composer classloader cannot find/resolve the class name. There isn't a lot I can do to help from here I am afraid.
Your additions, of course, fix that.
My best guess at this point is that you are using a framework that magically uses aliases instead of proper namespaces. swagger-php relies just on the composer autoloader, not any other features outside of that.
You might be able to get around this by creating an optimized autoloader dump; something like
composer dump-autoload --optimize
Hello ! thanks :)
I just want to know if you read my last message.. because i think we submit a message at the same time ;)
thanks
Yes, that is why I edited my last message again :)
Thank you so much ! I reallya appreicated your help !
Have a great day ! Pascal
Thanks a lot for this code.
spl_autoload_register('autoloader'); function autoloader(string $name) {
if (file_exists('../webapp/v1/Controllers/'.$name.'.php')){
require_once '../webapp/v1/Controllers/'.$name.'.php';
}
else if (file_exists('../../models/'.$name.'.php')){
require_once '../../models/'.$name.'.php';
}
}
Hello friends, I have the same issue, but fortunately I could resolve with the explication, but I want to add some comments for the future people resolved more quickly and easy.
I use this distribution or files:
api\v1\documentation\api.php <?php spl_autoload_register('autoloader'); function autoloader(string $name) {
if (file_exists('../Models/'.$name.'.php')){
require_once '../Models/'.$name.'.php';
}
} require($_SERVER['DOCUMENT_ROOT']."/api/vendor/autoload.php"); $openapi = \OpenApi\Generator::scan(['../Models']); echo $openapi->toJSON(); ?>
api\v1\documentation\index.php
<!DOCTYPE html>
This file I get of this repo: swagger-api/swagger-ui: Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API. (github.com)
I just copy the index.html on the folder dist and copy and page the css, png and js that I need into my folder swagger
And I was need rename my models as the same name has the class: api/v1/Models/CatAreas.php <?php /**
public function getAreas(): string { … } }
compose.json { "require": { "zircote/swagger-php": "^4.7" } }
I ran into a similar issue and it turned out to be a naming/autoloading issue for the underlying class.
Basicly class_exists('Namespace/To/Class') will be false if the class isn't autoloaded. This is what ReflectionAnalyzer->analyzeFqdn() checks.
My file name and the class name inside didn't match, therefore it was not autoloaded for some reason.
Hello !
I installed swagger-php via composer and i followed your documentation..
Here what i did :
To generate always-up-to-date documentation, i created a new file : api\documentation\api.php
in my Models folder : api\webapp\v1\Models\
But when in my browser when i try to see : http://refonte.local/api/documentation/api.php
I get this error :
What i'm doing wrong ?
thank you so much !
Have a great day !