zoukankan      html  css  js  c++  java
  • laravel (第三方类)

    1、laravel自身对类的引入提供了很大的方便,使用时use即可,并且这些类提供了很多的方法。

      ①model。model类的引用较大的方便了数据库的使用,在model类中定义需要的查询方法,控制器中引用方法输出查询结果,这样更加规范代码。

    use AppHttpModelUser;

      ②Input。用于接受数据,all()接受所有的数据,expect(‘’)接受除了不需要字段之外的所有字段,方便数据的添加。

    use IlluminatesupportfacadesInput;
    ...
    function index(){
          $input = Input::all();       #接受所有值
          $input = Input::expect();    #接受排除之外的值
    }

      ③Validator。验证类,对input接受的值进行过滤处理。

    use IlluminatesupportfacadesValidator; 
    ...
    function index(){
    $Input = [];
         $rules = [
                'cate_name' =>'required',                             #对数组中的值进行验证
                'cate_order'=>'integer'
            ];
            $message = [
                'cate_name.required'=>'分类名称必须填写',                #返回信息重新编写
                'cate_order.integer' =>'分类排序必须为数字格式'
            ];
            $validator = Validator::make($input,$rules,$message);      #make()此方法用于处理接受值
            if($validator->passes()){                                  #验证是否通过
                $re = Category::create($input);
                if($re){
                    return redirect('admin/category');
                }else{
                    return view('admin.category.create')->with('error','添加数据失败');
                }
            }else{
                return back()->withErrors($validator);                  #laravel提供withErrors()将错误的信息返回到模板中
            }

    }    

      ④Crypt。加密处理,用于密码验证

    use IlluminatesupportfacadesCrypt;
    ...
    function index(){
         $str = 123;
         echo Crypt::encrypt($str);                      #加密
         echo "<br/>".Crypt::decrypt($str1);             #解密
    }

      

    2、引入外部类(验证码类)

      可以自己建立一个文件夹,专门放置第三方的类,引用时从根目录开始查找

      注:laravel自己封装了session,不需要开启session_start(),在引入的第三方类中请求seesion时,需要先开启session_start();可以放到入口文件中

    require_once 'resources/org/code/Code.class.php';

      public function code(){
        $code = new Code;             #实例化第三方类,注意需要在根目录,即加上‘’,否则无法引入
        echo $code -> make();          #make()方法用于显示验证码,此方法将显示的验证码存储到了session中
        echo "code";
      }

     public function getcode(){

        $code = new Code;             #实例化
        echo $code->get();             #get()用于获取验证码

     }

        下面是code验证码源码,可以作为参考练习laravel引用第三方类

    <?php
    
    class Code{
    
        //资源
        private $img;
        //画布宽度
        private $width=100;
        //画布高度
        private $height=30;
        //背景颜色
        private $bgColor='#ffffff';
        //验证码
        private $code;
        //验证码的随机种子
        private $codeStr='23456789abcdefghjkmnpqrstuvwsyz';
        //验证码长度
        private $codeLen=4;
        //验证码字体
        private $font;
        //验证码字体大小
        private $fontSize=16;
        //验证码字体颜色
        private $fontColor='';
    
        public function __construct() {
        }
    
        //创建验证码
        public function make()
        {
            if(empty($this->font))
            {
                $this->font = __DIR__.'/consola.ttf';
            }
            $this->create();//生成验证码
            header("Content-type:image/png");
            imagepng($this->img);
            imagedestroy($this->img);
            exit;
        }
    
        //设置字体文件
        public function font($font)
        {
            $this->font= $font;
            return $this;
        }
    
        //设置文字大小
        public function fontSize($fontSize)
        {
            $this->fontSize=$fontSize;
            return $this;
        }
    
        //设置字体颜色
        public function fontColor($fontColor)
        {
            $this->fontColor = $fontColor;
            return $this;
        }
    
        //验证码数量
        public function num($num)
        {
            $this->codeLen=$num;
            return $this;
        }
    
        //设置宽度
        public function width($width)
        {
            $this->width = $width;
            return $this;
        }
    
        //设置高度
        public function height($height)
        {
            $this->height = $height;
            return $this;
        }
    
        //设置背景颜色
        public function background($color)
        {
            $this->bgColor = $color;
            return $this;
        }
    
        //返回验证码
        public function get() {
            return $_SESSION['code'];
        }
    
        //生成验证码
        private function createCode() {
            $code = '';
            for ($i = 0; $i < $this->codeLen; $i++) {
                $code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)];
            }
            $this->code = strtoupper($code);
            $_SESSION['code'] = $this->code;
        }
    
        //建画布
        private function create() {
            if (!$this->checkGD())
                return false;
            $w = $this->width;
            $h = $this->height;
            $bgColor = $this->bgColor;
            $img = imagecreatetruecolor($w, $h);
            $bgColor = imagecolorallocate($img, hexdec(substr($bgColor, 1, 2)), hexdec(substr($bgColor, 3, 2)), hexdec(substr($bgColor, 5, 2)));
            imagefill($img, 0, 0, $bgColor);
            $this->img = $img;
            $this->createLine();
            $this->createFont();
            $this->createPix();
            $this->createRec();
        }
    
        //画线
        private function createLine(){
            $w = $this->width;
            $h = $this->height;
            $line_color = "#dcdcdc";
            $color = imagecolorallocate($this->img, hexdec(substr($line_color, 1, 2)), hexdec(substr($line_color, 3, 2)), hexdec(substr($line_color, 5, 2)));
            $l = $h/5;
            for($i=1;$i<$l;$i++){
                $step =$i*5;
                imageline($this->img, 0, $step, $w,$step, $color);
            }
            $l= $w/10;
            for($i=1;$i<$l;$i++){
                $step =$i*10;
                imageline($this->img, $step, 0, $step,$h, $color);
            }
        }
    
        //画矩形边框
        private function createRec() {
            //imagerectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->fontColor);
        }
    
        //写入验证码文字
        private function createFont() {
            $this->createCode();
            $color = $this->fontColor;
            if (!empty($color)) {
                $fontColor = imagecolorallocate($this->img, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
            }
            $x = ($this->width - 10) / $this->codeLen;
            for ($i = 0; $i < $this->codeLen; $i++) {
                if (empty($color)) {
                    $fontColor = imagecolorallocate($this->img, mt_rand(50, 155), mt_rand(50, 155), mt_rand(50, 155));
                }
                imagettftext($this->img, $this->fontSize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontColor, $this->font, $this->code [$i]);
            }
            $this->fontColor = $fontColor;
        }
    
        //画线
        private function createPix() {
            $pix_color = $this->fontColor;
            for ($i = 0; $i < 50; $i++) {
                imagesetpixel($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
            }
    
            for ($i = 0; $i < 2; $i++) {
                imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
            }
            //画圆弧
            for ($i = 0; $i < 1; $i++) {
                // 设置画线宽度
                imagearc($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height)
                    , mt_rand(0, 160), mt_rand(0, 200), $pix_color);
            }
            imagesetthickness($this->img, 1);
        }
    
        //验证GD库
        private function checkGD() {
            return extension_loaded('gd') && function_exists("imagepng");
        }
    
    }
    以此来记录编程之路,偶尔需要静下心来写点东西。
  • 相关阅读:
    装饰
    统一软件开发过程之2:用例文本书写
    统一软件开发过程之1:创建领域模型
    工厂方法
    volatile
    中介者
    建造者
    C#委托,事件与回调函数
    控件资源嵌入
    装饰
  • 原文地址:https://www.cnblogs.com/yaradish/p/9451323.html
Copyright © 2011-2022 走看看