Skip to content

Create fast and modern APIs with Laravel and Invoke

Notifications You must be signed in to change notification settings

storinka/invoke-laravel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Invoke Laravel

Invoke with Laravel.

Installation

  1. Install the package via composer:
composer require storinka/invoke-laravel:^2
  1. Register InvokeProvider in the config/app.php:
return [
    // ...
    
    "providers" => [
        // ...
        \Invoke\Laravel\Providers\InvokeProvider::class,
    ],
    
    // ...
];
  1. Register invoke route in the routes/api.php:
Route::any("/invoke/{method}", \Invoke\Laravel\Http\Controllers\InvokeController::class);
  1. Create folders for methods, data, types and validators:
mkdir app/Http/Methods \
 app/Http/Data \
 app/Http/Types \
 app/Http/Validators

Usage

Create a type:

// app/Http/Data/UserResult.php

use Invoke\Data;

class UserResult extends Data
{
    public int $id;
    
    public string $name;
    
    public string $email;
}

Create a method:

// app/Http/Methods/Dec2Hex.php

use Invoke\Method;
use App\Http\Types\UserResult;

class GetUserById extends Method
{
    public int $id;
    
    protected function handle(): ?UserResult
    {
        $user = User::find($this->id);
        
        return UserResult::nullable($user);
    }
}

Register the method:

// config/methods.php

return [
    \App\Http\Methods\GetUserById::class,
];

Try to invoke:

curl 'http://localhost:8000/api/invoke/getUserById?id=1'

Artisan commands

invoke:make:method

Create a new method.

Example:

php artisan invoke:make:method GerUsers

invoke:make:data

Create a new data.

Example:

php artisan invoke:make:data UserData

invoke:make:type

Create a new type.

Example:

php artisan invoke:make:type SomeType

invoke:make:validator

Create a new validator.

Example:

php artisan invoke:make:validator ValidEmail

invoke:make:pipe

Create a new pipe.

Example:

php artisan invoke:make:pipe ToUpperCase

Other

Accessing Invoke

$invoke = app(\Invoke\Invoke::class);

$invoke->setMethod("someMethod", SomeMethod::class);
$invoke->registeExtension(SomeExtension::class);
// etc..

Set response headers

$response = app(\Symfony\Component\HttpFoundation\Response::class);

$response->header('X-Some-Header', 'some value');

About

Create fast and modern APIs with Laravel and Invoke

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages