前期準備
前言,為什么做以及要做個啥
本人姓小名白,不折不扣編程屆小白一名,但是自從大一那會兒接觸到編程這件奇妙的事情,就完完全全的陷入的程序的世界。
這不,最近又開始折騰app了,話說現在開發一款app真是容易,只用javascript和一點點html+css技術就可以完成。但是做app的后臺就不一樣了。開發了app,想讓讀點數據進去,那我們就要去開發個后臺了。
laravel框架,是我最喜歡的php框架了,沒有之一。去年就曾經用laravel寫了我的個人網站但粗糙程度讓我十分臉紅,好了不扯了,讓我們直接進入主題——先安裝laravel吧!
基礎環境配置
具體的步驟直接看文檔吧laravel5.2安裝
我自己的環境是win10上面安裝了wampsrver2.5,但是這里值得好好注意一下,用wampsrver2.5了話,這幾個地方要改動一下。關于這個請看我的筆記點擊預覽
工具:sublime
瀏覽器:chrome(要用到的插件postman)
關于api
api(application programming interface,應用程序編程接口)是一些預先定義的函數,目的是提供應用程序與開發人員基于某軟件或硬件得以訪問一組例程的能力,而又無需訪問源碼,或理解內部工作機制的細節。
需要注意的是:api有它的具體用途,我們應該清楚它是干啥的。訪問api的時候應該輸入什么。訪問過api過后應該得到什么。
在開始設計api時,我們應該注意這8點
這里的內容摘抄自大神的博客
后續的開發計劃就圍繞著這個進行了。(真心好棒的總結)
1.restful設計原則
2.api的命名
3.api的安全性
4.api返回數據
5.圖片的處理
6.返回的提示信息
7.在線api測試文檔
8.在app啟動時,調用一個初始化api獲取必要的信息
用laravel開發api
就在我上愁著要不要從零開始學習的時候,找到了這個插件dingo/api那么現在就來安裝吧!
首先一定是下載的沒錯
在新安裝好的laravel的composer.json加入如下內容
然后打開cmd執行
1
|
composer update |
在config/app.php中的providers里添加
1
2
3
4
|
app\providers\oauthserviceprovider:: class , dingo\api\provider\laravelserviceprovider:: class , lucadegasperi\oauth2server\storage\fluentstorageserviceprovider:: class , lucadegasperi\oauth2server\oauth2serverserviceprovider:: class , |
在aliases里添加
1
|
'authorizer' => lucadegasperi\oauth2server\facades\authorizer:: class , |
修改app/http/kernel.php文件里的內容
1
2
3
4
5
6
7
8
9
|
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 , ]; |
然后執行
1
2
|
php artisan vendor:publish php artisan migrate |
在.env文件里添加這些配置
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
修改app\config\oauth2.php文件
1
2
3
4
5
6
7
|
'grant_types' => [ 'password' => [ 'class' => 'league\oauth2\server\grant\passwordgrant' , 'access_token_ttl' => 604800, 'callback' => '\app\http\controllers\auth\passwordgrantverifier@verify' , ], ], |
新建一個服務提供者,在app/providers下新建oauthserviceprovider.php文件內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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() { // } } |
然后打開routes.php添加相關路由
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//get access_token route::post( 'oauth/access_token' , function () { return response::json(authorizer::issueaccesstoken()); }); //create a test user, you don't need this if you already have. 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())]; }); }); |
分別創建basecontroller.php和userscontroller.php內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
//basecontroller namespace app\http\controllers; use dingo\api\routing\helpers; use illuminate\routing\controller; class basecontroller extends controller { use helpers; } //userscontroller namespace 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 ); // 數組形式 return $this ->response-> array ( $user ->toarray()); } } |
隨后在app/http/controllers/auth/下創建passwordgrantverifier.php內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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; } } |
打開數據庫的oauth_client表新增一條client數據
1
|
insert into 'oauth_clients' ( 'id' , 'secret' , 'name' , 'created_at' , 'updated_at' ) values ( '1' , '2' , 'main website' , '2016–03–13 23:00:00' , '0000–00–00 00:00:00' ); |
隨后的就是去愉快的測試了,這里要測試的api有
新增一個用戶
http://localhost/register
讀取所有用戶信息
http://localhost/api/users
只返回用戶id為4的信息
http://localhost/api/users/4
獲取access_token
http://localhost/oauth/access_token
利用token值獲得時間,token值正確才能返回正確值
http://localhost/api/time
打開postman