PHP code example of landao / webman-core

1. Go to this page and download the library: Download landao/webman-core 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/ */

    

landao / webman-core example snippets


//annotation.route
 "annotation" => [
    //路由注解
    'route' => [
        'enable' => true,
        // 扫描的目录
        'directories' => [
            app_path('controller') => [
            ],
            base_path('plugin/system/app/controller/admin') => [
                'prefix' => '/admin-api/system',// 路由前缀
                'as'=>'system:',// 路由别名
                'namespace' => 'plugin\system\app\controller\admin\\'// 控制器命名空间
            ]
        ],
    ],
],



namespace app\controller;

use Landao\WebmanCore\Annotation\Router\Get;
use Landao\WebmanCore\Annotation\Router\Group;
use Landao\WebmanCore\Annotation\Router\Post;
use support\Request;
use support\Response;

#[Group('/user')]
class UserController
{
    #[Get('/index')]
    public function index()
    {
        try {
            // 假设这里有一些业务逻辑
            $response = [
                'status' => 'success',
                'message' => 'Welcome to the user page!'
            ];

            return json($response);
        } catch (\Exception $e) {
            // 记录异常日志
            Log::error($e->getMessage());

            // 返回错误响应
            return json(['status' => 'error', 'message' => 'An error occurred'], 500);
        }
    }

    #[Post('/test/123')]
    public function test(): Response
    {
        return json('ceshi');
    }


}



namespace app\controller;

use Landao\WebmanCore\Annotation\Router\Get;
use Landao\WebmanCore\Annotation\Router\Group;
use Landao\WebmanCore\Annotation\Router\Post;
use Landao\WebmanCore\Annotation\Validation;
use Landao\WebmanCore\Annotation\Validation\Required;
use Landao\WebmanCore\Annotation\Validation\Regex;
use Landao\WebmanCore\Http\FormRequest;
use support\Log;
use support\Request;
use support\Response;

#[Group('/user')]
class UserController
{

    #[Post('/test/123')]
//    #[Regex("message", ruleValue: "~^\w+$~", attribute: "留言")]
    #[Validation(field: "message", rule: "tion test2(FormRequest $request):Response
    {
        return json('ceshi2');
    }
}



namespace app\controller;


use Landao\WebmanCore\Validation\Validate;

class UserRequest extends Validate
{

