这篇文章主要介绍“如何用FastAdmin插件添加API接口”,在日常操作中,相信很多人在如何用FastAdmin插件添加API接口问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何用FastAdmin插件添加API接口”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
\addons\guestbook\controller
目录下建立api
目录(若插件只有api控制器,可以不建立,api目录名称自定义,此处以api
为目录名举例。)。
api
目录内,建立Base.php
基类文件:
// api基类 /addons/guestbook/controller/api/Base.php 文件
<?php
namespace addons\guestbook\controller\api;
use app\common\controller\Api;
use app\common\library\Auth;
use think\Lang;
/**
* api基类
*/
class Base extends Api
{
protected $noNeedLogin = [];// 无需登录即可访问的方法,同时也无需鉴权了
protected $noNeedRight = ['*'];// 无需鉴权即可访问的方法
public function _initialize()
{
parent::_initialize();
// 载入语言包、初始化Auth等
//这里手动载入语言包
Lang::load(ROOT_PATH . '/addons/guestbook/lang/zh-cn.php');
}
}
api
目录建立接口类,此处以留言板
的接口为例
// 留言接口类,可供小程序、app等使用 /addons/guestbook/controller/api/Guestbook.php 文件
<?php
namespace addons\guestbook\controller\api;
use think\Validate;
class Guestbook extends Base
{
protected $noNeedLogin = ['msglog'];
/*
* 记录留言接口
* 本接口URL:http://您的域名/addons/guestbook/api.guestbook/index
*/
public function msglog()
{
if ($this->request->isPost()) {
$contact = $this->request->post('contact');
$title = $this->request->post('title');
$message = $this->request->post('message');
$token = $this->request->post('__token__');
$rule = [
'contact' => 'require|length:3,30',
'title' => 'require|length:3,30',
'message' => 'require|length:3,255',
'__token__' => 'require|token',
];
$msg = [
'contact.require' => __('Contact information required'),
'contact.length' => __('Contact must be within 3 to 30 characters'),
'title.require' => __('Message subject required'),
'title.length' => __('Message subject must be within 3 to 30 characters'),
'message.require' => __('Message content required'),
'message.length' => __('Message content must be within 3 to 255 characters'),
];
$data = [
'contact' => $contact,
'title' => $title,
'message' => $message,
'__token__' => $token,
];
$validate = new Validate($rule, $msg);
$result = $validate->check($data);
if (!$result) {
$this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
}
// 留言入库
$data['user_id'] = $this->auth->isLogin() ? $this->auth->id : 0;
$Msglog_model = new \app\admin\model\guestbook\Msglog;
if ($Msglog_model->allowField(true)->save($data)){
$this->success(__('Message successfully'));
} else {
$this->error(__('Message failed'));
}
}
}
}
接口URL:http://您的域名/addons/guestbook/api.guestbook/index
利用/application
文件夹中的所有文件
会在插件安装时覆盖到根目录的/application
文件夹的原理,直接将我们的api控制器
文件覆盖到FastAdmin的api
模块。
\addons\guestbook\application
目录下建立api
目录,api
目录下再建立controller
目录。
新建的controller
目录内,建立api控制器类即可。
接口URL:http://您的域名/api/控制器/方法
此方式控制器文件
强烈建议以插件标识为文件前缀,以免文件冲突。
到此,关于“如何用FastAdmin插件添加API接口”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4039648/blog/3121564