You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+25-21Lines changed: 25 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ Here, we are going to implement REST API with Passport authentication.
4
4
We will build a CRUD for Blog's API using Laravel Passport Authentication.
5
5
6
6
#### **What is REST API?**
7
-
Rest API (Representational State Transfer) api's are web standards base architecture and uses HTTP Protocol for exchanging data between applications or systems.
7
+
Rest API (Representational State Transfer) api's are web standards-based architecture and uses HTTP Protocol for exchanging data between applications or systems.
8
8
In RESTFUL web service HTTP methods like GET, POST, PUT and DELETE can be used to perform CRUD operations.
9
9
10
10
#### **What is Passport?**
@@ -18,7 +18,6 @@ The passport package will register its own database migrations. This command wil
18
18
19
19
#### **How to implement Passport in REST API?**
20
20
In below example, we will create CRUD operation for Blog's API using passport authentication.
21
-
So, let's start.
22
21
23
22
**1) Create laravel project.**
24
23
@@ -30,11 +29,11 @@ Go to your project path in terminal and run below command for passport authentic
30
29
31
30
composer require laravel/passport
32
31
33
-
After successfully installing the package, we are required to get default migration for creating new passport tables in our database. so let's run bellow command:
32
+
After the successful installation of a package, we required to get default migration for creating new passport tables in our database. let's run below command:
34
33
35
34
php artisan migrate
36
35
37
-
Next, we need to install passport using command, Using passport:install command, it will create token keys for security. So let's run bellow command:
36
+
Next, we need to install the Passport using command, and it will create token keys for security. let's run bellow command:
38
37
39
38
php artisan passport:install
40
39
@@ -48,7 +47,7 @@ In user model, use HasApiTokens trait class of passport.
48
47
use Illuminate\Foundation\Auth\User as Authenticatable;
49
48
use Illuminate\Notifications\Notifiable;
50
49
use Laravel\Passport\HasApiTokens;
51
-
50
+
52
51
class User extends Authenticatable
53
52
{
54
53
use Notifiable,HasApiTokens;
@@ -133,9 +132,9 @@ Change config/auth.php file.
133
132
.....
134
133
]
135
134
136
-
**4) Add Blog table and model**
135
+
**4) Create Blog table and model**
137
136
138
-
Create migration file and model for Blog table by below command.
137
+
Create migration file and model for the Blog table by below command.
139
138
140
139
php artisan make:migration create_blogs_table
141
140
@@ -177,7 +176,7 @@ Now open your database/migration/'your blog migration file'. and add your code h
177
176
}
178
177
}
179
178
180
-
Add below code to your Blog model file.
179
+
Update below code to your Blog model file.
181
180
182
181
<?php
183
182
@@ -205,13 +204,13 @@ Now open your database/migration/'your blog migration file'. and add your code h
205
204
206
205
**6) Create Controller files**
207
206
208
-
We will create two controller files. One for Register and another for Blogs by below command.
207
+
Create two controller files. One for Register and another for Blog. Use below commands to create files.
209
208
210
209
php artisan make:controller RegisterController
211
210
212
211
php artisan make:controller BlogController
213
212
214
-
Add below code to register Controller.
213
+
Update below code to register Controller.
215
214
216
215
<?php
217
216
@@ -256,7 +255,7 @@ Add below code to register Controller.
256
255
}
257
256
}
258
257
259
-
And add below code to BlogController
258
+
Update below code to BlogController
260
259
261
260
<?php
262
261
@@ -359,7 +358,7 @@ And add below code to BlogController
359
358
{
360
359
$blog = Blog::find($id);
361
360
362
-
//Check if blog found or not.
361
+
//Check if the blog found or not.
363
362
if (is_null($blog)) {
364
363
$message = 'Blog not found.';
365
364
$status = false;
@@ -429,15 +428,15 @@ Now run below command in terminal:
429
428
430
429
**8) Run APIs**
431
430
432
-
Let's run out api's through postman.
431
+
Let's run API through postman.
432
+
433
+
Run authentication API to get passport access token & copy the token and use the same in Header of other CRUD APIs.
434
+
Follow all screenshots to understand how to generate a token and use it in APIs to authenticate a user.
433
435
434
436
Test Register API:
435
437
436
438

437
439
438
-
Let's test blog's CRUD. but before that, we need authentication to perform CRUD operation. because we used passport authentication.
439
-
So, we will access 'token' from register API's response. and add the token to each API's headers like below:
440
-
441
440

442
441
443
442
Add Blog:
@@ -461,15 +460,15 @@ Delete Blog:
461
460

462
461
463
462
464
-
Now, we are going to write a test cases for register and CRUD api.
463
+
Writing test cases for the register and CRUD APIs.
465
464
466
-
So, let's make test files by below command.
465
+
Commands to create test case file :
467
466
468
467
php artisan make:test RegisterTest
469
468
470
469
php artisan make:test BlogTest
471
470
472
-
Add below code to `tests/Feature/RegisterTest.php` file.
471
+
Update below code to `tests/Feature/RegisterTest.php` file.
473
472
474
473
<?php
475
474
@@ -531,7 +530,7 @@ Add below code to `tests/Feature/RegisterTest.php` file.
531
530
}
532
531
}
533
532
534
-
Add below code to `tests/Feature/BlogTest.php` file.
533
+
Update below code to `tests/Feature/BlogTest.php` file.
535
534
536
535
<?php
537
536
@@ -724,11 +723,16 @@ Add below code to `tests/Feature/BlogTest.php` file.
724
723
}
725
724
}
726
725
727
-
now run the test cases to terminal by below command:
0 commit comments