ericktabora92 avatar

API Laravel 5.3 & 5.2

ericktabora92

Published: 05 Jul 2018 › Updated: 05 Jul 2018API Laravel 5.3 & 5.2

API Laravel 5.3 & 5.2

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

Leave API Laravel 5.3 & 5.2 to:

Written by

Read more #api posts


Best Posts From ericktabora92

We have not curated any of ericktabora92's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From ericktabora92