激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - 基于laravel制作APP接口(API)

基于laravel制作APP接口(API)

2020-12-29 16:42PHP教程網 PHP教程

這篇文章主要介紹了基于laravel制作APP接口(API)的相關資料,需要的朋友可以參考下

前期準備

前言,為什么做以及要做個啥
本人姓小名白,不折不扣編程屆小白一名,但是自從大一那會兒接觸到編程這件奇妙的事情,就完完全全的陷入的程序的世界。

這不,最近又開始折騰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

基于laravel制作APP接口(API)

基于laravel制作APP接口(API)

基于laravel制作APP接口(API)

基于laravel制作APP接口(API)

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25
主站蜘蛛池模板: 一区二区三区视频播放 | 日韩午夜一区二区三区 | 久久大胆视频 | 黄色片视频在线观看 | 久久久久一区二区三区四区五区 | a黄毛片 | 天天夜天天操 | 把娇妻调教成暴露狂 | h视频在线播放 | 久久久久亚洲美女啪啪 | 97青青| av免费提供 | 精品一区二区久久久久久久网精 | 久久国产精品久久久久 | 国产精品久久久久影院老司 | 成人资源在线观看 | 男人久久天堂 | www.99热精品 | 欧美另类视频一区 | 国产一级淫片免费看 | 久久久成人免费视频 | 亚洲日韩精品欧美一区二区 | 欧美a视频| 欧美一区二区三区成人 | 久久网站热最新地址 | 国产九色视频在线观看 | 超级av在线 | 少妇色诱麻豆色哟哟 | 久久精品一区二区三 | 久久国产精品久久久久久久久久 | 日韩在线毛片 | 91麻豆蜜桃一区二区三区 | 久久色网站 | 美女久久久久久久久 | 极品美女一级毛片 | 亚洲成人福利在线 | 中文字幕国产欧美 | 最新黄色av| 成人福利视频 | 国产欧美日韩在线不卡第一页 | 国产91亚洲精品一区二区三区 |