Tag: composer

Understand The Implementation of REST API In Laravel 5

In this blog, we are going to demonstrate the understanding to implement REST API in Laravel 5. Starting with some quick introduction part;
  • Rest API is backend implementing also it is a simple basic HTTP authentication to store data for different users of the application.
  • We will use the middleware to provide the authentication for the rest call.
  • The middleware will authorize the user at every request without storing in session or using token.
Implement REST API:
composer create-project restAPP –prefer-dist
  • This terminal command to we create the new application and that name is restAPP.
  • Then after we create the model for basic authentication and we are using the email for login parameter.
  • Complete the project creation after we have created the controller and model, using artisan command.
    php artisan make:model Todo
<?php 
namespace App;
 
use Illuminate\Database\Eloquent\Model;
use App\User;
 
 
class Todo extends Model {
 protected $fillable = ['title','isDone'];
    public function user(){
 
        return $this->belongsTo('App\User');
 
    }
 
}
In the model we have to add the reference to the user model and method to retrieve it.
    php artisan make:controller TodoController
<?php
namespace App\Http\Controllers;
 
use App\Http\Controllers\Controller;
use App\Todo;
use Auth;
use Request;
 
class TodoController extends Controller {
 
   /**
    * Display a listing of the resource.
    *
    * @return Response
    */
   public function index() {
 
         $todos = Todo::where('user_id','=',Auth::user()->id)->get();
     return $todos;
   }
 
   /**
    * Store a newly created resource in storage.
    *
    * @return Response
    */
   public function store() {
      $todo = new Todo(Request::all());
      $todo->user_id = Auth::user()->id;
      $todo->save();
      return $todo;
   }
 
 
}
In controller, we have to add a basic method to create and retrieve data from the logged users.
It’s all complete then we have to create a middleware to for authentication for possible to access data only if the user passes in the HTTP request his email and password. Let’s create middleware.
    php artisan make:middleware simpleAuthMiddleware
<?php namespace App\Http\Middleware;
 
use Closure;
 
class SimpleAuthMiddleware
{
 
   /**
    * Handle an incoming request.
    *
    * @param  \Illuminate\Http\Request $request
    * @param  \Closure $next
    * @return mixed
    */
   public function handle($request, Closure $next)
   {
      return Auth::onceBasic('username') ?: $next($request);
   }
 
}
After creating middleware we have to register in “ kernel.php “ file and give the key for easily use this middleware.
‘simpleauth’ => ‘App\Http\Middleware\SimpleAuthMiddleware’, After register this middleware we have to create the route path for Todo Controller and middleware
Route::get('api/todo', ['uses' => 'TodoController@index','middleware'=>'simpleauth']);
Route::post('api/todo', ['uses' => 'TodoController@store','middleware'=>'simpleauth']);
If you try with the postman on every request of email and password will be prompted. You will also pass this info on URL or HTTP header.

Hope this helped you to get most out of the system. Feel free to share your reviews or need assistance for Hire Laravel Developer then get in touch with us. Pick the best answer for your requirements.

Advertisements