Introduction

Installation

This guide covers Laravel installation and manual setup for other PHP applications.

Using Laravel

To install PHP autodoc in a Laravel project, install the 421c/autodoc-laravel package.

composer require 421c/autodoc-laravel

Copy the package configuration file into your project:

php artisan vendor:publish --provider="AutoDoc\Laravel\Providers\AutoDocServiceProvider"

In `config/autodoc.php`, set `openapi_export_dir` to an existing writable directory. PHP autodoc writes the generated OpenAPI 3.1.0 JSON files there.

Set laravel.url to choose the documentation URL. By default, the documentation is available at /api-docs.

Alternatively, you can export OpenAPI JSON files using the command below. See workspace setup and usage guide for more information.

php artisan autodoc:openapi

For TypeScript generation, see TypeScript export.

Manual installation

For other PHP applications, set up PHP autodoc manually:

  1. Install 421c/autodoc-php package.
composer require 421c/autodoc-php
  1. Create a class that extends AutoDoc\AbstractRouteLoader and define a getRoutes method. See more information in code comments below.
use AutoDoc\AbstractRouteLoader;
use AutoDoc\Route;

class RouteLoader extends AbstractRouteLoader
{
    public function getRoutes(): iterable
    {
        /**
         * Return an array of routes, or yield them as shown here.
         */

        yield new Route(
            uri: '/api/test/x',
            method: 'get',
            className: TestController::class,
            classMethod: 'x',
        );

        /**
         * You may construct the Route objects so that they point to either
         * class methods or closures.
         */

        yield new Route(
            uri: '/api/test/y',
            method: 'post',
            closure: (
                /**
                 * @return array<object{test: bool}>
                 */
                function () {
                    // ...
                }
            ),
        );
    }
}
  1. Copy the configuration file located at vendor/421c/autodoc-php/config/autodoc.php to your project.
  2. Set route_loader to the class you created in step 2.
/**
 * Class that will be used to load and analyze routes.
 * This class must extend `AutoDoc\AbstractRouteLoader`.
 */
'route_loader' => RouteLoader::class,
  1. Set openapi_export_dir to an existing writable directory for the generated OpenAPI 3.1.0 JSON files.
/**
 * Directory where OpenAPI schema files will be exported.
 */
'openapi_export_dir' => '/path/to/openapi',
  1. Add a catch-all GET route, or equivalent front controller, that passes the matched path to AutoDoc\DocViewer. This endpoint serves the documentation page, OpenAPI JSON, and viewer assets:
// `$path` is the part of the URL after the base path — for a route mounted
// at `/docs`, it is '' for the page, 'openapi.json' for the schema and
// 'assets/...' for the viewer files.
$config = new \AutoDoc\Config(require '/path/to/your/config/autodoc.php');

new \AutoDoc\DocViewer($config, baseUrl: '/docs')->handle($path)->emit();

handle() returns an AutoDoc\DocViewerResponse with status, headers, and either body or filePath. Call emit() in plain PHP. If your framework manages responses, build its response from those properties instead.

Open this route to view the generated documentation. For file exports, see Workspaces. If the output is incomplete or too broad, see tips to improve the generated documentation.

Previous
Overview