Create a simple API with Laravel Dingo/api

Source: Internet
Author: User
Tags subdomain
This article mainly introduces the use of Laravel Dingo/api to create a simple API, has a certain reference value, now share to everyone, the need for friends can refer to

1, modify the. env configuration file to add

API_STANDARDS_TREE=VND api_subtype=myappapi_prefix=apiapi_domain=nullapi_version=v1api_name= "My API" API_ Conditional_request=falseapi_strict=falseapi_debug=true

Standards Tree Standard Trees

There are three different trees: X,prs and VND. The standard tree you use needs to depend on the project you develop

Unregistered trees (x) primarily represent local and private environments

Private tree (PRS) mainly indicates that there are no commercially released projects

The Vendor tree (VND) primarily represents publicly released projects

Sub-type subtype

A subtype is usually the short name of an application or project and is lowercase.

prefix prefix (e.g. www.z5w.net/api/)

If you've ever used an API, you'll know that most services come from subdomains or prefixes. A prefix or subdomain is required, but only one is required. Avoid using the version number as your prefix or subdomain, because version control is handled through header Accept.

Sub-domain Api_domain

For example, you can use Api.z5w.net to make API call address. If the prefix prefix is already set, domain is generally set to null

Version number versions

This version number is the default version number for your API and will be used as the default for callbacks if no version number is provided. This version number is also used as the default value when building the API documentation.

Names name

The name of your API will only be used when you use the API Blueprint command to generate a document. Use this name to avoid having to manually define a name every time you generate a document.
You may need to wrap it in quotation marks.

Conditional Request Conditional_request

Conditional requests are turned on by default, which facilitates the caching mechanism of the client to cache API requests where possible.

Strict Mode Strict

Strict mode requires the client to send an Accept header instead of the default version configured in the configuration file. This means that you will not be able to access your API directly from your browser.

If strict mode is turned on, sending an illegal acceept header throws an unhandled exception symfony\component\httpkernel\exception\badrequesthttpexception, and you need to handle the exception yourself.

Debug Mode Debug

The generic error that the package handles includes a debug key that populates the stack trace details when this key is enabled.

2, add route in/routes/web.php

$api = App (' Dingo\api\routing\router '); $api->version (' v1 ', function ($API) {    $api->get (' HelloWorld ', ' app\ Api\controllers\hellocontroller@index ');});

3. Create File/app/api/controllers/hellocontroller.php

<?phpnamespace App\api\controllers;use Illuminate\http\request;use App\http\controllers\controller;class Hellocontroller extends controller{public    function Index ()    {        return ' {content:helloworld!} ';    }}

4, Test route: $ PHP Artisan api:routes, if present

+-----+----------+-----------------+------+-------------------------------------------+-----------+------------ +----------+------------+| Host | Method   | URI             | Name | Action                                    | Protected | Version (s) | Scope (s) | Rate Limit |+------+----------+-----------------+------+-------------------------------------------+-----------+ ------------+----------+------------+|      | Get| HEAD | /api/helloworld |      | App\api\controllers\hellocontroller@index | No        | v1         |          |            | +------+----------+-----------------+------+-------------------------------------------+-----------+----------- -+----------+------------+

Indicates success

Then visit Http://www.*.com/api/helloworld to see if there is a JSON data for the API?

{content:helloworld!}

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.