欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于laravel制作APP接口(API)

 更新時(shí)間:2016年03月15日 10:03:52   投稿:hebedich  
這篇文章主要介紹了基于laravel制作APP接口(API)的相關(guān)資料,需要的朋友可以參考下

前期準(zhǔn)備

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

這不,最近又開(kāi)始折騰APP了,話(huà)說(shuō)現(xiàn)在開(kāi)發(fā)一款A(yù)PP真是容易,只用JavaScript和一點(diǎn)點(diǎn)HTML+css技術(shù)就可以完成。但是做APP的后臺(tái)就不一樣了。開(kāi)發(fā)了APP,想讓讀點(diǎn)數(shù)據(jù)進(jìn)去,那我們就要去開(kāi)發(fā)個(gè)后臺(tái)了。

laravel框架,是我最喜歡的PHP框架了,沒(méi)有之一。去年就曾經(jīng)用laravel寫(xiě)了我的個(gè)人網(wǎng)站但粗糙程度讓我十分臉紅,好了不扯了,讓我們直接進(jìn)入主題——先安裝laravel吧!

基礎(chǔ)環(huán)境配置

具體的步驟直接看文檔吧laravel5.2安裝

我自己的環(huán)境是win10上面安裝了wampsrver2.5,但是這里值得好好注意一下,用wampsrver2.5了話(huà),這幾個(gè)地方要改動(dòng)一下。關(guān)于這個(gè)請(qǐng)看我的筆記點(diǎn)擊預(yù)覽
工具:sublime
瀏覽器:chrome(要用到的插件postman)

關(guān)于API

API(Application Programming Interface,應(yīng)用程序編程接口)是一些預(yù)先定義的函數(shù),目的是提供應(yīng)用程序與開(kāi)發(fā)人員基于某軟件或硬件得以訪(fǎng)問(wèn)一組例程的能力,而又無(wú)需訪(fǎng)問(wèn)源碼,或理解內(nèi)部工作機(jī)制的細(xì)節(jié)。
需要注意的是:API有它的具體用途,我們應(yīng)該清楚它是干啥的。訪(fǎng)問(wèn)API的時(shí)候應(yīng)該輸入什么。訪(fǎng)問(wèn)過(guò)API過(guò)后應(yīng)該得到什么。

在開(kāi)始設(shè)計(jì)API時(shí),我們應(yīng)該注意這8點(diǎn)
這里的內(nèi)容摘抄自大神的博客
后續(xù)的開(kāi)發(fā)計(jì)劃就圍繞著這個(gè)進(jìn)行了。(真心好棒的總結(jié))

1.Restful設(shè)計(jì)原則
2.API的命名
3.API的安全性
4.API返回?cái)?shù)據(jù)
5.圖片的處理
6.返回的提示信息
7.在線(xiàn)API測(cè)試文檔
8.在app啟動(dòng)時(shí),調(diào)用一個(gè)初始化API獲取必要的信息

用laravel開(kāi)發(fā)API

就在我上愁著要不要從零開(kāi)始學(xué)習(xí)的時(shí)候,找到了這個(gè)插件dingo/api那么現(xiàn)在就來(lái)安裝吧!
首先一定是下載的沒(méi)錯(cuò)
在新安裝好的laravel的composer.json加入如下內(nèi)容

然后打開(kāi)cmd執(zhí)行

composer update

在config/app.php中的providers里添加

App\Providers\OAuthServiceProvider::class,
Dingo\Api\Provider\LaravelServiceProvider::class,
LucaDegasperi\OAuth2Server\Storage\FluentStorageServiceProvider::class,
LucaDegasperi\OAuth2Server\OAuth2ServerServiceProvider::class,


在aliases里添加

'Authorizer' => LucaDegasperi\OAuth2Server\Facades\Authorizer::class,


修改app/Http/Kernel.php文件里的內(nèi)容

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,
];


然后執(zhí)行

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文件

'grant_types' => [
  'password' => [
    'class' => 'League\OAuth2\Server\Grant\PasswordGrant',
    'access_token_ttl' => 604800,
    'callback' => '\App\Http\Controllers\Auth\PasswordGrantVerifier@verify',
  ],
],


新建一個(gè)服務(wù)提供者,在app/Providers下新建OAuthServiceProvider.php文件內(nèi)容如下

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()
  {
    //
  }
}


然后打開(kāi)routes.php添加相關(guān)路由

//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())];
  });
});

分別創(chuàng)建BaseController.php和UsersController.php內(nèi)容如下

//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);
    // 數(shù)組形式
    return $this->response->array($user->toArray());
  }
}


