[Laravel] how to use lumen to build APIs

Source: Internet
Author: User
[Laravel] how to use lumen to build an API? what is API?

Application Programming Interface (Application Programming Interface) is a number of predefined functions to provide applications and developers with the ability to access a group of routines based on a software or hardware, without accessing the source code or understanding the details of the internal working mechanism.

What is lumen?

Lumen is a microframework built by Laravel components and officially maintained by Laravel. lumen is born for speed. it is one of the fastest PHP frameworks currently, and is even faster than similar microframeworks, Silex. from the above we can see that Lumen is a Laravel that removes many configuration and customizable options. That is to say, if you first understand Laravel, you can easily master lumen. There are many laravel articles in your blog, so you can search for them.

Use lumen to develop APIs

First, you must configure lumen and add the required JWT, dingo, and other components. But in our projects, these are all configured, so you don't have to worry about them.

Build a route

Open the project directory app/Http/routes. php

 Version ('v1. 0', ['namespace' => 'app \ Http \ V1_0 \ controllers'], function ($ api) {// item category list $ api-> get ('category', 'categorycontroller @ index'); // verify whether the project is running successfully $ api-> get ('test ', 'apitestcontroller @ test'); // you need to provide JWT $ api-> group (['middleware '=> 'api. auth ', 'Providers' => 'jwt '], function ($ api ){});});

The above is the interface that was initially opened. many people may not understand what it means. The lumen document is not the same as above. In fact, the above is not the native lumen syntax. the above uses the dingo API. You can go to the following link to learn about Wiki documentation. Here is a brief introduction.

$ Api-> get ('test', 'apitestcontroller @ test'); // access the local address/api/test. the call method is the test method of ApitestController.

In this example

$api->group(['middleware' => 'api.auth', 'providers' => 'jwt'], function ($api) { }); 

The written route must pass JWT verification, that is, you must log on to obtain the token. You can go to the JWT official website or search for related articles to learn about the token mechanism. WIKI can learn how to obtain and parse tokens. Of course, if you do not need to obtain logon information for your task, you can skip this step.

The routes must follow the restful url specification. You can read this article restful url

The most used is laravel resource routing.

Controller

Do you still remember the route mentioned in the previous step? the route calls the test method of ApitestController. Next, let's write down this controller.

Open the Project directory

App/Http/V1_0/Controllers. the Controllers of our project are all written in this folder, and ApitestController is created as needed.

 Response ()-> array (['api project succeeded ']);}

There is a test method here, which is the method we accessed in the route in the previous step. That is to say, when we access the local address/api/test, we will respond to 'api project running successfully '. The process of our API is basically complete. But there may be many students asking about the database? In fact, the database only provides data for this Api.

Database

Here we will show how the database data is used in the controller. First, create a new migration. refer to this article. Sometimes you need to fill in the data.

Refer to this article

Then we need to create a Model. Suppose we have a users table.

Open the app/Entity folder and create the file User. php.

     

$ Fillable is a whitelist that allows batch operations. for details, visit the official website. Next we can use the users table. Change the method

public function test(){$users = User::all();return $this->response->collection($users, new UserTransformer);} 

Add use App \ Entity \ User;

Transformer is used here. you must create a new file. you can read this article. If you do not want to create a connection, you can directly return $ users; in this way, the users table data will be returned when you access the connection. For more information, see the database section in the official documentation. Many times, for convenience, we can separate the database layer by referring to this article.

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.