Jaroslaw Nowak
Jaroslaw Nowak

Reputation: 115

Laravel REST API and frontend

I created a project in Laravel, small database and added REST API in laravel to connect mobile app with database. What should I use to get data from database in web application? Using laravel models is easy but is that a good way to create another controllers to handle forms etc instead using rest api controllers? Thanks

Upvotes: 0

Views: 5480

Answers (2)

Ramin eghbalian
Ramin eghbalian

Reputation: 2667

Laravel also support for Restful API in own way. for this

  • you create your controller in Api folder: php artisan make:controller Api/TestController
  • define your routes in routes/api.php :

    Route::group(['namespace' => 'Api'], function (){
      Route::group(['prefix' => '/test'], function () {
        Route::get('/', 'TestController@list);
        Route::get('/single', 'TestController@single');
      });
    });
    
  • create a resource collection for data that is an array of collection

    php artisan make:resource Api/Collections TestCollection this command create a collection in folder app/Http/Resources/Api/Collections open in and change toArray($request) function and add a function with($request) like following code :

    public function toArray($request)
    {
      return $this->collection->map(function ($item){
        return [
            'id' => $item->id, // $item is instance of Test model
            'name' => $item->name,
            'description' => $item->description,
        ];
      });
    
    }
    
    public function with($request) // optional : this method return with of response 
     {
       return [
        'status' => true
       ];
     }
    
  • so go to TestController and create a method for get all tests:

    public function list()
    {
       $tests = Test::all(); // your Test Model
       return new TestCollection($test); // TestCollection you created above
    }
    

this is return a json object that contains array of tests.

  • for get a single test : php artisan make:resource Api/Resources TestResource

    then go to TestResource in app/Http/Resources/Api/Collections and change code like following:

    public function toArray($request)
    {
     return [
        'id' => $this->id,
        'name' => $this->name, // $this is instance of Test model
        'description' => $this->description,
        'body' => $this->body,
        'diff_name' => $this->name_in_table  // you can change the name differ from name in model instance 
     ];
    }
    

so go to TestController and create a method for single test

public function single(Request $request)
{
    $test = Test::findOrFail($request->id);
    return new TestResource($test);
}

this a summary of Rest API in laravel. Hope you find it useful

Upvotes: 1

Bryan
Bryan

Reputation: 3494

With laravel, you can reuse your api endpoints by taking advantage of the CreateFreshApiToken middleware.

Then you will only need to create new controllers and methods to display views. All of the CRUD stuff can be reused.

Upvotes: 0

Related Questions