隨后在app/Http/Controllers/Auth/下創(chuàng)建PasswordGrantVerifier.php內(nèi)容如下

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;
  }
}


打開(kāi)數(shù)據(jù)庫(kù)的oauth_client表新增一條client數(shù)據(jù)

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');

隨后的就是去愉快的測(cè)試了,這里要測(cè)試的API有

新增一個(gè)用戶(hù)

http://localhost/register

讀取所有用戶(hù)信息

http://localhost/api/users

只返回用戶(hù)id為4的信息

http://localhost/api/users/4

獲取access_token

http://localhost/oauth/access_token

利用token值獲得時(shí)間,token值正確才能返回正確值

http://localhost/api/time

打開(kāi)PostMan

  • php單態(tài)設(shè)計(jì)模式(單例模式)實(shí)例

    php單態(tài)設(shè)計(jì)模式(單例模式)實(shí)例

    這篇文章主要介紹了php單態(tài)設(shè)計(jì)模式(單例模式)實(shí)例,單態(tài)模式的主要作用是保證在面向?qū)ο缶幊淘O(shè)計(jì)中,一個(gè)類(lèi)只能有一個(gè)實(shí)例對(duì)象存在,需要的朋友可以參考下
    2014-11-11
  • PHP編程實(shí)現(xiàn)陽(yáng)歷轉(zhuǎn)換為陰歷的方法實(shí)例

    PHP編程實(shí)現(xiàn)陽(yáng)歷轉(zhuǎn)換為陰歷的方法實(shí)例

    這篇文章主要介紹了PHP編程實(shí)現(xiàn)陽(yáng)歷轉(zhuǎn)換為陰歷的方法,結(jié)合具體實(shí)例形式分析了php陰歷操作類(lèi)的定義與使用技巧,需要的朋友可以參考下
    2017-08-08
  • PHP Warning: Module ''modulename'' already loaded in問(wèn)題解決辦法

    PHP Warning: Module ''modulename'' already loaded in問(wèn)題解決辦法

    這篇文章主要介紹了PHP Warning: Module 'modulename' already loaded in問(wèn)題解決辦法,本文總結(jié)了兩種情況,需要的朋友可以參考下
    2015-03-03
  • php實(shí)現(xiàn)JWT驗(yàn)證的實(shí)例教程

    php實(shí)現(xiàn)JWT驗(yàn)證的實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于php實(shí)現(xiàn)JWT驗(yàn)證的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • PHP跨時(shí)區(qū)(UTC時(shí)間)應(yīng)用解決方案

    PHP跨時(shí)區(qū)(UTC時(shí)間)應(yīng)用解決方案

    解決PHP跨時(shí)區(qū)應(yīng)用需要將將程序內(nèi)部時(shí)區(qū)設(shè)置為UTC時(shí)間.(UTC 也可以叫 GMT) 數(shù)據(jù)庫(kù)中存儲(chǔ)UTC時(shí)間等等,感興趣的朋友可以了解下
    2013-01-01
  • php 魔術(shù)方法使用說(shuō)明

    php 魔術(shù)方法使用說(shuō)明

    一些在PHP叫魔術(shù)方法的函數(shù),在這里介紹一下:其實(shí)在一般的應(yīng)用中,我們都需要用到他們?。?
    2009-10-10
  • php自動(dòng)獲取字符串編碼函數(shù)mb_detect_encoding

    php自動(dòng)獲取字符串編碼函數(shù)mb_detect_encoding

    使用 mb_detect_encoding() 函數(shù)來(lái)判斷字符串是什么編碼的。
    2011-05-05
  • PHP錯(cuò)誤和異常處理功能模塊示例

    PHP錯(cuò)誤和異常處理功能模塊示例

    這篇文章主要介紹了PHP錯(cuò)誤和異常處理功能模塊,較為詳細(xì)的分析了php常見(jiàn)的錯(cuò)誤級(jí)別與錯(cuò)誤處理相關(guān)操作技巧,需要的朋友可以參考下
    2016-11-11
  • PHP持久連接mysql_pconnect()函數(shù)使用介紹

    PHP持久連接mysql_pconnect()函數(shù)使用介紹

    mysql_pconnect()函數(shù)可以大大的提高M(jìn)YSQL效率,不過(guò),此連接不自動(dòng)關(guān)閉,也會(huì)造成一些問(wèn)題,請(qǐng)注意將不用的連接即時(shí)關(guān)閉,以避免不必要的錯(cuò)誤發(fā)生
    2012-02-02
  • 最新評(píng)論