Based on Laravel authoring app interface (API), Laravelapp interface API
Pre-preparation
Preface, why do and what to do
My surname is white, to the letter of the programming session of small white one, but since freshman year when the contact to programming this wonderful thing, is completely into the program of the world.
This is not, recently began to toss the app, say now the development of an app is really easy, only with JavaScript and a little bit of html+css technology can be done. But the backstage of the app is different. Developed the app, want to let the reading data in, then we will go to develop a backstage.
Laravel framework, is my favorite PHP framework, no one. Last year, I used Laravel to write my personal website but the roughness let me very blush, OK not to pull, let us directly into the theme-first install Laravel it!
Basic Environment Configuration
Specific steps to see the document directly laravel5.2 installation
My own environment is WIN10 installed on the wampsrver2.5, but here is worth paying attention to, with wampsrver2.5 words, these several places to change a bit. For this please see my notes click to preview
Tool: Sublime
Browser: Chrome (plugin postman to use)
About API
APIs (application Programming Interface, application programming interfaces) are pre-defined functions designed to provide the ability of applications and developers to access a set of routines based on a piece of software or hardware without having to access the source code. Or understand the details of the internal working mechanism.
It is important to note that the API has its specific purpose, and we should be aware of what it is doing. What you should enter when accessing the API. What you should get after you've accessed the API.
We should pay attention to these 8 points when we start to design the API
Here's a excerpt from the Arrogant God blog.
The follow-up development plan revolves around this. (Really great summary)
1.Restful Design Principles
The naming of 2.API
3.API of security
4.API return Data
5. Processing of pictures
6. Returned message
7. Online API Test Documentation
8. When the app starts, call an initialization API to get the necessary information
Developing APIs with Laravel
Just in my sorrow to start learning from scratch, found this plugin dingo/api so now to install it!
First of all, it must be downloaded, yes.
Add the following to the newly installed Laravel Composer.json
Then open cmd execution
Composer Update
Added in providers in config/app.php
App\providers\oauthserviceprovider::class,dingo\api\provider\laravelserviceprovider::class,lucadegasperi\ Oauth2server\storage\fluentstorageserviceprovider::class,lucadegasperi\oauth2server\ Oauth2serverserviceprovider::class,
Add in aliases
' Authorizer ' = Lucadegasperi\oauth2server\facades\authorizer::class,
Modify content in the app/http/kernel.php file
protected $middleware = [\lucadegasperi\oauth2server\middleware\oauthexceptionhandlermiddleware::class,]; protected $routeMiddleware = [ ' OAuth ' = \lucadegasperi\oauth2server\middleware\oauthmiddleware::class, ' oauth-user ' = \lucadegasperi\oauth2server\middleware\oauthuserownermiddleware::class, ' Oauth-client ' = \lucadegasperi\oauth2server\middleware\oauthclientownermiddleware::class, ' Check-authorization-params ' = \lucadegasperi\oauth2server\middleware\checkauthcoderequestmiddleware::class, ' csrf ' = \app\http\middleware\verifycsrftoken::class,];
And then execute
PHP artisan vendor:publish php Artisan Migrate
Add these configurations to the. env file
Api_standards_tree=x
Api_subtype=rest
Api_name=rest
Api_prefix=api
Api_version=v1
Api_conditional_request=true
Api_strict=false
Api_debug=true
Api_default_format=json
Modify the app\config\oauth2.php file
' Grant_types ' = [ ' password ' and ' = ' class ' = ' league\oauth2\server\grant\passwordgrant ', ' Access_token_ttl ' = ' and 604800, ' callback ' and ' \app\http\controllers\auth\passwordgrantverifier@verify ', ],],
Create a new service provider under App/providers and create a new oauthserviceprovider.php file with the following contents
namespace App\providers;use dingo\api\auth\auth;use Dingo\api\auth\provider\oauth2;use illuminate\support\ Serviceprovider;class Oauthserviceprovider extends serviceprovider{public function boot () { $this App[auth::class]->extend (' OAuth ', function ($app) { $provider = new OAuth2 ($app [' Oauth2-server.authorizer ']- >getchecker ()); $provider->setuserresolver (function ($id) { //Logic to return a user by their ID. }); $provider->setclientresolver (function ($id) { //Logic to return a client by their ID. }); return $provider; }); } Public Function Register () { // }}
Then open routes.php Add related route
Get Access_tokenroute::p ost (' Oauth/access_token ', function () { return Response::json (authorizer:: Issueaccesstoken ());}); /create A test user, you don ' t need the If you already has. Route::get ('/register ', function () { $user = new App\user (); $user->name= "Tester"; $user->email= "test@test.com"; $user->password = \illuminate\support\facades\hash::make ("password"); $user->save ();}); $api = App (' Dingo\api\routing\router ');//show user info via restful service. $api->version (' v1 ', [' namespace ' = ') App\http\controllers '], function ($API) { $api->get (' users ', ' Userscontroller@index '); $api->get (' Users/{id} ', ' userscontroller@show '); /just a test with auth check. $api->version (' v1 ', [' middleware ' = ' api.auth '], function ($API) { $api->get (' Time ', function () { return [' Now ' = Microtime (), ' date ' = = Date (' y-m-d ', Time ())];});
Create the basecontroller.php and userscontroller.php content separately as follows
Basecontrollernamespace app\http\controllers;use Dingo\api\routing\helpers;use Illuminate\Routing\Controller; Class Basecontroller extends controller{use Helpers;} Userscontrollernamespace App\http\controllers;use App\user;use App\http\controllers\controller;class Userscontroller extends basecontroller{public function Index () { return user::all (); } Public function Show ($id) { $user = User::findorfail ($id); The array form return $this->response->array ($user->toarray ());} }
Then create the passwordgrantverifier.php content under app/http/controllers/auth/below
Namespace App\http\controllers\auth;use illuminate\support\facades\auth;class passwordgrantverifier{public function Verify ($username, $password) { $credentials = [ ' email ' + = $username, ' Password ' = $password, ]; if (Auth::once ($credentials)) { return Auth::user ()->id; } return false; }}
Open a Database oauth_client table with a new client data
INSERT into ' oauth_clients ' (' id ', ' secret ', ' name ', ' created_at ', ' Updated_at ') VALUES (' 1 ', ' 2 ', ' Main website ', ' 2016–0 3–13 23:00:00 ', ' 0000–00–00 00:00:00 ');
The next is to go to a happy test, where the API to be tested has
Add a user
Http://localhost/register
Read all user information
Http://localhost/api/users
Returns only information with user ID 4
Http://localhost/api/users/4
Get Access_token
Http://localhost/oauth/access_token
Use the token value to get the time, the token value is correct to return the correct value
Http://localhost/api/time
Open Postman
Articles you may be interested in:
- A detailed example of trait usage in Laravel
- Steps to register facades in Laravel
- Laravel method of implementing automatic dependency injection of constructors
- Laravel ways to reduce database query pressure using caching cache data
- PHP Framework Laravel Learning Experience
- Get previous and next data in Laravel
- Nginx Run PHP Framework laravel configuration file sharing
- Yii method for executing SQL statements using the Migrate command
- Yii Framework framework uses YIIC to quickly create migrate usage examples of YII applications
- Laravel Execute migrate command prompt: No such solution for file or directory
http://www.bkjia.com/PHPjc/1110074.html www.bkjia.com true http://www.bkjia.com/PHPjc/1110074.html techarticle based on the Laravel Production app interface (API), Laravelapp interface API pre-preparation preface, why do and want to do what I surname White, to the letter programming session small white one, but since ...