新项目流程4--快捷函数

在 根目录/index.php 中倒入自定义函数

//倒入自定义函数
include_once "./protected/functions.php";

在 根目录/protected 下建立自定义函数function.php

常用函数:

function ASSWWW(){
    return Yii::app()->request->baseUrl.'/assets/www/';
}

function ASSADM(){
    return Yii::app()->request->baseUrl.'/assets/adm/';
}

function params($str){
    return Yii::app()->params[$str];
}

function gCid(){//get controller id
    return Yii::app()->controller->id;
}

function gUid(){//get user id
    return Yii::app()->user->id;
}
function gUname(){
    return Yii::app()->user->name;
}


function p($arr){
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
}

function v($arr){
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
    echo '<pre>';
    var_dump($arr);
    echo '</pre>';
}

function alert($mess, $returnUrl=FALSE){
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
    echo '<script type="text/javascript">';
    echo 'alert("'.$mess.'");';
    if (!$returnUrl) {
        echo 'history.back();';//后退不刷新
    }else{
        echo 'window.location="'.$returnUrl.'";';
    }
    echo '</script>';
}

function gBackUrl(){
    return Yii::app()->request->urlReferrer;
}

function imgUploads($model,$mName,$img){
    //$model  模型
    //$mName  模型名称
    //$img    字段名称
    if($_FILES[$mName]['name'][$img]){
        $path_file = "/uploads/$mName/img/".date('Ym')."/";
        $path = dirname(Yii::app()->BasePath).$path_file;
        if (!file_exists($path)) {
            @mkdir($path, 0777);//建立目录
            @chmod($path, 0777);//给与权限
        }
        $thumb = CUploadedFile::getInstance($model,$img);
        if($thumb){
            $preRand = 'img'.time().mt_rand(0,9999);
            $imgName = $preRand.".".$thumb->extensionName;
            $thumb->saveAs($path.$imgName);
            $model->$img = $path_file.$imgName;
            return true;
        }
    }
}

function thumbUploads($model,$mName,$img,$img_small,$width=200,$height=200){
    //img+thumb 组合上传
    //$model    模型
    //$mName    模型名称
    //$img      字段名称
    //img_small 缩略图字段名称
    //$width    缩略图宽
    //$height   缩略图高
    if($_FILES[$mName]['name'][$img]){
        $path_file = "/uploads/$mName/img/".date('Ym')."/";
        $path = dirname(Yii::app()->BasePath).$path_file;
        if (!file_exists($path)) {
            @mkdir($path, 0777);//建立目录
            @chmod($path, 0777);//给与权限
        }
        $thumb = CUploadedFile::getInstance($model,$img);
        if($thumb){
            $path_thumb_file = "/uploads/$mName/thumb/".date('Ym')."/";
            $path_thumb = dirname(Yii::app()->BasePath).$path_thumb_file;
            if (!file_exists($path_thumb)) {
                @mkdir($path_thumb, 0777);
                @chmod($path_thumb, 0777);
            }
            $preRand = 'img'.time().mt_rand(0,9999);
            $imgName = $preRand.".".$thumb->extensionName;
            $thumb->saveAs($path.$imgName);
            $model->$img = $path_file.$imgName;
            $model->$img_small = $path_thumb_file.$imgName;
            
            $CThumb = Yii::app()->CThumb;
            $CThumb->image = $path.$imgName;
            $CThumb->width = $width;
            $CThumb->height = $height;
            $CThumb->mode = 3;
            $CThumb->directory = $path_thumb;
            $CThumb->defaultName = $preRand;
            $CThumb->createThumb();
            $CThumb->save();
            return true;
        }
    }
}

 

设置url美化

隐藏index.php

根目录下设置

RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

 

posted @ 2018-12-07 12:56  一懒众衫小丶  阅读(173)  评论(0编辑  收藏  举报