PHPackages                             jasonej/laravel-endpoints - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. jasonej/laravel-endpoints

ActiveLibrary

jasonej/laravel-endpoints
=========================

Single file endpoints for Laravel.

v0.1.0(1mo ago)01↓66.7%MITPHPPHP ^8.3||^8.4||^8.5CI passing

Since Jul 17Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/Jasonej/laravel-endpoints)[ Packagist](https://packagist.org/packages/jasonej/laravel-endpoints)[ RSS](/packages/jasonej-laravel-endpoints/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (10)Versions (2)Used By (0)

Laravel Endpoints
=================

[](#laravel-endpoints)

Single file endpoints for Laravel.

[![Latest Version on Packagist](https://camo.githubusercontent.com/d076b3d012e0e2e625fc86f76bdf3d32f0a5d489131f4b5d6ae3546bd40487d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736f6e656a2f6c61726176656c2d656e64706f696e74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jasonej/laravel-endpoints)[![Total Downloads](https://camo.githubusercontent.com/7f16286f4fbe91613d8f7a141a8507e991cd5615dd0801352f1e9954a8a8e413/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a61736f6e656a2f6c61726176656c2d656e64706f696e74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jasonej/laravel-endpoints)[![Tests](https://github.com/jasonej/laravel-endpoints/actions/workflows/tests.yml/badge.svg)](https://github.com/jasonej/laravel-endpoints/actions/workflows/tests.yml)

> A typical Laravel endpoint is spread across a route, a form request, and a controller. `laravel-endpoints` collapses all three into a single auto-discovered class: extend `Endpoint`, declare the route with an attribute, and handle the request in `__invoke`. Add `rules()` to validate and `authorize()` to gate access, just like a form request.

```
#[Post('/api/comments', middleware: ['api', 'auth:sanctum'])]
final class CreateCommentEndpoint extends Endpoint
{
    public function authorize(): bool
    {
        return Gate::allows('create', Comment::class);
    }

    public function rules(): array
    {
        return [
            'body' => ['required', 'string'],
        ];
    }

    public function __invoke()
    {
        // ...
    }
}
```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require jasonej/laravel-endpoints
```

You can publish the config file with:

```
php artisan vendor:publish --tag="endpoints-config"
```

Usage
-----

[](#usage)

### Defining an endpoint

[](#defining-an-endpoint)

Extend `Endpoint`, declare a route with an attribute, and handle the request in `__invoke`. No need to register it in `routes/api.php` as endpoints are auto-discovered.

```
