API Laravel 5.3 & 5.2

in api •  6 years ago 

API En Laravel 5.3 & 5.2

You hace tried to use API in a version of laravel 5.2 or 5.3? You'll know that isn't a specific API routes file for create a API-RESTFUL via JSON. Therefore I will teach you in a few simple steps how to make a connection via API to be able to communicate via JSON with any third party system.

Steps to follow:

  • Add an api_token
  • Edit your routes
  • Edit Middleware VerifyCsrfToken.php

Add an api_token

The first think you need to do is to add an api_token column to your users table. If you are just starting your application you can likely get away with modifying the user migration that ships with Laravel to include your new column.

Edit Your Routes

Second, we need to make sure that any routes that will be using Token Authentication are being protected by the auth:api middleware.
Use the following route group as an example of what your routes might look like.

Note: Typically when protecting routes from unauthenticated users, we use the auth middleware, but by appending :api to the end we are telling Laravel that we want to use the driver for the api guard which is set up in the config/auth.php and is defaulted to token.

At this point, any routes wrapped with your auth:api middleware are only accessible to those that visit the route with a valid api_token in their request.

Edit Middleware VerifyCsrfToken.php

If you have problems with the CSRF Protection Token, you need to disabled this token for the 'api/v1/*', only in this way you can enter the url. This file is located on app/Http/Middleware/VerifyCsrfToken.php

###Extras

In the App\Http\Middleware\Authenticate middleware, you might want to change the following lines:

This will return a 401 status code to unauthorized API requests instead of redirect it to a login page.

EXAMPLE

If you want use your new API - ULR you need to add the parameter api_token in your URL, for example if you create a user with the api_token = test, On your API-URL you need to add the api_token of this mode:

http://localhost:8000/api/v1/example/?api_token=test&other_parameter=123

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

@ericktabora92, I gave you an upvote on your post! Please give me a follow and I will give you a follow in return and possible future votes!

Thank you in advance!

Congratulations @ericktabora92! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

You published your First Post
You got a First Vote

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - Round of 16 - Day 4


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @ericktabora92! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - Brazil vs Belgium


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @ericktabora92! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Congratulations @ericktabora92! You received a personal award!

1 Year on Steemit

Click here to view your Board

Do not miss the last post from @steemitboard:

SteemWhales has officially moved to SteemitBoard Ranking
SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @ericktabora92! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!