1. Go to this page and download the library: Download laragear/api-manager 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/ */
laragear / api-manager example snippets
use App\Http\Apis\Chirper;
$chirp = Chirper::api()->chirp('Hello world!');
namespace App\Http\Apis;
use Laragear\ApiManager\ApiServer;
use Laragear\ApiManager\Attributes\Action;
#[Action('latest', '/')]
#[Action('create', 'post', 'new')]
class Chirper extends ApiServer
{
/**
* The headers to >isProduction()
? 'https://chirper.com/api/v1'
: 'https://dev.chirper.com/api/v1';
}
/**
* Returns the Bearer Token used for authentication.
*/
protected function authToken(): string
{
return config('services.chirper.secret');
}
}
namespace App\Http\Apis;
use Laragear\ApiManager\ApiServer;
use Laragear\ApiManager\Attributes\Action;
#[Action('newChirp', 'post', 'new')]
#[Action('latest', 'latest')]
#[Action('view', 'chirp/{id}')]
#[Action('edit', 'update', 'chirp/{id}')]
#[Action('delete', 'delete', 'chirp/{id}')]
class Chirper extends ApiServer
{
// ...
}
use App\Http\Apis\Chirper;
$latestChirps = Chirper::api()->latest();
use App\Http\Apis\Chirper;
$latestChirps = Chirper::api()->latest;
use App\Http\Apis\Chirper;
// Create a new chirp.
$chirp = Chirper::api()->newChirp(['message' => 'This should be complex']);
use App\Http\Apis\Chirper;
// Edit a chirp.
Chirper::api(['id' => 231])->edit(['message' => 'No, it was a breeze!']);
// Same as:
Chirper::api('chirper')->withUrlParameters(['id' => 231])->edit(['message' => 'No, it was a breeze!']);
use Illuminate\Http\Client\PendingRequest;
public function newChirp(PendingRequest $request, string $message)
{
return $request->connectTimeout(10)->post('new', ['message' => $message]);
}
public function noReply(PendingRequest $request)
{
$request->withHeaders(['X-No-Reply' => 'false'])
return $this;
}
use App\Http\Apis\Chirper;
$chirp = Chirper::api()->newChirp('Easy peasy');
use App\Http\Apis\Chirper;
$latest = Chirper::api()->noReply->newChirp('Easy peasy');
/**
* Returns the Basic Authentication to use against the API.
*
* @var array{string, string}|array<string>|void
*/
public function authBasic(): array
{
return app()->isProduction()
? ['[email protected]' => 'real-password']
: ['[email protected]' => 'fake-password'];
}
> // This is still supported, but discouraged!
> return ['username' => 'app@chirper', 'password' => 'real-password'];
>
use Illuminate\Http\Client\PendingRequest;
public function beforeBuild(PendingRequest $request)
{
//
}
public function afterBuild(PendingRequest $request)
{
//
}
use App\Http\Apis\Chirper;
$chirp = Chirper::api()->timeout(5)->latest();
use Illuminate\Filesystem\Filesystem;
use Laragear\ApiManager\ApiServer;
class Chirper extends ApiServer
{
public function __construct(protected Filesystem $file)
{
if ($this->file->missing('important_file.txt')) {
throw new RuntimeException('Important file missing!')
}
}
// ...
}
// app\Providers\AppServiceProvider.php
use App\Http\Apis\Chirper;
public function register()
{
$this->app->bind(Chirper::class, function () {
return new Chirper(config('services.chirper.version'));
})
}
use Illuminate\Support\Facades\Http;
use App\Http\Apis\Chirper;
use App\Http\Apis\Twitter;
$responses = Http::pool(fn ($pool) => [
Chirper::api()->on($pool)->chirp('Hello world!'),
Twitter::api()->on($pool)->tweet('Goodbye world!'),
$pool->post('mastodon.org/api', ['message' => 'Greetings citizens!'])
]);
return $responses[0]->ok();
use Illuminate\Support\Facades\Http;
use App\Http\Apis\Chirper;
use App\Http\Apis\Twitter;
$responses = Http::pool(fn ($pool) => [
Chirper::api()->on($pool, 'first')->chirp('Hello world!'),
Twitter::api()->on($pool, 'second')->tweet('Goodbye world!'),
$pool->as('third')->post('mastodon.org/api', ['message' => 'Greetings citizens!'])
]);
return $responses['first']->ok();
namespace App\Http\Apis\Chirper\Responses;
use Illuminate\Http\Client\Response;
class ViewResponse extends Response
{
//
}
class ViewResponse extends Response
{
/**
* Determine if the chirp is private.
*/
public function isPrivate(): bool
{
return $this->json('metadata.is_private', false)
}
}
use Laragear\ApiManager\Attributes\Response;
#[Response(Responses\ViewResponse::class)]
public function view()
{
//...
}
use App\Http\Apis\Chirper;
$chirp = Chirper::api(['id' => 5])->view();
if ($chirp->successful() && $chirp->isPrivate()) {
return 'The chirp cannot be seen publicly.';
}
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Client\Request;
public function test_creates_new_chirp(): void
{
Http::fake(function (Request $request) {
return Http::response([
'posted' => 'ok',
...json_decode($request->body())
], 200);
});
$this->post('spread-message', ['message' => 'Hello world!'])
->assertSee('Posted!');
}