和上一章節差不多主要多了一個后臺設置以及插入數據
這次我們在addons添加一個dajishi文件如下
然后前往官網[http://www.115cms.com/index/home/set.html](http://www.115cms.com/index/home/set.html)
基本設置創建模板插件如圖

創建好你目錄命名的插件footer標識方可進行下一步?
如果不申請的話你是無法在后臺安裝的
controller 是控制器
\-----Dajishi.php 控制器首字母必須是大寫開頭
view? 視圖
\-----home.html 鉤子輸出的視圖
\-----index-index.html 控制器輸出的模板文件
config.php 基本設置
dajishi.php 配置文件
ico.png 插件的圖標
install.sql 安裝的sql語句
uninstall.sql 卸載的sql語句
addons/dajishi/dajishi.php 如下代碼
~~~
<?php
namespace addons\dajishi;
use app\api\controller\Addon;
use Think\Db;
use Think\Config;
/*
@夢雨
50361804@qq.$com
www.115cms.com
*/
class dajishi extends Addon {
//插件的配置
public $info = array(
'name' => 'dajishi', //標識名稱與文件同名
'title' => '大記事', //插件標題
'description' => '用于顯示網站的記錄大記事', //插件描述
'status' => 1, //狀態
'pic' => '/addons/dajishi/ico.png', //插件的圖標
'author' => '夢雨', //插件的作者
'version' => '1.0'
//插件版本
);
//有后臺設置顯示數據表方法頁面
public $admin_list = array(
//條件 例如只顯示open為1的數據
//'where' => array('open' => 1),
'where' => array() ,
//顯示的表
'model' => 'dajishi',
//排序方式
'order' => 'id desc',
//搜索的字段 多個字段用|隔開例如 title|uid
'ks' => 'content'
);
public $custom_adminlist = 'admin.html';
//萬年不變安裝方法
public function install() {
//$this->info['description']是插件的描述
//$this->info['name']是插件的標題
//dajishi是鉤子名稱
$this->getisHook('dajishi', $this->info['name'], $this->info['description']);
$db_config = array();
$db_config['prefix'] = Config::get('database.prefix');
$dirname = dirname(__FILE__);
$sqldata = file_get_contents($dirname . '/install.sql');
$sql_array = preg_split("/;[\r\n]+/", str_replace('my_', $db_config['prefix'], $sqldata));
foreach ($sql_array as $k => $v) {
if (!empty($v)) {
Db::query($v);
}
}
return true;
}
//萬年不變卸載方法
public function uninstall() {
$db_config = array();
$db_config['prefix'] = Config::get('database.prefix');
$dirname = dirname(__FILE__);
$sqldata = file_get_contents($dirname . '/uninstall.sql');
$sql_array = preg_split("/;[\r\n]+/", str_replace('my_', $db_config['prefix'], $sqldata));
foreach ($sql_array as $k => $v) {
if (!empty($v)) {
Db::query($v);
}
}
return true;
}
//插件添加鉤子dajishi的方法
public function dajishi() {
$c = $this->getConfig('dajishi');
$this->assign('c', $c);
//輸出方法到/addons/dajishi/home.html頁面顯示
if ($c['open'] == 1) {
echo $this->tplfetch('home');
}
}
}
~~~
然后addons/dajishi/controller/Dajishi.php 如下代碼
~~~
<?php
namespace addons\dajishi\controller;
use app\index\controller\Addons;
use think\Db;
class Dajishi extends Addons {
//添加頁面方法
public function index() {
$c = $this->getConfig('dajishi');
$this->assign('c', $c);
if ($c['open'] == 0) {
$this->error('頁面不存在', '/');
}
//輸出在view目錄下的模板顯示
echo $this->fetch('addons/dajishi/view/index-index.html');
}
//后臺添加大記事
public function add() {
//后臺操作請加上不然未登錄用戶都會能訪問編輯
if (!session('admin_name')) {
$this->error('請登錄', url('admin/login/index'));
}
if (request()->isPost()) {
$data['time'] = strtotime(input('time'));
//xss是過濾方法
$data['content'] = xss(input('content'));
$data['open'] = input('open');
if (!Db::name('dajishi')->data($data)->insert()) {
$this->error('添加失敗');
} else {
$this->success('添加成功');
}
}
//輸出在view目錄下的模板顯示
echo $this->fetch('addons/dajishi/view/index-add.html');
}
}
~~~
config.php設置文件
~~~
<?php
return array(
//當config.php不存在的時候當前插件就沒有設置的方法
'open' => array(
'title' => '是否開啟',
'type' => 'radio',
'options' => array(
'1' => '啟用',
'0' => '關閉',
) ,
'value' => '1',
'labelwidth' => '150px',
) ,
'title' => array(
'title' => '頁面標題',
'type' => 'text',
'value' => '115CMS大記事',
'labelwidth' => '150px',
'width' => '400px',
) ,
'keywords' => array(
'title' => 'SEO關鍵字',
'type' => 'textarea',
'rows' => '10',
'cols' => '54',
'value' => '115cms,大記事',
'labelwidth' => '150px',
) ,
'description' => array(
'title' => 'SEO描述',
'type' => 'textarea',
'rows' => '10',
'cols' => '54',
'value' => '115CMS大記事',
'labelwidth' => '150px',
) ,
);
~~~