This article mainly introduces about using laravel dingo/api to create a simple api, which has a certain reference value. Now I share it with you. Friends in need can refer to it
1, modify the .env configuration File addition
API_STANDARDS_TREE=vnd API_SUBTYPE=myapp API_PREFIX=api API_DOMAIN=null API_VERSION=v1 API_NAME="My API" API_CONDITIONAL_REQUEST=false API_STRICT=false API_DEBUG=true
Standards Tree Standard Tree
There are three different trees: x, prs and vnd. The standard tree you use depends on the project you develop
Unregistered tree (x) mainly represents local and private environments
Private tree (prs) mainly represents projects without commercial release
Vendor tree (vnd) mainly represents publicly released projects
Subtype SUBTYPE
The subtype is usually the short name of the application or project, All are lowercase.
Prefix PREFIX(such as www.z5w.net/api/)
If you have ever used an API you will know that most services come from subdomains or prefixes . Prefix or subdomain is required, but only one is required. Please avoid using version numbers as your prefix or subdomain, as versioning is handled via the Accept header.
Subdomain name API_DOMAIN
For example, you can use api.z5w.net as the api calling address. If the prefix has been set, the domain is generally set to null
version
This version number is the default version number of your API, and will be used in some Used as the default value for callbacks when no version number is provided. This version number is also used as a default value when generating API documentation.
NameName
The name of your API will only be used when you generate documentation using the API Blueprint command. Using this name prevents you from having to manually define the name each time you generate a document.
You may need to wrap it in quotes.
Conditional request CONDITIONAL_REQUEST
"Conditional request" is turned on by default, which is conducive to the client's caching mechanism to cache API requests when possible.
Strict Mode STRICT
Strict mode requires the client to send the Accept header instead of the default version configured in the configuration file. This means you won't be able to access your API directly through your browser.
If strict mode is enabled, sending an illegal Accept header will throw an unhandled exception Symfony\Component\HttpKernel\Exception\BadRequestHttpException. You need to handle this exception yourself.
Debug ModeDebug
Common errors handled by this package include a debug key which, when enabled, will populate stack trace details.
2. Add a 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 the file /app/Api/Controllers/HelloController.php
Copy after login
4. Test the route: $ php artisan api:routes, if
+-----+----------+-----------------+------+-------------------------------------------+-----------+------------+----------+------------+ | Host | Method | URI | Name | Action | Protected | Version(s) | Scope(s) | Rate Limit | +------+----------+-----------------+------+-------------------------------------------+-----------+------------+----------+------------+ | | GET|HEAD | /api/helloworld | | App\Api\Controllers\HelloController@index | No | v1 | | | +------+----------+-----------------+------+-------------------------------------------+-----------+------------+----------+------------+
appears, it means success
Then visit http://www.*.com/api/helloworld to see if the json data of the api appears. Woolen cloth?
{content:Helloworld!}
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
laravel dingo/api add jwt-auth authentication
The above is the detailed content of Create a simple API with laravel dingo/api. For more information, please follow other related articles on the PHP Chinese website!