如何基于 Notadd 構(gòu)建 API (Laravel 寫(xiě) API)

2018-05-23 11:20 更新

如何基于 Notadd 構(gòu)建 API

Notadd 底層實(shí)現(xiàn)了 passport 機(jī)制,有統(tǒng)一的授權(quán)管理,主要支持兩種方式進(jìn)行 API 授權(quán),一個(gè)是 client,領(lǐng)一個(gè)是 passport,這個(gè)在其他文檔中有做詳細(xì)的說(shuō)明。

這里主要說(shuō)的是,如何基于 Notadd 進(jìn)行 API 接口的開(kāi)發(fā)。

業(yè)務(wù)邏輯

熟悉 Laravel 的同學(xué)都應(yīng)該知道,Laravel 遵循這樣的業(yè)務(wù)邏輯實(shí)現(xiàn):

路由(route) -> 控制器(controller) -> 業(yè)務(wù)邏輯(model) -> 數(shù)據(jù)輸出(view)

而 Notadd 的 API 業(yè)務(wù)邏輯實(shí)現(xiàn)同樣遵循類(lèi)似的流程:

路由(route) -> 控制器(controller) -> API 處理器(handler) -> 模型(model) -> 數(shù)據(jù)輸出(json)

其中,主要的差異在于,API 處理器提供了對(duì)數(shù)據(jù)輸出格式的輸出,返回的數(shù)據(jù)格式統(tǒng)一為:

[
    'code' => 200,             // API 接口返回的狀態(tài)碼,默認(rèn)為 200
    'data' => [],              // API 接口返回的數(shù)據(jù),主要為數(shù)組形式
    'message' => 'success!',  // API 接口返回的提示信息,可以包含錯(cuò)誤信息或成功信息
]

路由

Notadd 在實(shí)現(xiàn) API 授權(quán)的時(shí)候,使用的是有 路由中間件(middleware) 的方式來(lái)實(shí)現(xiàn)的。

具體實(shí)現(xiàn)方式,是在路由的中間配置參數(shù)中添加 auth:api 。

例如,在實(shí)現(xiàn) api/setting/all 和 api/setting/set 兩個(gè) API 的時(shí)候,添加 auth:api 的中間件,代碼參考如下:

$this->router->group(['middleware' => ['auth:api', 'web'], 'prefix' => 'api/setting'], function () {
    $this->router->post('all', 'Notadd\Foundation\Setting\Controllers\SettingController@all');
    $this->router->post('set', 'Notadd\Foundation\Setting\Controllers\SettingController@set');
});

Notadd 針對(duì)需要跨域的 API 還提供了 cross 的路由中間件,以實(shí)現(xiàn) API 跨域的功能。

例如,為前兩個(gè) API 提供跨域的功能實(shí)現(xiàn),代碼參考如下:

$this->router->group(['middleware' => ['auth:api', 'cross', 'web'], 'prefix' => 'api/setting'], function () {
    $this->router->post('all', 'Notadd\Foundation\Setting\Controllers\SettingController@all');
    $this->router->post('set', 'Notadd\Foundation\Setting\Controllers\SettingController@set');
});

控制器

由于有了獨(dú)立的 API處理器 ,控制器層可以制作簡(jiǎn)單處理,僅需向控制器注入 handler,并由 handler 提供的輔助方法返回 API 數(shù)據(jù)給前臺(tái),即可。

例如,在前面路由調(diào)用的 SettingController 中,僅需要注入 AllHandler ,使用方法 toResponse 和 generateHttpResponse 來(lái)返回結(jié)果給前臺(tái),代碼參考如下:

<?php
/**
 * This file is part of Notadd.
 *
 * @author TwilRoad <269044570@qq.com>
 * @copyright (c) 2016, iBenchu.org
 * @datetime 2016-11-08 17:01
 */
namespace Notadd\Foundation\Setting\Controllers;


use Notadd\Foundation\Routing\Abstracts\Controller;
use Notadd\Foundation\Setting\Contracts\SettingsRepository;
use Notadd\Foundation\Setting\Handlers\AllHandler;
use Notadd\Foundation\Setting\Handlers\SetHandler;


/**
 * Class SettingController.
 */
class SettingController extends Controller
{
    /**
     * @var \Notadd\Foundation\Setting\Contracts\SettingsRepository
     */
    protected $settings;


    /**
     * SettingController constructor.
     *
     * @param \Notadd\Foundation\Setting\Contracts\SettingsRepository $settings
     *
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     */
    public function __construct(SettingsRepository $settings)
    {
        parent::__construct();
        $this->settings = $settings;
    }


    /**
     * All handler.
     *
     * @param \Notadd\Foundation\Setting\Handlers\AllHandler $handler
     *
     * @return \Notadd\Foundation\Passport\Responses\ApiResponse
     * @throws \Exception
     */
    public function all(AllHandler $handler)
    {
        return $handler->toResponse()->generateHttpResponse();
    }


    /**
     * Set handler.
     *
     * @param \Notadd\Foundation\Setting\Handlers\SetHandler $handler
     *
     * @return \Notadd\Foundation\Passport\Responses\ApiResponse
     * @throws \Exception
     */
    public function set(SetHandler $handler)
    {
        return $handler->toResponse()->generateHttpResponse();
    }
}

API Handler 和模型

在 API Handler中提供了模型的操作接口。

