"Go" simple Laravel 5 REST API

Source: Internet
Author: User

Introduction

Almost all successful internet based companies has APIs. API is a acronym for application Programming Interface. APIs allows different systems to communicate with one another. Let's say you has developed an Android application for our online store. The API can is used to retrieve data from the online store and display it in the mobile application. The API can also is used to process orders from remote clients such as mobile applications, and other websites etc.

Topics to be covered

We'll cover the following topics

    • What is a RESTful API?
    • REST API Best Practices
    • Larashop API
What is a RESTful API?

REST is the acronym-representational State Transition. It is a software architectural design for building scalable Web services. REST APIs allow systems to communicate over HTTP using HTTP verbs. HTTP GET is used-get resources, POST used to create new resources, PUT to update existing ones and delete to delete Exi Sting resources.

REST API Best Practices

This was a summary from the blog post we posted on kode blog Ten REST API Design best practices that'll make developers Lo ve Your API. Read the article for detailed explanations of this summary.

    1. Best Practice # 1:use HTTP VERBS to determine action to be taken
    2. Best Practice # 2:api Versioning
    3. Best Practice # 3:use plurals to describe resources
    4. Best Practice # 4:use Query strings to build relations
    5. Best Practice # 5:partial Responses
    6. Best Practice # 6:response Codes and Error handling
    7. Best Practice # 7:limit The number of request in a given time period from the same IP Address
    8. Best Practice # 8:use OAuth Latest version for authentication
    9. Best Practice # 9:use JSON as the default
    10. Best Practice # 10:cache GET results for less frequently changing data
Larashop API

For now, we'll only display the products and categories. Our API would implement basic authentication only. Future tutorial updates would include more functionality.

Our API would has the following URLs. All the URLs would use the HTTP verb GET

S/
N Resource URL Description Status Code
1 Product /api/v1/products List Products 200
2 Product /api/v1/products/1 List Product with ID 1 200
3 Category /api/v1/categories List Categories 200
4 Category /api/v1/categories/1 List category with ID 1 200

Let's now create the routes that would give us

    1. Open/app/Http/routes.php
    2. Add the following routes
1234567891011121314151617181920212223242526
API routes ...Route::Get('/api/v1/products/{id?} ',[' Middleware '=' Auth.basic ',function($id=Null){If($id==Null){$products=App\Product::All(Array(' ID ',' Name ',' Price '));}Else{$products=App\Product::Find($id,Array(' ID ',' Name ',' Price '));}ReturnResponse::Json(Array(' Error '=False,' Products '=$products,' Status_code '=200));}]);Route::Get('/api/v1/categories/{id?} ',[' Middleware '=' Auth.basic ',function($id=Null){If($id==Null){$categories=App\Category::All(Array(' ID ',' Name '));}Else{$categories=App\Category::Find($id,Array(' ID ',' Name '));}ReturnResponse::Json(Array(' Error '=> false ,            < Span class= "sunlight-string" > ' user '  =>   $categories ,              ' status_code '  

Here,

  • Route::get(‘/api/v1/products/{id?}‘, [‘middleware‘ => ‘auth.basic‘, function($id = null)Defines a RESTful URL for version 1 of the API. The requested resource is products. {ID?} Specifies an optional parameter. The ID is used to retrieve a single product. The API uses Basic authentication
  • The routes is calling the respective models to retrieve the data from the database.
  • return Response::json(…)Returns the results in JSON format.

Load the following URL in your Web browser

1
HTTP://localhost/larashop/public/api/v1/products 

You'll get the following Basic Authentication login window

User a registered email address and password from the previous tutorial on authetication you'll get the following result S

Summary

Building a basic REST API in Laravel are no more than retrieving data using models and formatting the response to JSON. The future tutorial updates would build a fairly complex API that would do more.

Tutorial history

Tutorial version 1:date Published 2015-08-31

Code files

"Go" simple Laravel 5 REST API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.