mouyong/webman-init-template

There is no license information available for the latest version (dev-master) of this package.

dev-master 2022-03-17 16:07 UTC

This package is auto-updated.

Last update: 2024-10-17 22:14:08 UTC


README

注意: 安装包之前,先移除项目的 composer.lock 文件,避免无法安装 phinx 包的问题 (https://www.workerman.net/q/8016)

rm composer.lock && composer require mouyong/webman-init-template:dev-master

启用插件

./webman plugin:install zhen-mu/support
./webman plugin:install mouyong/validate
./webman plugin:install mouyong/webman-init-template
rm app/controller/WebmanBaseController.php

引入的第三方包

使用

  1. 禁用默认路由,并按照文档配置路由信息 https://www.workerman.net/doc/webman/route.html

config/route.php

Route::disableDefaultRoute();

  1. 配置数据库连接信息 https://www.workerman.net/doc/webman/db/tutorial.html

config/database.php

return [
    // 默认数据库
    'default' => 'mysql',

    // 各种数据库配置
    'connections' => [
        'mysql' => [
            'driver'      => getenv('DB_DRIVER') ?: 'mysql',
            'host'        => getenv('DB_HOST') ?: '127.0.0.1',
            'port'        => getenv('DB_PORT') ?: 3306,
            'database'    => getenv('DB_DATABASE') ?: 'webman',
            'username'    => getenv('DB_USERNAME') ?: 'root',
            'password'    => getenv('DB_PASSWORD') ?: '',
            'unix_socket' => getenv('DB_UNIX_SOCKET') ?: '',
            'charset'     => getenv('DB_CHARSET') ?: 'utf8mb4',
            'collation'   => getenv('DB_COLLATION') ?: 'utf8mb4_bin',
            'prefix'      => getenv('DB_PREFIX') ?: '',
            'strict'      => getenv('DB_STRICT') ?: true,
            'engine'      => getenv('DB_ENGINE') ?: null,
        ],
    ],
];
  1. 在项目目录下创建 .env 文件,并更具需要配置环境变量

cp .env.example .env

# database
DB_DRIVER=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=webman
DB_USERNAME=root
DB_PASSWORD=root
  1. 控制器继承
<?php

namespace app\controller;

use support\Request;
use Mouyong\WebmanInitTemplate\Controllers\WebmanBaseController;

class Index extends WebmanBaseController
{
    <...>
}
  1. 错误处理集成

见文档 README.md 说明: https://github.com/mouyong/php-support

support/exception/Handle.php

<?php

namespace support\exception;

use Webman\Http\Request;
use Webman\Http\Response;
use Throwable;
use Webman\Exception\ExceptionHandler;
use ZhenMu\Support\Traits\WebmanResponseTrait;

class Handler extends ExceptionHandler
{
    use WebmanResponseTrait;

    <...>
    
    public function render(Request $request, Throwable $exception): Response
    {
        return $this->renderableHandle($request, $exception); // 这里进行调用,做了一些错误捕捉
    }
}