Invoke with Laravel.
- Install the package via composer:
composer require storinka/invoke-laravel:^2
- Register
InvokeProvider
in theconfig/app.php
:
return [
// ...
"providers" => [
// ...
\Invoke\Laravel\Providers\InvokeProvider::class,
],
// ...
];
- Register invoke route in the
routes/api.php
:
Route::any("/invoke/{method}", \Invoke\Laravel\Http\Controllers\InvokeController::class);
- Create folders for methods, data, types and validators:
mkdir app/Http/Methods \
app/Http/Data \
app/Http/Types \
app/Http/Validators
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'
Create a new method.
Example:
php artisan invoke:make:method GerUsers
Create a new data.
Example:
php artisan invoke:make:data UserData
Create a new type.
Example:
php artisan invoke:make:type SomeType
Create a new validator.
Example:
php artisan invoke:make:validator ValidEmail
Create a new pipe.
Example:
php artisan invoke:make:pipe ToUpperCase
$invoke = app(\Invoke\Invoke::class);
$invoke->setMethod("someMethod", SomeMethod::class);
$invoke->registeExtension(SomeExtension::class);
// etc..
$response = app(\Symfony\Component\HttpFoundation\Response::class);
$response->header('X-Some-Header', 'some value');