建立自己的异常类方式

第一种方式 异常使用工具包,错误码和错误信息,每个项目单独控制,互不影响 

本次使用laravel5.7 

1 安装laravel

2 composer.json里面引入git项目包

"url": "https://github.com/brady-wang/tool.git"
"brady/tool": "dev-master"
{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": [
        "framework",
        "laravel"
    ],
    "license": "MIT",
    "repositories": {
        "0": {
            "type": "git",
            "url": "https://github.com/brady-wang/tool.git"
        },
        "packagist": {
            "type": "composer",
            "url": "https://mirrors.aliyun.com/composer/"
        }
    },
    "require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.8.*",
        "laravel/tinker": "^1.0",
        "brady/tool": "dev-master"
    },
    "require-dev": {
        "beyondcode/laravel-dump-server": "^1.0",
        "filp/whoops": "^2.0",
        "fzaninotto/faker": "^1.4",
        "mockery/mockery": "^1.0",
        "nunomaduro/collision": "^3.0",
        "phpunit/phpunit": "^7.5"
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true,
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    }
}

2. app下新建目录 Constants

目录下新建 ErrorCode.php ErrorMsg.php

php

namespace App\Constants;

class ErrorCode
{
    /**
     * 基本错误码
     */
    const SUCCESS = 200;
    const UNAUTHORIZED = 401;
    const FAIL = 500;


    const USER_NOT_EMPTY = 10001;
    const PASSWORD_NOT_EMPTY = 10002;
    const PASSWORD_RE_NOT_EMPTY = 10003;
    const EMAIL_NOT_EMPTY = 10004;
}
 '成功' ,
	ErrorCode::UNAUTHORIZED => '暂无权限!' ,
	ErrorCode::FAIL         => '失败' ,

	ErrorCode:: USER_NOT_EMPTY        => '用户名不能为空' ,
	ErrorCode::PASSWORD_NOT_EMPTY         => '请输入密码' ,
	ErrorCode::PASSWORD_RE_NOT_EMPTY         => '请再次输入密码',
	ErrorCode::EMAIL_NOT_EMPTY         => '邮箱不能为空',

];

3.在app下的provider下建立服务提供者  

ExceptionServiceProvider
 
 

4 注册服务提供者到app下的bootstrap下的app文件

 

$app->register(App\Providers\ExceptionServiceProvider::class);

5

在控制器或者服务里面使用

getMessage());
		}
		return view('registor/index');
	}

	public function registor()
	{
		ExceptionResult::throwException("sdfsdfsf",true);
	}
} 

 返回结果

建立自己的异常类方式_第1张图片

 

 

 

第二种方式,错误码和错误信息也放到工具包 一次设置,多次取用

 

你可能感兴趣的:(建立自己的异常类方式)