Laravel api endpoint

From event4u-app/agent-config · View interactive version

Laravel api endpoint is an AI agent skill from the GitHub repository event4u-app/agent-config. Use when creating a new Laravel API endpoint — Controller, FormRequest, Resource, route, Policy, OpenAPI annotations — versioned route layout, single-action `__invoke` controllers.

How to use this skill

Skill file

laravel-api-endpoint

When to use

Use this skill when the project is Laravel (detected via artisan + composer.json with laravel/framework) and the user asks to create a new API endpoint, REST route, or controller action.

Routed in from CODE0 once the stack is detected as Laravel.

Do NOT use when:

Procedure: Create a Laravel API endpoint

  1. Read project docs — Check ./agents/ and AGENTS.md for controller conventions, resource patterns, routing.
  2. Create route — Add to the correct routes/api.php or module route file (routes/api/v{N}/{domain}.php).
  3. Create controller — Single-action invokable controller, thin, delegate logic to a service.
  4. Create FormRequest — Validate all input at the boundary; authorize via Policy in authorize().
  5. Create Resource — Transform model output via API Resource (never raw arrays / models / response()->json()).
  6. Create Policy — If authorization is needed and no Policy exists yet.
  7. Verify — Run PHPStan, run tests, confirm response shape matches conventions.

What to generate

  1. Controller — Single Action (invokable). Read agents/reference/docs/controller.md and ../../guidelines/php/controllers.md.
  2. FormRequest — Validation rules, authorize() via policies. Read ../../guidelines/php/validations.md.
  3. Resource — JSON response transformation. Read agents/reference/docs/api-resources.md.
  4. Route — Add to the correct versioned route file.
  5. Policy — If authorization is needed.
  6. Filter classes — If it's a list endpoint with filtering. Read agents/reference/docs/query-filter.md (if it exists).

Conventions

Show endpoint example

declare(strict_types=1);

namespace App\Http\Controllers\v1\Project;

use App\Http\Controllers\Controller;
use App\Http\Requests\v1\Projects\ShowProjectRequest;
use App\Http\Resources\v1\Project\ProjectResource;
use App\Models\ExternalCustomerDatabase\Project\Project;
use App\OpenApi\Schema\Request\ShowResourceRequestSchema;
use App\OpenApi\Schema\Response\ResourceNotFoundResponse;
use App\OpenApi\Schema\Response\ShowResourceResponseSchema;

class ShowProjectController extends Controller
{
    #[ShowResourceRequestSchema(path: '/projects/{id}', version: '1', resource: ProjectResource::class)]
    #[ShowResourceResponseSchema(ProjectResource::class, wrapInDataObject: false)]
    #[ResourceNotFoundResponse(ProjectResource::class)]
    public function __invoke(ShowProjectRequest $request, Project $project): ProjectResource
    {
        return ProjectResource::make($project);
    }
}

Create endpoint with service injection

class CreateCustomerController extends Controller
{
    #[CreateCustomerRequestSchema(path: '/customers', version: '1', resource: CustomerResource::class)]
    #[CreateResourceResponseSchema(resource: CreatedCustomerResource::class, wrapInDataObject: false)]
    #[ValidationErrorResponse]
    public function __invoke(
        CreateCustomerRequest $request,
        CustomerModelService $customerService,
    ): CustomerResource {
        $result = $customerService->create(CreateCustomerDTO::fromRequest($request));

        return CreatedCustomerResource::make($result);
    }
}

FormRequest example

declare(strict_types=1);

namespace App\Http\Requests\v1\Projects;

use Illuminate\Foundation\Http\FormRequest;

class ShowProjectRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()->can('view', $this->route('project'));
    }

    /** @return array<string, mixed> */
    public function rules(): array
    {
        return [];
    }
}

List endpoint with CollectionFormRequest

For list endpoints, extend CollectionFormRequest which provides perPage, page, and orderBy rules:

use App\Contracts\Http\Requests\CollectionFormRequest;

class ListProjectsRequest extends CollectionFormRequest
{
    public string $model = Project::class;

    /** @return array<string, mixed> */
    public function rules(): array
    {
        return [
            ...parent::rules(),
            'status' => ['sometimes', 'string'],
        ];
    }
}

File locations

| Component | Path |

|---|---|

| Controller | app/Http/Controllers/v{N}/{Domain}/{Action}{Entity}Controller.php |

| FormRequest | app/Http/Requests/v{N}/{Domain}/{Action}{Entity}Request.php |

| Resource | app/Http/Resources/v{N}/{Domain}/{Entity}Resource.php |

| Route | routes/api/v{N}/{domain}.php |

| Policy | app/Policies/{Entity}Policy.php |

OpenAPI documentation

Controllers use PHP 8 attributes for OpenAPI spec generation from App\OpenApi\Schema\:

Output format

  1. Generated files — controller, route registration, FormRequest, Resource, Policy.
  2. Test file with happy path and validation error cases.
  3. Summary of created files and their locations.

Gotcha

Do NOT

Auto-trigger keywords

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.