Section III: DINGO/API latest version of V2.0 Creating API endpoints (Create API Route)

Source: Internet
Author: User

For the title of this article, in fact, the literal translation is to create an API endpoint. But, it is really difficult to understand, I still write as API routing bar. Each article, the article, I will all go to carry on the practice operation, strives to write a good point tutorial.

This English address-->https://github.com/dingo/api/wiki/creating-api-endpoints

An endpoint are simply another term for a route. When talking on APIs many people refer to the routes you visit as an endpoint.

The English sentence above is an official introduction to this article. The point is that the endpoint is just a term for routing. when talking about APIs, many people refer to the routes you access as endpoints.

1.Version Groups Version Group

in order to avoid distinguishing itself from the route that comes with the main program, the package uses its own router. Therefore, we must first obtain an instance of the DINGOAPI router to create our route. As follows:

$api = App (' Dingo\api\routing\router ');
If you want a group to respond to multiple versions of the API, you can pass multiple versions of the array. As follows:
$api function ($api) {});
The version number here can be thought of as the standard routing grouping of the framework passing the array property as the second parameter. As follows:
$api function ($api) {});

You can also unify some properties for the routes in your version. As follows:

$api function ($api) {    $apifunction ($api) {        //  Endpoints registered here would have the "foo" middleware applied.     });});
2.Creating Endpoints Creating routes

Once you have a version group, you can use $API to create a route in this version group.

$api function ($api) {    $api->get (' Users/{id} ', ' App\api\controllers\[email protected] ');});

Because each version group is irrelevant, the same URL route can be made in different versions of the group, with different responses.

$api function ($api) {    $api->get (' Users/{id} ', ' App\api\v1\controllers\[email protected] '  );}); $api function ($api) {    $api->get (' Users/{id} ', ' App\api\v2\controllers\[email protected] ');});

You can also register resources and controllers in different versions using their own methods.

As a reminder, you need to add a description for the controller to the full namespace (namespace),. For example: App\http\controllers

3.Named Routes and generating URLs naming routes and generating URLs

Naming your routes can make it easy for you to generate their URLs. You can name your route in the same way as Laravel.

$api->get (' Users/{id} ', [' as ' = ' users.index ', ' uses ' = ' api\v1\[email protected] ');

Now you can generate the URL for this alias by routing the alias.

App (' Dingo\api\routing\urlgenerator ')->version (' v1 ')->route (' Users.index ');

4.Viewing Routes In the console displays routes on a specific console

If you use Laravel 5.1, you can view it by using the artisan command.

$ PHP Artisan api:routes

The command is the route:list same as the command in Laravel.

Section III: DINGO/API latest version of V2.0 Creating API endpoints (Create API Route)

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.