Core concepts

TypeScript export

Keep your frontend and backend in sync – define your types once in PHP and let PHP autodoc generate the matching TypeScript types automatically.

PHP autodoc supports exporting enums, classes, and request/response structures as TypeScript types. PHP autodoc-laravel extends this functionality by offering support for Laravel models and other Laravel-specific structures that need custom handling.

To use this feature, add @autodoc annotations to your TypeScript code (see examples below) and run one of the following commands:

For Laravel:

php artisan autodoc:ts-sync

For other PHP projects:

vendor/bin/ts-sync --config="/path/to/your/autodoc/config.php"

Optionally, you can add additional parameter to any of the commands above to specify the directory of your TypeScript code. If omitted, PHP autodoc will look for it in your autodoc config file under typescript.working_directory.

Configuration options

In your autodoc configuration file, you have these configuration options available:

    'typescript' => [
        'working_directory' => '/path/to/your/typescript/code',
        'file_extensions' => ['ts', 'tsx', 'vue'],
        'indent' => '    ',
        'string_quote' => "'",
        'add_semicolons' => false,
        'show_values_for_scalar_types' => true,
    ],

You may also customize the output for specific classes/enums using TypeScript export extensions.

Examples

Example 1 - Enum

Consider the following PHP enum:

namespace App\Enums;

enum SomeEnum: int {
    case A = 1;
    case B = 2;
    case C = 3;
}

In your typescript file:

/**
 * @autodoc App\Enums\SomeEnum
 */

After running the command:

/**
 * @autodoc App\Enums\SomeEnum
 */
enum SomeEnum {
    A = 1,
    B = 2,
    C = 3,
}

If the enum/class already exists in the typescript file, it will be updated.

Example 2 - Response structure

Consider the following PHP return statement from the GET /api/user route handler:

return [
    'id' => $user->id,
    'name' => $user->name,
    'email' => $user->email,
];

In your typescript file:

/**
 * @autodoc GET /api/user
 */
interface UserResponse {
    // empty or outdated data
}

After running the command:

/**
 * @autodoc GET /api/user
 */
interface UserResponse {
    id: number
    name: string
    email: string
}

Available annotations

@autodoc {class-name?}

Exports a class or enum.
Example: @autodoc Namespace\ClassName

@autodoc {http-method} {route}

Exports response structure of the route.
Example: @autodoc GET /api/user

@autodoc {http-method} {route} {http-status}

Exports response structure for a specific HTTP status.
Example: @autodoc POST /api/message 201

@autodoc {http-method} {route} request

Exports request body structure of the route.
Example: @autodoc GET /api/user request

Previous
Extensions