PHP code example of zoujingli / thinkadmin

1. Go to this page and download the library: Download zoujingli/thinkadmin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zoujingli / thinkadmin example snippets



namespace app\plugin\example;

use think\admin\Plugin;

class ExamplePlugin extends Plugin
{
    public function install()
    {
        // 安装插件时的操作
        $this->createTables();
        $this->createMenus();
    }
    
    public function uninstall()
    {
        // 卸载插件时的操作
        $this->dropTables();
        $this->removeMenus();
    }
}


namespace app\admin\controller;

use think\admin\Controller;

class UserController extends Controller
{
    /**
     * 用户列表
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '用户管理';
        $this->_query('SystemUser')
            ->like('username,nickname,phone')
            ->equal('status')
            ->dateBetween('create_at')
            ->order('id desc')
            ->page();
    }
}


namespace app\admin\service;

use think\admin\Storage;

class FileService
{
    public function upload($file, $type = 'local')
    {
        $storage = Storage::instance($type);
        
        // 文件去重检查
        $hash = md5_file($file->getPathname());
        $exists = $this->checkFileExists($hash);
        
        if ($exists) {
            return $exists['url'];
        }
        
        // 上传文件
        $result = $storage->upload($file);
        return $result['url'];
    }
}


namespace app\admin\service;

use think\admin\service\QueueService;

class TaskService
{
    // 注册邮件发送任务
    public function sendEmail($to, $subject, $content)
    {
        $name = "发送邮件到 {$to}";
        $command = "xadmin:service email {$to} {$subject}";
        
        return QueueService::register($name, $command, 0);
    }
}

// 邮件队列处理类
class EmailQueue extends QueueService
{
    public function execute(array $data = [])
    {
        $this->setQueueProgress('开始发送邮件...', 10.00);
        
        try {
            // 发送邮件逻辑
            $result = $this->sendMail($data['to'], $data['subject'], $data['content']);
            
            if ($result) {
                $this->setQueueProgress('邮件发送成功', 100.00);
                $this->setQueueSuccess('邮件发送成功');
            } else {
                $this->setQueueError('邮件发送失败');
            }
        } catch (\Exception $e) {
            $this->setQueueError('邮件发送异常: ' . $e->getMessage());
        }
    }
}


namespace app\admin\controller;

use think\admin\Controller;

class ProductController extends Controller
{
    protected $dbQuery = 'Product';
    
    public function index()
    {
        $this->title = '商品管理';
        $this->_query($this->dbQuery)
            ->like('name,description')
            ->equal('category_id,status')
            ->dateBetween('create_at')
            ->order('id desc')
            ->page();
    }
    
    public function add()
    {
        $this->title = '添加商品';
        $this->_form($this->dbQuery, 'form');
    }
    
    public function save()
    {
        $this->_save($this->dbQuery, $this->_vali([
            'status.


namespace app\plugin\example;

use think\admin\Plugin;

class ExamplePlugin extends Plugin
{
    public function install()
    {
        // 安装插件时的操作
        $this->createTables();
        $this->createMenus();
    }
    
    public function uninstall()
    {
        // 卸载插件时的操作
        $this->dropTables();
        $this->removeMenus();
    }
}

// config/database.php
return [
    'default' => 'sqlite',
    'connections' => [
        'sqlite' => [
            'type' => 'sqlite',
            'database' => 'database/sqlite.db',
        ],
    ]
];

// config/database.php
return [
    'default' => 'mysql',
    'connections' => [
        'mysql' => [
            'type' => 'mysql',
            'hostname' => '127.0.0.1',
            'database' => 'thinkadmin',
            'username' => 'root',
            'password' => 'your_password',
            'hostport' => '3306',
            'charset' => 'utf8mb4',
        ],
    ]
];


namespace app\admin\controller;

use think\admin\Controller;

class MyController extends Controller
{
    /**
     * 我的页面
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->title = '我的页面';
        $this->fetch();
    }
}

/**
 * 需要权限验证的方法
 * @auth true    # 需要权限验证
 * @menu true    # 添加到菜单
 * @login true   # 需要登录
 */
public function myMethod()
{
    // 方法内容
}

// 查询数据
$this->_query('user')
    ->like('username,email')
    ->equal('status')
    ->dateBetween('create_time')
    ->order('id desc')
    ->page();

// 表单处理
$this->_form('user', 'form');

// 状态更新
$this->_save('user', $this->_vali([
    'status.
bash
# 检查 PHP 版本
php -v

# 检查 Composer
composer -v

# 检查 PHP 扩展
php -m | grep -E "(gd|mbstring|openssl|pdo|curl|fileinfo|json|zip)"
bash
# 启动开发服务器
php think run --host 127.0.0.1 --port 8000

# 数据库迁移
php think migrate:run

# 清除缓存
php think clear

# 异步任务管理
php think xadmin:queue start    # 启动守护进程
php think xadmin:queue stop     # 停止进程
php think xadmin:queue query    # 查询任务
php think xadmin:queue listen   # 监听进程