PHP code example of serhiikamolov / laravel-jsonapi
1. Go to this page and download the library: Download serhiikamolov/laravel-jsonapi 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/ */
namespace App\Http\Controllers;
use JsonAPI\Response\Response;
use App\Http\Requests\Auth\LoginRequest;
class AuthController extends Controller
{
/**
* Request a JWT token
*
* @param LoginRequest $request
* @param Response $response
* @return JsonResponse
*/
public function login(LoginRequest $request, Response $response):Response
{
// validation is passed, you can check the user credentials now
// and generate a JWT token
}
}
$response->serialize($collection, $serializer = new Serializer())
$response->serialize($collection)->paginate()
$response->code($statusCode)
public function login(LoginRequest $request, Response $response): Response
{
...
return $response
->token((string)$token)
->attach('uuid', Auth::guard('api')->user()->uuid);
}
namespace App\Http\Serializers;
use JsonAPI\Response\Serializer;
class UserSerializer extends Serializer
{
public function fields(): array
{
return [
'id', // take data from $user->id
'name', // take data from $user->name
'email', // take data from $user->email
'uuid' // take data from the public method defined below
];
}
/**
* Define a custom field
*/
public function uuid(Model $item): string
{
return md5($item->id);
}
}
class UsersController extends Controller
{
/**
* Get list of all users.
*
* @param Response $response
* @return Response
*/
public function read(Response $response): Response
{
$users = User::all();
return $response->serialize($users, new UserSerializer());
}
}
class UserSerializer extends Serializer
{
public function fields(): array
{
return [
'id' => 'md5' // use custom modifier
...
'created_at' => 'timestamp', // use default modifier which
// transforms a Carbon date object
// into the unix timestamp number
'roles' => RoleSerializer::class // use a serializing class as a modifier
// for the related data
];
}
/**
* Define custom modifier which transforms user id to md5 hash.
* @param int|null $value
* @return int
*/
protected function modifierMd5(?int $value): string
{
return md5($value);
}
}
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middlewareGroups = [
...
'api' => [
...
\JsonAPI\Http\Middleware\JsonApiDebug::class
],
...
];
}
namespace Tests;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use JsonAPI\Traits\Tests\JsonApiAsserts;
abstract class TestCase extends BaseTestCase
{
use JsonApiAsserts;
}
/**
* Testing GET /api/v1/entries/<id>
*/
public function test_read()
{
$response = $this->get("/api/v1/entries/1");
// expecting to get response in JSON:API format and
// find "id", "value", "type", "active" fields within
// a response's data
$this->assertJsonApiResponse($response, [
"id",
"value",
"type",
"active",
]);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.