Prepare RESTful Web Services using Laravel: The PHP Framework

on

I prepare this document to remember these steps. Hope it will help others too.

Step 1: Install laravel
Laravel documentation is very resource full. To install laravel please follow the bellow link:
https://laravel.com/docs/5.4
1st have to fulfill all prerequisite! Without these, laravel will not run properly.
Step 2: Make Database schema using migration.
Now 1st design your application database schema. Suppose you need 5 tables for your application.
Now go to the project directory in command line and run these commands.


php artisan make:migration create_users_table --create=users
php artisan make:migration create_books_table --create=books
php artisan make:migration create_authors_table --create=authors
php artisan make:migration create_categorys_table --create=categorys
php artisan make:migration create_publishers_table --create=publishers

Wait! You are not done yet!
Now you have modify all these files to add necessary table fields.

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('authors', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->bigIncrements('id');
$table->text('title')->nullable();
$table->timestamps();
});
}

To know more please follow this link:
https://laravel.com/docs/5.4/migrations#columns
Step 3: Create Tables in database
1st configure your database connection properly in .env file.
Then run this command:
php artisan migrate
Step 4: Make models
Now make your models by commands which will represent database tables.

php artisan make:model Book
php artisan make:model Author
php artisan make:model Catergory
php artisan make:model Publisher

Wait, you have to modify these files to map with corresponding db table:

class Author extends Model
{
protected $table = 'authors';
}

Step 5: Make resource controller
Now make controller by commands:

php artisan make:controller BookController --resource
php artisan make:controller AuthorController --resource
php artisan make:controller CatergoryController --resource
php artisan make:controller PublisherController --resource

How resource controller works?? To know more please follow this link:
https://laravel.com/docs/5.4/controllers#resource-controllers
To return all author in this request http://projectname/public/api/authors

public function index()
{
$data = Author::all();
return response()->json($data);
}

 

Step 6: Make resource controller
Wait, Not done yet!
Finally you have to configure all your routes to work properly!
For my project it was api.php file.

Route::resource('books', 'BookController');
Route::resource('authors', 'AuthorController');
Route::resource('catergorys', 'CatergoryController');
Route::resource('publishers', 'PublisherController');

You will find more information in resource controller documentation about this resource routing.

Step 7: Done!
Finally you have done this. Now test your API using any client (browser, postman)

Thanks everybody. Cheers

2 thoughts on “Prepare RESTful Web Services using Laravel: The PHP Framework

Leave a Reply

Your email address will not be published. Required fields are marked *