Skip to main content

Laravel Basics & API Development

 Laravel Basics & Introduction 

How to install & create Laravel projects


Run these commands:

  1. composer global require laravel/installer
  2. composer create-project --prefer-dist laravel/laravel [app-name]
  3. cd [app-name]


Open the project folder in VSCode, and find the ‘.env’ file.


Configure it like so:

DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=8889

DB_DATABASE=laravel

DB_USERNAME=root

DB_PASSWORD=root


Note that the configurations above works on my mac running mamp. Your machine may vary.


Run the project by using this command:

  1. php artisan serve
  2. (optional) php artisan serve —port 8002


Then go to this link

-> http://127.0.0.1:8000


Create a controller

  1. php artisan make:controller [controller-name]


Create a model

  1. php artisan make:model [model-name]


API Development with Laravel


API - Application Programming Interface


How API works:

  1. Request - client initiate
  2. Receive - the API provider receive your request
  3. Response - API returns the requested data, usually in JSON format


API ada banyak jenis


|——————————————————————————————————————————|


HTTP (RESTful) methods:

  1. GET - Fetch data, takde writing, hanya read
  2. POST - Create satu resouce baru (data/object), 
  3. PUT - (update) Biasa guna utk upload file
  4. PATCH - (update) Biasa guna utk update sesetengah maklumat sahaja, bukan full. Tapi nk guna utk update whole record pun boleh je
  5. DELETE - Obviously guna utk delete


RESTful - Representative State Transfer


Banyak methods boleh guna utk satu resouce. Resouce tu akan support utk operation berbeza.


Examples:

  1. get - products/{id} - akan retrieve data all products. kalau letak id akan retrieve yg id tu je
  2. post - products/ - akan insert new products
  3. put/patch - products/{id} - akan update products yg ada id tu
  4. delete - products/{id} - akan delete products yg ada id tu


|——————————————————————————————————————————|


Request

  1. ada header & body, all http method

Header

  1. Simpan semua metadata, includes authentication

Response

  1. ada header & body, uniform output (usually JSON), HTTP status code


Metadata - data yg explain pasal data. eg: data is gambar, metadata is format, size, resolution, date taken


|——————————————————————————————————————————|


Authentication

  1. basic auth - username & pass, encoded base64
    1. login page
  2. api token - bearer token per user
    1. login page, mobile app
  3. api key - server generated token/key.
    1. app to api, api to api
  4. Oauth 2.0 - combination username/pass & token. more robust and complex
    1. login page, mobile app, user based auth with scope

**bearer token under authorization header


Authenticate - check if user is legit or nah

Authorization - check if user is noob ah hell or chad admin


|——————————————————————————————————————————|


Best Practice & Design Principle

  1. Atomic/singularity
  2. Follow standard and consistent naming conventions
    1. adheres to HTTP methods
    2. endpoints uses nouns instead of verbs
    3. responses using JSON
    4. uniform response structure with appropriate status code
  3. user versioning to mitigate backward incompatibility
    1. eg: 
      1. /api/v4/products
      2. /api/v1/products
    2. bila ada major changes yg tambah param/tukar logik
  4. always validate input and properly handle errors
  5. capture errors in logs for easier troubleshooting/debugging
  6. provide good support documentation


|——————————————————————————————————————————|


php artisan make:controller [controller-name] --api

php artisan route:list

php artisan make:model [model-name] --migration


Notes:

  1. make sure there are no semicolons in env file or it wont work
  2. make sure the routes method are correct for page with post

Comments