Trong tài liệu này chúng tôi ví dụ module hiện tại là module Page đang chuẩn bị lập trình chức năng API.
Lưu ý: Tên module, tên file Api và tên Class phân biệt chữ hoa và chữ thường.
Bước 1: Tạo thư mục Api để chứa các file Api
Trong thư mục của module tạo thêm thư mục Api. Khi đó tồn tại đường dẫn modules/Page/Api
Bước 2: Tạo file trong thư mục Api
Mỗi Api sẽ nằm trong một file php nằm trong thư mục Api. Tên file là tên class (phân biệt chữ viết hoa và viết thường). Ví dụ:
<?php /** * @Project NUKEVIET 4.x * @Author VINADES.,JSC <contact@vinades.vn> * @Copyright (C) 2014 VINADES ., JSC. All rights reserved * @License GNU/GPL version 2 or any later version * @Createdate Jun 20, 2010 8:59:32 PM */ namespace NukeViet\Module\Page\Api; use NukeViet\Api\Api; use NukeViet\Api\ApiResult; use NukeViet\Api\IApi; if (!defined('NV_ADMIN') or !defined('NV_MAINFILE')) { die('Stop!!!'); } class CreatArticle implements IApi { private $result; /** * @return number */ public static function getAdminLev() { return Api::ADMIN_LEV_MOD; } /** * @return string */ public static function getCat() { return 'System'; } /** * {@inheritDoc} * @see \NukeViet\Api\IApi::setResultHander() */ public function setResultHander(ApiResult $result) { $this->result = $result; } /** * {@inheritDoc} * @see \NukeViet\Api\IApi::execute() */ public function execute() { // @TODO return $this->result->getResult(); } }
Giải thích code như sau:
namespace NukeViet\Module\Page\Api;
Quy luật:
NukeViet\Module\<ModuleName>\Api
<ModuleName>
chính là tên module, trong ví dụ này là Page
.
Sau khi đặt namespace, ta khai báo sử dụng các class NukeViet Core hỗ trợ:
use NukeViet\Api\Api; use NukeViet\Api\ApiResult; use NukeViet\Api\IApi;
class CreatArticle implements IApi
Class cần phải implements lớp IApi, theo đó phải có đủ 3 phương thức
/** * Lấy được quyền hạn sử dụng của admin * Admin tối cao, điều hành chung hay quản lý module được sử dụng */ public static function getAdminLev(); /** * Danh mục, cũng là khóa ngôn ngữ của API * Nếu không có danh mục thì trả về chuỗi rỗng */ public static function getCat(); /** * Thiết lập trình xử lý kết quả * * @param ApiResult $result */ public function setResultHander(ApiResult $result); /** * Thực thi API */ public function execute();
Lưu ý: Class name chính là tên file. Ví dụ class name là CreatArticle thì file sẽ làmodule/Page/Api/CreatArticle.php
Cho biết đối tượng được quyền sử dụng API:
public static function getAdminLev() { return Api::ADMIN_LEV_MOD; }
Cần trả về một trong 3 giá trị:
Api::ADMIN_LEV_GOD; // 3: Quản lý module Api::ADMIN_LEV_SP; // 2: Điều hành chung Api::ADMIN_LEV_MOD; // 1: Admin tối cao
Cho biết danh mục của API nếu có, nếu API không được xếp danh mục hãy trả về chuỗi rỗng.
public static function getCat() { return 'System'; }
public function setResultHander(ApiResult $result) { $this->result = $result; }
Giữ nguyên code như mẫu, không thay đổi thêm.
public function execute() { // Viết code thực thi tại đây return $this->result->getResult(); }
Code thực thi được viết tự do, và bắt buộc phải trả về thông qua phương thức getResult
của class NukeViet\Api\ApiResult
$module_name
sử dụng lệnh $module_name = Api::getModuleName();
$module_info
sử dụng lệnh $module_name = Api::getModuleInfo();
. Các biến $module_data, $module_file, $module_upload
xác định từ $module_info
.$admin_id = Api::getAdminName();
$admin_username = Api::getAdminId();
$admin_level = Api::getAdminLev();
$nv_Lang, $nv_Request, $db, $nv_Cache, …
có thể gọi global như thông thường.Message thông báo:
$this->result->setMessage($nv_Lang->getModule('empty_bodytext'));
Mã lỗi (nếu có):
$this->result->setCode($code);
Đánh dấu thành công:
$this->result->setSuccess();
Đánh dấu lỗi:
$this->result->setError();
Các dữ liệu khác:
$this->result->set($key, $value);