Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReflectionException with controller from make:controller --api #72

Open
h3xx opened this issue Jun 10, 2022 · 1 comment
Open

ReflectionException with controller from make:controller --api #72

h3xx opened this issue Jun 10, 2022 · 1 comment

Comments

@h3xx
Copy link

h3xx commented Jun 10, 2022

The following error is seen when generating the OpenAPI JSON from the command line (artisan openapi:generate). This is for a bare controller with no editing beyond the Laravel boilerplate for artisan make:controller --api; no #[attribute] annotations were added to the controller, only a Route::resource() entry in routes/api.php.

   ReflectionException

  Method App\Http\Controllers\FooController::create() does not exist

  at vendor/vyuldashev/laravel-openapi/src/RouteInformation.php:75
     71▕                 ]);
     72▕             }
     73▕
     74▕             $reflectionClass = new ReflectionClass($controller);
  ➜  75▕             $reflectionMethod = $reflectionClass->getMethod($action);
     76▕
     77▕             $docComment = $reflectionMethod->getDocComment();
     78▕             $docBlock = $docComment ? DocBlockFactory::createInstance()->create($docComment) : null;
     79▕

      +4 vendor frames
  5   [internal]:0
      Vyuldashev\LaravelOpenApi\Builders\PathsBuilder::Vyuldashev\LaravelOpenApi\Builders\{closure}(Object(Illuminate\Routing\Route))

      +19 vendor frames
  25  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

Steps to Reproduce

  1. Start a new Laravel project using PHP 8.1.6
composer create-project laravel/laravel myproject
cd myproject
  1. Run the following commands to add dependencies and make a controller:
composer require 'php=^8.0'
composer require 'vyuldashev/laravel-openapi'
php artisan make:controller --api FooController
  1. Add the following line to the routes/api.php file:
Route::resource('/foo', App\Http\Controllers\FooController::class);
  1. Run php artisan openapi:generate.

This results in the error I included above.

Extra Info

  • Mac OSX (brew install php@8.1)
  • PHP 8.1.6
  • Laravel 9.11
Click for composer.json contents
{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^8.0",
        "guzzlehttp/guzzle": "^7.2",
        "laravel/framework": "^9.11",
        "laravel/sanctum": "^2.14.1",
        "laravel/tinker": "^2.7",
        "vyuldashev/laravel-openapi": "^1.5"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.5.10",
        "spatie/laravel-ignition": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}
@mrmstn
Copy link

mrmstn commented Oct 5, 2022

Hi @h3xx, I'm not quite sure if this issue is still present on your side, but you're referencing a method which does not exist (as the exception says)

Please see https://laravel.com/docs/9.x/controllers#restful-partial-resource-routes and use the ->only or ->except function to prevent the resource function to expose endpoints which do not exist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants