laravel5.8笔记六:公共函数和常量设置

公共函数

创建bootstrap/common.php

<?php
// 发送短信
function sendSMS($mobile){

}
// 发送邮件
function sendMail($mail){

}

// 密码生成
function pass(){
    return rand(100000,999999);
}

// 自定义打印数据
if(!function_exists('p')){
    function p($data) {
        /**
         * 格式化打印数据
         * @param $data  array/string 需要打印的数据
         */
//        header("Content-type:text/html;charset=utf-8");
        // 定义样式
        $str = '<pre style="display: block;padding: 9.5px;margin: 44px 0 0 0;font-size: 13px;line-height: 1.42857;color: #333;word-break: break-all;word-wrap: break-word;background-color: #F5F5F5;border: 1px solid #CCC;border-radius: 4px;">';
        // 如果是boolean或者null直接显示文字;否则print
        if (is_bool($data)) {
            $show_data = $data ? 'true' : 'false';
        } elseif (is_null($data)) {
            $show_data = 'null';
        } else {
            $show_data = print_r($data, true);
        }
        $str .= $show_data;
        $str .= '</pre>';
        echo $str;
    }
}

常量设置

bootstrap/initBase.php

<?php
// 默认时间
define('SYSTEM_TIME',time());
// 默认分页条数
define('PAGE_SIEZ',15);
// oos路径
define('OOS_URL','https://gz.bcebos.com/v1/gz1802/');//oos

bootstrap/app.php引入:

<?php
// 引入常量文件
require __DIR__ . '/initBase.php';
// 公共函数
require __DIR__ . '/common.php';

只有在app.php引入了,才会全局加载生效。vendor/autoload.php也可以引入,但不推荐操作

全局调用(控制器,模型 ...)

public function index(){
        echo SYSTEM_TIME;
        echo pass();

    }
原文地址:https://www.cnblogs.com/wesky/p/10444461.html