PHP code example of jasonej / laravel-endpoints

1. Go to this page and download the library: Download jasonej/laravel-endpoints library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jasonej / laravel-endpoints example snippets


#[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' => ['



declare(strict_types=1);

namespace App\Endpoints;

use Jasonej\LaravelEndpoints\Attributes\Get;
use Jasonej\LaravelEndpoints\Endpoint;

#[Get('/status')]
final class StatusEndpoint extends Endpoint
{
    public function __invoke(): array
    {
        return ['status' => 'ok'];
    }
}

use Jasonej\LaravelEndpoints\Attributes\Post;

#[Post('/posts', name: 'posts.store')]
final class CreatePostEndpoint extends Endpoint { /* … */ }

use Jasonej\LaravelEndpoints\Attributes\Route;

#[Route(['GET', 'POST'], '/webhook', name: 'webhook')]
final class WebhookEndpoint extends Endpoint { /* … */ }

use Jasonej\LaravelEndpoints\Attributes\Patch;
use Jasonej\LaravelEndpoints\Attributes\Put;

#[Put('/profile', name: 'profile.replace')]
#[Patch('/profile', name: 'profile.update')]
final class UpdateProfileEndpoint extends Endpoint { /* … */ }

#[Get(
    '/posts/{post}',
    name: 'posts.show',
    middleware: ['api', 'auth:sanctum'],
    withoutMiddleware: ['throttle'],
    wheres: ['post' => '[0-9]+'],
    domain: 'api.example.com',
    scopeBindings: true,
)]
final class ShowPostEndpoint extends Endpoint { /* … */ }

use App\Http\Resources\PostResource;
use App\Models\Post;

#[Get('/posts/{post}', wheres: ['post' => '[0-9]+'])]
final class ShowPostEndpoint extends Endpoint
{
    public function __invoke(Post $post): PostResource
    {
        return PostResource::make($post);
    }
}

#[Post('/posts')]
final class CreatePostEndpoint extends Endpoint
{
    public function __invoke(): PostResource
    {
        $post = Post::create([
            'title' => $this->string('title')->value(),
            'body' => $this->string('body')->value(),
            'published' => $this->boolean('published'),
        ]);

        return PostResource::make($post);
    }
}

use Illuminate\Support\Facades\Gate;
use App\Models\Post;

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

    public function rules(): array
    {
        return [
            'title' => ['

// config/endpoints.php
return [
    'paths' => [
        app_path(),           // default
        base_path('modules'), // add your own
    ],
];
shell
php artisan vendor:publish --tag="endpoints-config"
shell
php artisan vendor:publish --tag="endpoints-config"
shell
php artisan route:cache