Laravel Api
2021 January 31st
Now to build a RESTful API using Laravel 8.x learn by doing
.
As mobile development and JavaScript frameworks become more popular even the norm, it is nessarary to learn how RESTful apis work.
Laravel an open-source PHP model–view–controller (MVC) web framework, created by Taylor Otwell.
An opinionated PHP application framework as apposed to for example ExpressJS a minimal,
un-opinionated JavaScript framework of which i will cover in another post.
In this post I am going to a build an MVC App single interface with a user login to a MySql DataBase (DB). Feel free to explore / reference / download or clone the repo GitHub
REST stands for REpresentational State Transfer.
HTTP Verb | CRUD | Entire Collection (e.g. /users) | Specific Item (e.g. /user/{id}) |
---|---|---|---|
POST | Create | 201 (Created), 'Location' header with link to /users/{id} containing new ID. | 404 (Not Found), 409 (Conflict) if resource already exists.. |
GET | Read | 200 (OK), list of users. Use pagination, sorting and filtering to navigate big lists. | 200 (OK), single user. 404 (Not Found), if ID not found or invalid. |
PUT | Update/Replace | 405 (Method Not Allowed), unless you want to update/replace every resource in the entire collection. | 200 (OK) or 204 (No Content). 404 (Not Found), if ID not found or invalid. |
PATCH | Update/Modify | 405 (Method Not Allowed), unless you want to modify the collection itself. 200 (OK) or 204 (No Content). | 404 (Not Found), if ID not found or invalid. |
DELETE | Delete | 405 (Method Not Allowed), unless you want to delete the whole collection—not often desirable. | 200 (OK). 404 (Not Found), if ID not found or invalid. |
Getting set up
First install Get Composer a dependency manager for PHP. After composer installation run the below commands in your cli.
1$ composer global require laravel/installer2$ laravel new myapp3$ php artisan serve
Laravel localhost development server started:
1http://127.0.0.1:8000
Install MySQL, if using MacOS or Linux I recommend using https://brew.sh/ then run the below command.
1brew services start mysql
if you run into an issue like I did, having a prevoius installed version of MySQL here is the solution I used https://stackoverflow.com/a/6378429/1967126
I am using Migrate it's like version control for our DB read more here Laravel 8.x Migrations
1php artisan make:seeder ArticlesTableSeeder
1<?php23namespace Database\Seeders;45use Illuminate\Database\Seeder;67class ArticlesTableSeeder extends Seeder8{9 /**10 * Run the database seeds.11 *12 * @return void13 */14 public function run()15 {16 // lets: truncate our existing records to start from scratch17 Article:truncate();1819 $faker = \Faker\Factory::create();2021 // lets create some articles in our DB22 for($i = 0; $i < 50; $i++) {23 Article::create([24 'title' => $faker->sentence,25 'body' => $faker->paragraph26 ]);27 }28 }29}
php artisan make:seeder UsersTableSeeder
Database Seeding
API throttling and Authenication middleware: https://laravel.com/docs/8.x/middleware
Closure https://stackoverflow.com/a/47348663/1967126
Routes and Controllers Create the test folder (if it does not exist) then create TestController inside. https://stackoverflow.com/a/43674716/1967126 basic endpoints for our application: create, retrieve the list, retrieve a single one, update, and delete. On the routes/api.php file, we can simply do this:
1php artisan make:controller Article/ArticleController
HTTP Status Codes and the Response Format
Sending a Correct 404 Response
Laravel 8.x Handler.php return a JSON response, in Laravel 8x, render exceptions in the register() method. Laravel Errors: Rendering Exceptions
Laracasts.com/discuss/channels/laravel/laravel7 add Throwable
php artisan make:migration --table=users adds_api_token_to_users_table
Medium simple authentication Laravel 8
Laravel frontend introduction
npm install && npm run dev
php artisan ui vue --auth
Toptal RESTful Laravel API tut
free web hosting
Techradar: best free web hosting
React Basics