    protected array $rules = [
        "title" => [" * 自定义回调验证 
    * @return bool
    */
    protected function checkSafeTags() :bool
    {
        if(str_contains($this->data['message'],'<script'))
            return false;
        return true;
    }
}



namespace app\controller;

use Landao\WebmanCore\Annotation\Router\Get;
use Landao\WebmanCore\Annotation\Router\Group;
use Landao\WebmanCore\Annotation\Router\Post;
use Landao\WebmanCore\Annotation\Validation;
use Landao\WebmanCore\Annotation\Validation\Required;
use Landao\WebmanCore\Annotation\Validation\Regex;
use Landao\WebmanCore\Http\FormRequest;
use support\Log;
use support\Request;
use support\Response;

#[Group('/user')]
class UserController
{


    #[Post('/test/12')]
    #[Validation(class: UserRequest::class)]
    public function test2(FormRequest $request):Response
    {
        return json('ceshi2');
    }
}


return [
    '' => \Landao\WebmanCore\Exceptions\LanDaoException::class,//support\exception\Handler::class,
];

throw new UnauthorizedHttpException('账号或密码错误',['errorCode'=>40001]);



namespace plugin\system\app\enum;

use Landao\WebmanCore\Exceptions\ErrorCodeInterface;
use Landao\WebmanCore\Annotation\Description;
use Landao\WebmanCore\Enum\EnumExtend;

/**
 * 错误码枚举
 * 错误码规则:模块编号-功能编号-错误码编号-错误码序号 1_002_001_000
 */
enum ErrorCodeConstants: int implements ErrorCodeInterface
{
    use EnumExtend;

    // ========== 菜单模块 1-002-001-000 ==========
    #[Description('已经存在该名字的菜单')]
    case MENU_NAME_DUPLICATE = 1_002_001_000;
    #[Description('父菜单({})不存在<{}>')]
    case MENU_PARENT_NOT_EXISTS = 1_002_001_001;
    
    #[Description('不能设置{0}自己为父菜单{1}')]
    case MENU_PARENT_ERROR = 1_002_001_002;
    


    public function getValue(): int
    {
        return $this->value;
    }
}


ExceptionFactory::throwBusinessException(ErrorCodeConstants::MENU_NAME_DUPLICATE);
// 支持 {} 格式(按顺序替换)
ExceptionFactory::throwBusinessException(ErrorCodeConstants::MENU_PARENT_NOT_EXISTS, '新增');
// 支持 {0}, {1} 等索引格式
ExceptionFactory::throwBusinessException(ErrorCodeConstants::MENU_PARENT_ERROR, '新增','新增1');
 

// 假设我们有一个StatusEnum枚举
$status = StatusEnum::ENABLE;

// 比较两个枚举实例
$status->equals(StatusEnum::ENABLE); // true
$status->equals(StatusEnum::DISABLE); // false

// 比较枚举值
$status->valueEquals(1); // 如果ENABLE的值是1,则返回true
$status->valueEquals('active'); // 如果ENABLE的值是'active',则返回true



namespace plugin\system\app\controller;

use Landao\WebmanCore\Annotation\Desensitize;
use Landao\WebmanCore\Traits\ApiResponse;
use support\Request;


#[Desensitize(field: 'mobile', rule: 'mobile')]
#[Desensitize(field: 'email', rule: ['pattern' => '/(.{3})[\w\-]+@/', 'replacement' => '$1***@'])]
#[Desensitize(field: 'user_indo.mobile', rule: 'mobile')]
#[Desensitize(field: 'user_indo.email', rule: ['pattern' => '/(.{3})[\w\-]+@/', 'replacement' => '$1***@'])]
#[Desensitize(field: 'user_indo.chaer.mobile', rule: 'mobile')]
#[Desensitize(field: 'user_indo.chaer.email', rule: ['pattern' => '/(.{3})[\w\-]+@/', 'replacement' => '$1***@'])]
class IndexController
{
    use ApiResponse;

//    #[Desensitize(field: 'id_card', rule: ['pattern' => '/(\d{6})\d{8}(\d{4})/', 'replacement' => '$1********$2'])]
    public function index()
    {
//        return $this->success([
//            'mobile' => '13800138000',
//            'email' => '[email protected]',
//            'id_card' => '110101199001011234',
//            'user_indo'=>[
//                'mobile' => '13800138000',
//                'email' => '[email protected]',
//                'id_card' => '110101199001011234',
//                'chaer'=>[
//                    'mobile' => '13800138000',
//                    'email' => '[email protected]',
//                    'id_card' => '110101199001011234',
//                ]
//            ]
//        ]);
//        return $this->success([
//            [
//                'mobile' => '13800138000',
//                'email' => '[email protected]',
//                'id_card' => '110101199001011234',
//            ],
//            [
//                'mobile' => '13800138000',
//                'email' => '[email protected]',
//                'id_card' => '110101199001011234',
//            ],
//            [
//                'mobile' => '13800138000',
//                'email' => '[email protected]',
//                'id_card' => '110101199001011234',
//            ]
//        ]);
        return $this->success([
            'page'=>1,
            'list'=>[
                [
                    'mobile' => '13800138000',
                    'email' => '[email protected]',
                    'id_card' => '110101199001011234',
                    'user_info'=>[
                        'mobile' => '13800138000',
                        'email' => '[email protected]',
                    ]
                ],
                [
                    'mobile' => '13800138000',
                    'email' => '[email protected]',
                    'id_card' => '110101199001011234',
                ],
                [
                    'mobile' => '13800138000',
                    'email' => '[email protected]',
                    'id_card' => '110101199001011234',
                ]
            ]
        ]);
    }

}

shell

# 运行迁移
php webman landao:migrate

# 运行迁移并填充数据
php webman landao:migrate --seed

# 仅预览 SQL(不执行)
php webman landao:migrate --pretend

# 回滚最后一次迁移
php webman landao:migrate-rollback

# 回滚最后 3 次迁移
php webman landao:migrate-rollback --step=3

# 回滚所有迁移
php webman landao:migrate-rollback --all

shell
# 回滚最后一次迁移
php webman landao:migrate-rollback

# 回滚最后 3 次迁移
php webman landao:migrate-rollback --step=3

# 回滚所有迁移
php webman landao:migrate-rollback --all

shell
# 运行默认的 DatabaseSeeder
php webman landao:seeder

# 运行指定的 Seeder
php webman landao:seeder --class=UserSeeder
shell
# 主应用生成枚举, 默认生成在 app/enums 目录下, 
php webman landao:make-enum UserStatus

# 主应用生成枚举, 默认生成在 app/enums 目录下, 并指定子目录
php webman landao:make-enum System\\UserStatus

# webman 插件plugin 生成枚举, foo 生成在 plugin/foo/app/enums 目录下,
php webman landao:make-enum UserStatus --plugin=foo

# 多应用生成枚举,--multi-app=api 生成在 app/api/enums 目录下, 
php webman landao:make-enum UserStatus --multi-app=api
shell
# 主应用生成,默认生成在 app/models 目录下,
php webman landao:make-model User

# 主应用生成,默认生成在 app/models/System 目录下,
php webman landao:make-model Seytem\\User

# webman 插件plugin 生成, foo 生成在 papp/models/System 目录下,
php webman landao:make-model User --plugin=foo

# 多应用生成,,--multi-app=api 生成在 app/api/models 目录下, 
php webman landao:make-model User --multi-app=api
shell
# 主应用生成,默认生成在 app/requests 目录下,
php webman landao:make-request User

# 主应用生成,默认生成在 app/requests/System 目录下,
php webman landao:make-request Seytem\\User

# webman 插件plugin 生成, foo 生成在 papp/requests/System 目录下,
php webman landao:make-request User --plugin=foo

# 多应用生成,,--multi-app=api 生成在 app/api/requests 目录下, 
php webman landao:make-request User --multi-app=api
json
{
    "code": 401,
    "msg": "账号或密码错误",
    "data": {
        "domain": "127.0.0.1:8787",
        "method": "POST",
        "request_url": "POST /admin-api/system/auth/login?tenantName=****&username=peadmin&password=123456qwe@A2&code=brmam&captchaKey=$2y$10$bNg0RzZkvzhkBr8XZiSDJOJTjwFc8pdTd7LJ1a5E6ywF4t.KUCwgm",
        "timestamp": "2025-07-14 16:41:21",
        "client_ip": "172.0.0.1",
        "request_param": {
            "tenant_name": "****",
            "username": "peadmin",
            "password": "****",
            "code": "brmam",
            "captcha_key": "$2y$10$bNg0RzZkvzhkBr8XZiSDJOJTjwFc8pdTd7LJ1a5E6ywF4t.KUCwgm"
        },
        "error_message": "账号或密码错误",
        "error_trace": [
            "#0 /www/php/landao/webman/plugin/system/app/controller/admin/auth/AuthController.php(54): plugin\\system\\app\\repositories\\UserRepo->doLogin('\\xE6\\x99\\xBA\\xE6\\xB1\\x87\\xE9\\x80\\x9A\\xE8\\xBD\\xAF\\xE4\\xBB\\xB6', 'peadmin', '****', 'WEB')",
            "#20 {main}"
        ],
        "file": "/www/php/landao/webman/plugin/system/app/repositories/UserRepo.php",
        "line": 64
    }
}