Skip to content

Saad-Amjad/laravel-restful-apis

Repository files navigation

RESTful APIs using Laravel

Introduction

Pre Workshop Webinar:

Introduction to Advanced Web Development

Slides

https://slides.com/saadbinamjad/intro-to-advanced-web-development

Video

https://www.facebook.com/watch/?v=34735400019038

Workshop Structure

Session 1

  • Workshop Introduction
  • RESTful API Concepts
  • RESTful API Design

Session 2

  • Laravel Essentials
  • Bootstrapping with Laravel

Session 3

  • Building RESTful APIs
  • Open API Specification
  • Coding Tasks.

Session 4

  • Code Review
  • Solution Discussion.

Playground

Whimsical

Local Playground:

Reading Materials

API

HTTP

REST

REST API Design

Why no HATEOAS

Best Practices:

Laravel Learning Paths

Tasks

We want to create an online book store that allows users to browse through different genres of books and then if they want to purchase it, they need to sign in the system and order for their copy. The Admin, the store owner, can see the orders and then decide to ship them according to the user addresses.

Pre-requisites

Git

Github Account

With Docker

Docker: https://docs.docker.com/get-docker/

Code Editor: like VSCode or get the free trail version of PHPStorm,

Without Docker

If no docker, then you have to install Composer, PHP (Preferred PHP 8, PHP 7.4 would also do), MySQL, Git, Redis.

Open API Specification: https://swagger.io/tools/swagger-editor/


PHP

  • Create a file named: index.php.

  • Drop this code snippet:

<!DOCTYPE html>
<html>
<body>
<h1>Saad</h1>
<?php
echo "Hello World!<br>";
?>
</body>
</html>
  • Requires a web server to run its code.

  • Comes with a built in server, when you install PHP.

  • And then run the following command:

    php -S localhost:3000 index.php

In order to create web applications, and to have the best practises for reusuable scalable and maintainable PHP code, we resort to PHP frameworks.

Laravel

Benefits of Framework:

  • Out-of-the-box application architecture.

  • Faster development.

  • Highly testable.

  • Scalable

  • Loosely coupled

  • Easily maintainable applications.

Principles

- Model View Controller
- Laravel Codebase
- Laravel Essentials
- Bootstrapping with Laravel Breeze and JetStream

Examples

Task

We want to create an online book store that allows users to browse through different genres of books and then if they want to purchase it, they need to sign in the system and order for their copy. The Admin, the store owner, can see the orders and then decide to ship them according to the user addresses.

Sample GET Books API Task

{
    "books": [
        {
            "title": "demo 1",
            "author": "saad1",
            "id": 1,
            "book_url": "api/v1/books/1",
            "author_url": "api/v1/authors/saad1"
        },
        {
            "title": "demo 2",
            "author": "saad2",
            "id": 2,
            "book_url": "api/v1/books/2",
            "author_url": "api/v1/authors/saad2"
        }
    ]
}

With Pagination and Meta

{
  "data": [
    {
      "id": 1,
      "title": "Compatible exuding software as",
      "book_url": "api\/v1\/books\/1",
      "author": "Bethany Funk III",
      "author_url": "api\/v1\/authors\/1"
    },
    {
      "id": 2,
      "title": "Open-source asynchronous encoding",
      "book_url": "api\/v1\/books\/2",
      "author": "Bethany Funk III",
      "author_url": "api\/v1\/authors\/1"
    },
    {
      "id": 3,
      "title": "Configurable regional budgetarymanagement",
      "book_url": "api\/v1\/books\/3",
      "author": "Verona Lubowitz",
      "author_url": "api\/v1\/authors\/2"
    },
    {
      "id": 4,
      "title": "Ergonomic actuating support",
      "book_url": "api\/v1\/books\/4",
      "author": "Verona Lubowitz",
      "author_url": "api\/v1\/authors\/2"
    },
    {
      "id": 5,
      "title": "Business-focused bifurcated help-desk",
      "book_url": "api\/v1\/books\/5",
      "author": "Gerda Dare I",
      "author_url": "api\/v1\/authors\/3"
    }
  ],
  "links": {
    "first": "http:\/\/localhost:8000\/api\/v1\/books?page=1",
    "last": "http:\/\/localhost:8000\/api\/v1\/books?page=2",
    "prev": null,
    "next": "http:\/\/localhost:8000\/api\/v1\/books?page=2"
  },
  "meta": {
    "current_page": 1,
    "from": 1,
    "last_page": 2,
    "links": [
      {
        "url": null,
        "label": "&laquo; Previous",
        "active": false
      },
      {
        "url": "http:\/\/localhost:8000\/api\/v1\/books?page=1",
        "label": "1",
        "active": true
      },
      {
        "url": "http:\/\/localhost:8000\/api\/v1\/books?page=2",
        "label": "2",
        "active": false
      },
      {
        "url": "http:\/\/localhost:8000\/api\/v1\/books?page=2",
        "label": "Next &raquo;",
        "active": false
      }
    ],
    "path": "http:\/\/localhost:8000\/api\/v1\/books",
    "per_page": 5,
    "to": 5,
    "total": 10
  }
}

Todos / Steps

  • Installation

  • Design APIs (see above task)

  • Migration

    • php artisan make:migration create_books_table
    • php artisan make:migration create_authors_table
  • Relationships

    • php artisan make:migration add_fk_to_books_table
  • Models

    • php artisan make:model Book
    • php artisan make:model Author
  • Model Relationship

    • Author hasMany Books.
    • Books BelongsTo author.
  • Controllers

    • php artisan make:controller BookController
    • php artisan make:controller AuthorController
  • Create Routes

  • Create Factory

    • php artisan make:factory AuthorFactory
    • php artisan make:factory BookFactory
  • Create Seeder

    • php artisan db:seed
  • Create Resource

    • php artisan make:resource BookResource
    • php artisan make:resource BookCollection
  • Feature Tests

    • php artisan make:test BookTest
    • Run: ./vendor/bin/phpunit
  • [Optional] Install Sanctum

    • Optional: Install sanctum composer require laravel/sanctum
    • Enable sanctum
    • Enable middleware
  • [Optional] Login / Register Routes

    • Create AuthController
    • Register Api
    • Login Api
  • Predis

  • Cache

  • Swagger lumen

    • composer require "darkaonline/l5-swagger"
    • php artisan l5-swagger:generate
    • Create doc block and Schemas
    • Generate Docs and see: http://localhost:8000/api/documentation
  • Laravel Sail

  • JWT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published