# PhpGUI桌面应用开发 **Repository Path**: return-com/php-web-view-basic-template ## Basic Information - **Project Name**: PhpGUI桌面应用开发 - **Description**: php基于webview开发win桌面,js和php直接交互 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2024-06-28 - **Last Updated**: 2024-10-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README PHP 桌面应用开发 =============== [![PHP Version](https://img.shields.io/badge/php-%3E%3D8.1-8892BF.svg)](http://www.php.net/) [![PHP FFI-EXT](https://img.shields.io/badge/FFI-*-8A2BE2.svg)](https://www.php.net/manual/zh/class.ffi) [![License](https://poser.pugx.org/topthink/framework/license)](https://packagist.org/packages/kingbes/framework) ## 要求 * PHP `8.1+` * FFI `*` * 系统 `windows` # 单独使用 ```shell composer create-project kingbes/framework desktop ``` # 获取依赖 ```shell cd src composer install ``` ## 启动应用 双击`windows.bat`文件 仅 `windows` 系统 自带环境 ```shell .\php\php.exe src/index.php ``` 自己系统环境 ```shell php src/index.php ``` # 命名规范 遵循PSR-2命名规范和PSR-4自动加载规范。 # 编译 仅 `windows` 系统 ```shell .\php\php.exe build.php -exclude="\view" ``` ## 更多编译指令 `-debug` `-name=xxx` `-exclude="\xxx"` ```shell .\php\php.exe build.php -debug # 编译为debug .\php\php.exe build.php -name=myapp # 文件名为 myapp .\php\php.exe build.php -exclude="\view" # 忽略文件夹view 批量忽略:-exclude="\view,\asd\123.txt,..." ``` # 开发文档 ## 基础 ### 根目录结构 ``` ├─php php环境 │ ├─src 应用目录 ├─build.php 编译执行文件 ├─compiler.exe win编译文件 ├─favicon.ico 图标文件 ├─README.md README 文件 └─windows.bat windows执行文件 ``` ### `src`目录结构 ``` ├─app 应用目录 │ ├─config 配置目录 │ │ ├─app.php 应用配置 │ │ ├─database.php 数据库配置 │ │ ├─menu.php 图标菜单配置 │ │ └─middleware.php 中间件配置 │ │ │ ├─controller 控制器目录 │ ├─kingbes 源码目录 │ ├─middleware 中间件目录 │ ├─ ... 更多类库目录 │ │ │ ├─public.php 公共函数文件 │ └─sqlite3.db sqlite文件 │ ├─vendor Composer类库目录 ├─view 视图目录 ├─composer.json composer 定义文件 ├─index.php 入口文件 └─README.md README 文件 ``` ### 配置 #### app.php 应用配置 ```php return [ // 窗口 "windows" => [ // 标题 "title" => "PHP GUI", // 宽 "width" => 640, // 高 "height" => 480, // 是否测试模式 ,非测试模式:false "debug" => true ], // 默认控制器 "default_controller" => "Home", // 默认时区 "default_timezone" => "Asia/Shanghai", // 视图 "default_view" => [ // 目录名 "dirname" => "view", // 文件后缀 "suffix" => "html" ] ]; ``` #### database.php 采用独立的`ThinkORM`库 数据库配置 ```php return [ 'default' => 'sqlite', 'connections' => [ 'sqlite' => [ // 数据库类型 'type' => 'sqlite', // 数据库名 'database' => app_path("sqlite3.db"), // 数据库编码默认采用utf8 'charset' => 'utf8', // 数据库表前缀 'prefix' => '', ], ], ]; ``` #### menu.php 小图标菜单配置 ```php return [ [ "name" => "显示", // 名称 "fn" => function () { // 点击后触发的函数 app()->show_win(); // 触发显示窗口函数 } ], [ "name" => "退出", "fn" => function () { app()->destroy_win(); // 关闭应用 } ] ... //追加更多 ]; ``` 效果 ![](https://gitee.com/kllxs_admin/php-webview-frame/raw/master/src/view/images/002.png) #### middleware.php 中间件配置 ```php return [ // 例子 app\middleware\TestMiddleware::class ]; ``` ## 控制器和视图 一个控制器文件对应一个视图文件,控制器 `Home.php` 对应视图 `Home.html`,文件名相同。 ### 公共函数 `Home.php` 控制器,每个公共函数绑定一个js函数,可直接在js中触发 ```php namespace app\controller; class Home { /** * get function * * @param integer $seq 触发次数 * @param array $req js传来的参数 * @return array */ public function get(int $seq, array $req): array { /** * 你的逻辑代码 */ ... return ["PHP WINDOWS GUI"]; } } ``` `Home.html` 视图触发 ```html Document ``` ### 公共参数 `$width`、`$height`、`$hint` 会改变当前窗口大小 ```php namespace app\controller; // 窗口提示类型 use app\kingbes\PhpWebview\WindowSizeHint; class SizeWin { /** * 改变当前窗口的宽度 variable * * @var integer */ public int $width = 800; /** * 改变当前窗口的高度 variable * * @var integer */ public int $height = 800; /** * 改变当前窗口的提示 variable * * @var WindowSizeHint */ public WindowSizeHint $hint = WindowSizeHint::HINT_FIXED; } ``` ### 页面跳转 常规的页面跳转是无法绑定到控制器里面的函数的,在js中调用函数`app_jump("要跳转的控制器文件名即可")` `` 成功跳转到About页面 和绑定好的控制器函数 `跳转about` 跳转了,但没有绑定对于的控制器函数 ## 中间件 ```php class TestMiddleware { public function process(string $page, object $next_class): bool { var_dump("下一页的页面"); var_dump($page); var_dump("下一页的class"); var_dump($next_class); // true 会放行 , false不放行 return true; } } ``` ## 数据库 thinkphp的orm库 [点击进入](https://github.com/top-think/think-orm) ## 自带的公共函数 ```php /** * 根目录路径 function * * @param string ...$path 拼接 * @return string */ function base_path(string ...$path): string {} /** * app目录路径 function * * @param string ...$path 拼接 * @return string */ function app_path(string ...$path): string {} /** * app应用 function * * @return object */ function app(): object {} /** * 页面跳转 function * * @param string $page * @return void */ function app_jump(string $page): void {} /** * 消息对话框 function * * @param string $str * @param integer $type * @return boolean */ function dialog_msg(string $str, int $type = 0): bool {} /** * 输入对话框 function * * @return string */ function dialog_prompt(): string {} /** * 打开文件对话框 function * * @return string */ function dialog_file(): string {} /** * 打开文件夹对话框 function * * @param string $default_dir 默认文件夹路径位置 * @return string */ function dialog_dir(string $default_dir = ""): string {} /** * 保存文件对话框 function * * @param string $content 内容 * @param string $filename 文件名 如:test.txt * @param string $path 保存路径 如:D:/dir * @return boolean */ function dialog_save( string $content, string $filename, string $path = "" ): bool {} ``` ## 图标 图标必须在启动目录下,不然不显示 # webman-webview 套壳桌面版 [码云](https://gitee.com/kllxs_admin/webview-jacketing) # php-webview Linux桌面 拓展 [webview.so](https://github.com/KingBes/php-webview-expand) 鸡肋版 # 相关 [webview](https://github.com/webview/webview) [php-webview](https://github.com/0hr/php-webview) [dialog](https://github.com/ttytm/dialog) [static-php-cli](https://github.com/crazywhalecc/static-php-cli) [打包为安装包](https://jrsoftware.org/isinfo.php) #### 鸣谢 大雄