1. Go to this page and download the library: Download blueink/snorlax 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/ */
blueink / snorlax example snippets
use Snorlax\RestResource;
use Snorlax\RestClient;
class PokemonResource extends RestResource
{
public function getBaseUri()
{
// You don't want a raw value like this, use an environment variable :)
return 'http://localhost/api/pokemons';
}
public function getActions()
{
return [
'all' => [
'method' => 'GET',
'path' => '/',
],
'get' => [
'method' => 'GET',
'path' => '/{0}.json',
],
'create' => [
'method' => 'POST',
'path' => '/',
],
];
}
}
$client = new RestClient([
'resources' => [
'pokemons' => PokemonResource::class,
],
]);
// GET http://localhost/api/pokemons?sort=id:asc
$response = $client->pokemons->all([
'query' => [
'sort' => 'id:asc',
],
]);
// GET http://localhost/api/pokemons/143.json?fields=id,name
$response = $client->pokemons->get(143, [
'query' => [
'fields' => 'id,name',
],
]);
// POST http://localhost/api/pokemons
$response = $client->pokemons->create([
'body' => [
'name' => 'Bulbasaur',
],
]);
public function parse($method, $response)
{
return $response->pokemon;
}
public function parse($method, $response)
{
return collect($response->pokemon);
}
public function parse($method, $response)
{
switch ($method) {
case 'all':
return collect($response->pokemon);
break;
case 'get':
return $response->pokemon;
break;
}
}
public function parse($action, $response)
{
$date_fields = [
'created_at',
'updated_at',
];
$response = $response->pokemon;
foreach ($date_fields as $date_field) {
if (property_exists($response, $date_field)) {
$response->{$date_field} = Carbon::parse($response->{$date_field});
}
}
return $response;
}