在 Notadd 中,提供了兩類(lèi) API Handler,一類(lèi)是 DataHandler,另一類(lèi)是 SetHandler,顧名思義,DataHandler 僅提供數(shù)據(jù)返回接口,而 SetHandler 不僅提供數(shù)據(jù)返回接口,還提供其他操作處理的接口。

具體差異體現(xiàn)在,DataHandler 在返回?cái)?shù)據(jù)接口時(shí)僅調(diào)用方法 data,而 SetHandler 在調(diào)用 data 方法前還有調(diào)用 execute 方法。

例如,在前面的 SettingController 中使用的 AllHandler 為 DataHandler 類(lèi) Handler,提供返回所有 配置項(xiàng) 的 API 功能,SetHandler 為 SetHandler 類(lèi) Handler,提供 修改配置項(xiàng) 并返回所有 配置項(xiàng) 的 API 功能。

AllHandler 的代碼如下:

<?php
/**
 * This file is part of Notadd.
 *
 * @author TwilRoad <269044570@qq.com>
 * @copyright (c) 2016, iBenchu.org
 * @datetime 2016-11-23 14:44
 */
namespace Notadd\Foundation\Setting\Handlers;


use Illuminate\Container\Container;
use Notadd\Foundation\Passport\Abstracts\DataHandler;
use Notadd\Foundation\Setting\Contracts\SettingsRepository;


/**
 * Class AllHandler.
 */
class AllHandler extends DataHandler
{
    /**
     * @var \Notadd\Foundation\Setting\Contracts\SettingsRepository
     */
    protected $settings;


    /**
     * AllHandler constructor.
     *
     * @param \Illuminate\Container\Container                         $container
     * @param \Notadd\Foundation\Setting\Contracts\SettingsRepository $settings
     */
    public function __construct(
        Container $container,
        SettingsRepository $settings
    ) {
        parent::__construct($container);
        $this->settings = $settings;
    }


    /**
     * Http code.
     *
     * @return int
     */
    public function code()                                                 // 定義 API 操作結(jié)果的狀態(tài)碼
    {
        return 200;
    }


    /**
     * Data for handler.
     *
     * @return array
     */
    public function data()                                                  // 定義 API 返回的數(shù)據(jù)
    {
        return $this->settings->all()->toArray();
    }


    /**
     * Errors for handler.
     *
     * @return array
     */
    public function errors()                                                // 定義 API 操作失敗時(shí)返回的信息
    {
        return [
            '獲取全局設(shè)置失敗!',
        ];
    }


    /**
     * Messages for handler.
     *
     * @return array
     */
    public function messages()                                             // 定義 API 操作成功時(shí)返回的信息
    {
        return [
            '獲取全局設(shè)置成功!',
        ];
    }
}

SetHandler 的代碼如下:

<?php
/**
 * This file is part of Notadd.
 *
 * @author TwilRoad <269044570@qq.com>
 * @copyright (c) 2016, iBenchu.org
 * @datetime 2016-11-23 15:09
 */
namespace Notadd\Foundation\Setting\Handlers;


use Illuminate\Container\Container;
use Notadd\Foundation\Passport\Abstracts\SetHandler as AbstractSetHandler;
use Notadd\Foundation\Setting\Contracts\SettingsRepository;


/**
 * Class SetHandler.
 */
class SetHandler extends AbstractSetHandler
{
    /**
     * @var \Notadd\Foundation\Setting\Contracts\SettingsRepository
     */
    protected $settings;


    /**
     * SetHandler constructor.
     *
     * @param \Illuminate\Container\Container                         $container
     * @param \Notadd\Foundation\Setting\Contracts\SettingsRepository $settings
     */
    public function __construct(
        Container $container,
        SettingsRepository $settings
    ) {
        parent::__construct($container);
        $this->settings = $settings;
    }


    /**
     * Data for handler.
     *
     * @return array
     */
    public function data()                                                                    // 定義 API 返回的數(shù)據(jù)
    {
        return $this->settings->all()->toArray();
    }


    /**
     * Errors for handler.
     *
     * @return array
     */
    public function errors()                                                                  // 定義 API 操作失敗時(shí)返回的信息
    {
        return [
            '修改設(shè)置失?。?,
        ];
    }


    /**
     * Execute Handler.
     *
     * @return bool
     */
    public function execute()                                                                 // 定義 API 執(zhí)行的修改操作
    {
        $this->settings->set('site.enabled', $this->request->input('enabled'));
        $this->settings->set('site.name', $this->request->input('name'));
        $this->settings->set('site.domain', $this->request->input('domain'));
        $this->settings->set('site.beian', $this->request->input('beian'));
        $this->settings->set('site.company', $this->request->input('company'));
        $this->settings->set('site.copyright', $this->request->input('copyright'));
        $this->settings->set('site.statistics', $this->request->input('statistics'));


        return true;
    }


    /**
     * Messages for handler.
     *
     * @return array
     */
    public function messages()                                                                // 定義 API 操作成功時(shí)返回的信息
    {
        return [
            '修改設(shè)置成功!',
        ];
    }
}

數(shù)據(jù)輸出

API 結(jié)果的數(shù)據(jù)輸出,已經(jīng)在 控制器(controller) 中做了處理。

至此,一個(gè)完整的 API 開(kāi)發(fā)完成。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)