Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Thomas Gamborg Nørgaard

Rating No ratings yet

Build your API with confidence Are you unsure how to build an API with Laravel, or do you struggle with different conventions?650+ pages step by step guide that teaches you everything you need to know!What will you learn from this book? JSON:API — You will learn about the JSON:API specification and all the benefits. Documentation — You will learn how to create great documentation for your API. Security — You will learn how to secure your APIs using Laravel Passport. Building — You will learn how to implement the JSON:API Specification in Laravel. Testing — You will learn how to test your APIs, to ensure that they work as intended. Workflow — You will learn an excellent development workflow and stay productive, even if you are offline.

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# Build an API with Laravel — Reading Guide ## 【One-Line Pitch】 A comprehensive, hands-on guide for PHP developers who want to master the JSON:API specification and build production-ready, well-tested APIs with Laravel — covering everything from planning and documentation to authentication and test-driven development. ## 【Book Arc】 - **Opening (~0%–10%)**: Introduces the JSON:API specification and why it matters — solving the problem of inconsistent, unstructured API responses by defining a strict document layout with `id`, `type`, `attributes`, and `relationships` members. Establishes the bookstore case study used throughout the book. - **Early (~10%–23%)**: Covers the planning phase — gathering requirements from customers, identifying resources, attributes, and relationships, and documenting the API before writing code. Includes setting up Laravel Passport for authentication and understanding OAuth2 grant types. - **Early–Middle (~23%–39%)**: Moves into implementation — building controllers, using Eloquent API Resources to transform models into JSON:API-compliant responses, and implementing CRUD operations for resources. Introduces the test-driven workflow with Laravel's built-in testing tools. - **Middle (~39%–48%)**: Deepens the test-driven approach — writing tests first, then implementing features to make them pass. Covers validation, content negotiation (the `Accept: application/vnd.api+json` header), exception handling, sorting, and pagination. - **Late (~48%–end)**: Implements the book resource — the central resource of the application that ties together relationships, complex validation, and the full JSON:API feature set. The excerpts do not cover the final chapters in detail, but the pattern established suggests continued implementation of relationships and advanced features. ## 【Key Takeaways】 - **JSON:API provides a consistent document structure** (Early): Instead of Laravel's default model-to-JSON conversion that exposes data inconsistently, the specification mandates a top-level `data` member containing resource objects with `id`, `type`, `attributes`, and `relationships` — making APIs predictable for consumers. - **Planning before coding pays off** (Early): The book emphasizes interviewing customers, taking thorough notes, and writing requirement specifications divided into main application areas. Identifying attributes and relationships early enables early API documentation and frontend mocking. - **Eloquent API Resources are the Laravel-native way to transform models** (Early): Rather than manually building response arrays in controllers or creating custom transformer classes, API Resources provide a reusable convention for converting models and collections into JSON:API-compliant structures. - **Laravel Passport handles OAuth2 authentication** (Early): The book walks through installing Passport, adding the `HasApiTokens` trait to the User model, and implementing the authorization code and password credentials grant flows — including how to test authenticated endpoints. - **Test-driven development is central to the workflow** (Middle): The book shifts from writing tests after implementation to writing tests first, using Laravel's testing tools (PHPUnit, factories, `postJson`) and a custom package with a `@watch` annotation to automate test runs. - **Content negotiation is a non-negotiable part of JSON:API compliance** (Middle): Clients must send the `Accept: application/vnd.api+json` header, and the book shows how to enforce this and update existing tests when implementing it. - **Validation errors must follow the JSON:API error format** (Middle): Tests verify that missing or malformed `attributes` members produce structured error responses with `details` and `source.pointer` fields — not just generic Laravel validation messages. ## 【Reading Tips】 - **Skim the early planning chapters (~10%–19%)** if you're already comfortable with requirements gathering — the core value is in the JSON:API structure and the implementation chapters that follow. - **Deep-read the test-driven workflow chapters (~32%–48%)**: The book deliberately shows repetitive test-writing patterns to teach methodology. Pay attention to how tests are structured before implementation, not just the final code. - **Watch for the shift in approach**: Around the 42% mark, the book explicitly switches from writing tests after code to writing tests first. This is a key mindset change — don't skip it. - **Use the bookstore case study as your anchor**: Every example builds on the same domain (books, authors, comments), so if you follow along with the code, you'll have a complete working API by the end. - **The excerpts don't cover the final book resource implementation in detail** — if you're specifically interested in complex relationships and advanced JSON:API features, you'll need to read the later chapters directly. ## 【Coverage Limits】 This guide is based on excerpts covering roughly the first half of the book (through ~48%). The final chapters on implementing the book resource, advanced relationships, and any concluding topics are not covered in detail here. ##
Excerpt 1
Let’s have a look at the case. 13 BUILD AN API WITH LARAVEL We know that Laravel Models can be returned in a response, where Laravel will handle the whole co...
View in text
Excerpt 2
e end-users start working with the application. 90 PLANNING we expect to receive the data in the same format. Click the “Save” button next to the “Send” butt...
View in text
Excerpt 3
and then return to validation in a bit. Upda ng a resource When updating a resource, we need to send a resource object again. This time, the resource object...
View in text
Excerpt 4
longer. Let’s fix that. Back in the tests/FeatureAuthorTest.php, let’s start with the first test, where we can add the headers to the request like this: * @t...
View in text
Excerpt 5
t the database that • a. We see that no relationships exist We have written this test like so: * @test * @watch public function it_can_remove_all_relationshi...
View in text
Excerpt 6
dynamic abilities to take a string in an argument and then call the method on the model with the method name that matches that string. Back in our controller...
View in text
Excerpt 7
iling though, but it’s because of the same issue as before: we are only handling collections. So let’s go into app/Services/JSONAPISer- vice.php and take a l...
View in text
Excerpt 8
lass)->state('admin')->create(); Passport::actingAs($user); $this->postJson('/api/v1/books', [ 'data' => [ 'type' => 'books', 'attributes' => [ 'title' => 'B...
View in text
Tags
AI categories
BackendProgrammingFramework
Publish Year: 2019
Language: English
Pages: 746
File Format: PDF
File Size: 8.5 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…