Web
Analytics Made Easy - StatCounter

Fill the form & Get Installation Support

GET IN TOUCH WITH US NOW

Laravel Installation+API

What is an API ?

API stands for Application Programming Interface. An API is a set of subroutine definitions, protocols and tools for building applications.

You can create a good API by applying all building blocks which is required to create an API.

What is RESTful API’s ?

Here REST stand’s for Representational State Transfer.

A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE  to access and use data. That data can be used to GET, PUT, POST and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources.

RESTful API in the sense if you are developing any API you have to follow some circumstances like, if you are fetching any data & that is not exists in database table , the you have to handle that situation in your program like the record record doesn’t exists or whatyever you want to give message , then you can.

If you are using any third party source like you need to integrate twilio. In trial version you will not allow to send message using your created API’s till you ll not verify your number in twilio account. So there you can use try catch for handle error & give that message & status code you are getting from the outer application  side in response. In another way you can set the status as 1 means there is no error in the program or response & vice versa. So if anyone use that API , the one will get easily what the issue is. 

API’s always return response in json format.

Steps to get how API’s are working :

1. Create a project in laravel :

To install new laravel project, run command given below :

After installation of laravel , go to the project folder & run the below command . You will get the serving platform of project. (http://127.0.0.1:8000/)

2. Install LARAVEL ui package in laravel app by using the following command :

3. Install Bootstrap Auth Scaffolding

4. Install Npm Packages

5. Then type the following command on cmd to run npm:

6. Create database. Here i have named database as restapi.

7. Set database configuration in .env file as given in snapshot :

8. After that run the following command to get the default tables of laravel.

9. Now fire command to get the auth functionality from where you can register & login on web.

Now do register by which we can create an API for get users records

Now create a controller where the API code will placed . You can use command to create controller with resource functions :

Here is simple API to get users list :

Add api routes routes/api.php file.

Here is API route to get users list as given below.

Run the created API in postman. Please refer screenshot given below.

According to validation point of you , as we can say it rest API , we can handle by checking requests backend side.

For example, in users table i have records. In that i am going to find users details of that id which is not exists in table , we can use their validation. If there will be validation will be mobile application side, you also have to check each and every point backend side to get the proper result.

According to laravel coding standard , you can use the resource functions like :

  • index (use the function for listing)
  • create (use the function to display user add page)
  • store  (use the function to add post user record)
  • edit  (use the function to display edit page)
  • update (use the function to update post user record)
  • destroy (use the function to delete record from database)

Here i am trying to get detail of user where the user ID is not exists in table :

You can handle the same using validation that the requested userId is exists in table or not.
So , after that there is no need to check in else part that the related data of requested User ID exists in table or not.

This is also a code optimisation part according to coding standard.

Here is API route to get user detail :

When you will enter correct user Id you ll get response . Please refer screenshot given below :

HTTP status code while creating rest API’s : 

With REST APIs, in addition to the HTTP methods, other factors including the HTTP status codes and their meanings are also defined. The following listing gives a brief overview.

  • 200: OK, no errors occurred
  • 201: Object has been created
  • 204: No contents – indicates that an action was successful but did not return any content
  • 206: Partial contents – used e.g. with paginated content
  • 400: Invalid request – standard response code if request was not valid
  • 401: Unauthorized – either the user is not authorized or a log-in is required
  • 403: Denied – the user is logged in but is not authorized to perform this action
  • 404: Not found – the desired resource or endpoint does not exist
  • 500: Internal server error – should not normally occur, but indicates that an unexpected error has occurred at the server while processing the request
  • 503: Service not available – indicates that the requested resource or endpoint is temporarily unavailable

The use of these status codes is not mandatory, but if development is to be REST-compliant, they are indispensable.

Leave a Reply

Your email address will not be published.

Fill the below form if you have any Laravel programming need.

Let’s get started!