Laravel Basics & Introduction
How to install & create Laravel projects
Run these commands:
- composer global require laravel/installer
- composer create-project --prefer-dist laravel/laravel [app-name]
- 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:
- php artisan serve
- (optional) php artisan serve —port 8002
Then go to this link
Create a controller
- php artisan make:controller [controller-name]
Create a model
- php artisan make:model [model-name]
API Development with Laravel
API - Application Programming Interface
How API works:
- Request - client initiate
- Receive - the API provider receive your request
- Response - API returns the requested data, usually in JSON format
API ada banyak jenis
|——————————————————————————————————————————|
HTTP (RESTful) methods:
- GET - Fetch data, takde writing, hanya read
- POST - Create satu resouce baru (data/object),
- PUT - (update) Biasa guna utk upload file
- PATCH - (update) Biasa guna utk update sesetengah maklumat sahaja, bukan full. Tapi nk guna utk update whole record pun boleh je
- DELETE - Obviously guna utk delete
RESTful - Representative State Transfer
Banyak methods boleh guna utk satu resouce. Resouce tu akan support utk operation berbeza.
Examples:
- get - products/{id} - akan retrieve data all products. kalau letak id akan retrieve yg id tu je
- post - products/ - akan insert new products
- put/patch - products/{id} - akan update products yg ada id tu
- delete - products/{id} - akan delete products yg ada id tu
|——————————————————————————————————————————|
Request
- ada header & body, all http method
Header
- Simpan semua metadata, includes authentication
Response
- 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
- basic auth - username & pass, encoded base64
- login page
- api token - bearer token per user
- login page, mobile app
- api key - server generated token/key.
- app to api, api to api
- Oauth 2.0 - combination username/pass & token. more robust and complex
- 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
- Atomic/singularity
- Follow standard and consistent naming conventions
- adheres to HTTP methods
- endpoints uses nouns instead of verbs
- responses using JSON
- uniform response structure with appropriate status code
- user versioning to mitigate backward incompatibility
- eg:
- /api/v4/products
- /api/v1/products
- bila ada major changes yg tambah param/tukar logik
- always validate input and properly handle errors
- capture errors in logs for easier troubleshooting/debugging
- provide good support documentation
|——————————————————————————————————————————|
php artisan make:controller [controller-name] --api
php artisan route:list
php artisan make:model [model-name] --migration
Notes:
- make sure there are no semicolons in env file or it wont work
- make sure the routes method are correct for page with post
Comments
Post a Comment