thinkphp5创建文章的增删改查

第一先创建数据库:

create table `bk_article`(

  `id` mediumint(9) auto_inctement primary key,

  `title` varchar(40),

   `cateid` mediumint(9) comment‘栏目第’,

   `kewords` varchar(30) comment'关键词',

   `desc` varchar(225) comment'描述',

   `author` varchar(60) comment'作者',

  `thumb` varchar(255) not null comment'图片',

   `comtent` text not null comment'内容',

   `click` mediument(9) comment'点击',

    `zan` mediument(9) comment'点赞',

   `rec` tinyint(1) comment'0推荐,1不推荐',

   `time` int(10) comment'时间'

   )

这里需要注意的是图片的上传和修改删除在模型中定义路径,
先创建目录引入文件,然后控制器中Admin.php:

<?php
namespace app\admin\controller;
use app\admin\model\Cate as CateModel;
use app\admin\model\Article as ArticleModel;
use app\admin\controller\Common;
class Article extends Common
{

    public function lst(){
        $artres=db('article')->field('a.*,b.catename')->alias('a')->join('bk_cate b','a.cateid=b.id')->order('a.id desc')->paginate(10);
        $this->assign('artres',$artres);
        return view();
    }

    public function add(){
        if(request()->isPost()){
            $data=input('post.');
            $data['time']=time();
            $validate = \think\Loader::validate('Article');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $article=new ArticleModel();
            if($article->save($data)){
                $this->success('添加文章成功',url('lst'));
            }else{
                $this->error('添加文章失败!');
            }
            return;
        }
        $cate=new CateModel();
        $cateres=$cate->catetree();
        $this->assign('cateres',$cateres);
        return view();
    }

    public function edit(){
        if(request()->isPost()){
            $data=input('post.');
            $validate = \think\Loader::validate('Article');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $article=new ArticleModel;
            $save=$article->update($data);
            if($save){
                $this->success('修改文章成功!',url('lst'));
            }else{
                $this->error('修改文章失败!');
            }
            return;
        }
        $cate=new CateModel();
        $cateres=$cate->catetree();
        $arts=db('article')->where(array('id'=>input('id')))->find();
        $this->assign(array(
            'cateres'=>$cateres,
            'arts'=>$arts,
            ));
        return view();
    }

    public function del(){
        if(ArticleModel::destroy(input('id'))){
            $this->success('删除文章成功!',url('lst'));
        }else{
            $this->error('删除文章失败!');
        }
    }

模型层model/Admin.php:

<?php
namespace app\admin\model;
use think\Model;
class Article extends Model
{
    
    protected static function init()
    {
          Article::event('before_insert',function($article){
          if($_FILES['thumb']['tmp_name']){
                $file = request()->file('thumb');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                     //$thumb=ROOT_PATH . 'public' . DS . 'uploads'.'/'.$info->getExtension();
                    $thumb= '/11/'.'public'.DS.'uploads'.'/'.$info->getSaveName();
                    $article['thumb']=$thumb;
                }
            }
      });


          Article::event('before_update',function($article){
          if($_FILES['thumb']['tmp_name']){
                  $arts=Article::find($article->id);
                  $thumbpath=$_SERVER['DOCUMENT_ROOT'].$arts['thumb'];
                if(file_exists($thumbpath)){
                    @unlink($thumbpath);
                }
                $file = request()->file('thumb');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                     //$thumb=ROOT_PATH . 'public' . DS . 'uploads'.'/'.$info->getExtension();
                    $thumb=   '/11/'.'public'.DS.'uploads'.'/'.$info->getSaveName();
                    $article['thumb']=$thumb;
                }

            }
      });

          Article::event('before_delete',function($article){
          
                  $arts=Article::find($article->id);
                  $thumbpath=$_SERVER['DOCUMENT_ROOT'].$arts['thumb'];
                if(file_exists($thumbpath)){
                    @unlink($thumbpath);
                }
        });

    }
}

验证:validate/Admin.php

<?php
namespace app\admin\validate;
use think\Validate;
class Article extends Validate
{

    protected $rule=[
        'title'=>'unique:article|require',
        'cateid'=>'require',
        'content'=>'require',
    ];


    protected $message=[
        'title.require'=>'文章标题不得为空!',
        'title.unique'=>'文章标题不得重复!',
        // 'title.max'=>'文章标题长度大的大于25个字符!',
        'cateid.require'=>'文章所属栏目不得为空!',
        'content.require'=>'文章内容不得为空!',
    ];

    protected $scene=[
        'add'=>['title','cateid','content'],
        'edit'=>['title','cateid'],
    ];

模板文件中要只有列表输出的用法
lst.htm:

{volist name="artres" id="art"}
                                    <tr>
                                        <td align="center">{$art.id}</td>
                                        <td><?php echo mb_substr($art['title'], 0,9,'utf-8'); if(strlen($art['title'])>9){echo '...';}?></td>
                                        <td align="center">
                                            {if condition="$art['thumb'] neq ''"}
                                            <img src="{$art.thumb}" height="30">
                                            {else /}
                                            暂无缩略图
                                            {/if}
                                        </td>
                                        <td align="center">{if condition="$art['author'] eq ''"}未填写{else /}{$art.author}{/if}</td>
                                        <td align="center">{if condition="$art['rec'] eq 0"}未推荐{else /}已推荐{/if}</td>
                                        <td align="center">{$art.catename}</td>
                                        <td align="center">
                                            <a href="{:url('edit',array('id'=>$art['id']))}" class="btn btn-primary btn-sm shiny">
                                                <i class="fa fa-edit"></i> 编辑
                                            </a>
                                            <a href="#" onClick="warning('确实要删除吗', '{:url('del',array('id'=>$art['id']))}')" class="btn btn-danger btn-sm shiny">
                                                <i class="fa fa-trash-o"></i> 删除
                                            </a>
                                        </td>
                                    </tr>
                                    {/volist}

分页:{$artres->render()}
add.htm:

<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data" >
                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">标题</label>
                            <div class="col-sm-6">
                                <input class="form-control"  placeholder="" name="title"  required="" type="text">
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">作者</label>
                            <div class="col-sm-6">
                                <input class="form-control"  placeholder="" name="author"  type="text">
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">关键词</label>
                            <div class="col-sm-6">
                                <input class="form-control"  placeholder="" name="keywords" type="text">
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">描述</label>
                            <div class="col-sm-6">
                                <textarea name="desc" class="form-control"></textarea>
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">缩略图</label>
                            <div class="col-sm-6">
                                <input  placeholder="" name="thumb"  type="file">
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">是否推荐</label>
                            <div class="col-sm-6">
                                <label style="margin-right:15px;">
                                    <input name="rec" value="1" type="radio">
                                    <span class="text">是</span>
                                </label>
                                <label style="margin-right:15px;">
                                    <input checked="checked" class="inverted" name="rec" value="0" type="radio">
                                    <span class="text">否</span>
                                </label>
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">所属栏目</label>
                            <div class="col-sm-6">
                                <select name="cateid">
                                    {volist name="cateres" id="cate"}
                                    <option value="{$cate.id}">{if condition="$cate['level'] neq 0"}|{/if}<?php echo str_repeat('-', $cate['level']*4)?>{$cate.catename}</option>
                                    {/volist}
                                </select>
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>
                        <div class="form-group">
                            <label for="username"  required="" class="col-sm-2 control-label no-padding-right">内容</label>
                            <div class="col-sm-6">
                                <textarea id="content" name="content"></textarea>
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-default">保存信息</button>
                            </div>
                        </div>
                    </form>

edit.htm:

 <form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data" >
                        <input type="hidden" name="id" value="{$arts.id}">
                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">标题</label>
                            <div class="col-sm-6">
                                <input class="form-control"  placeholder=""  value="{$arts.title}" name="title" required="" type="text">
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">作者</label>
                            <div class="col-sm-6">
                                <input class="form-control"  placeholder=""  value="{$arts.author}" name="author" type="text">
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">关键词</label>
                            <div class="col-sm-6">
                                <input class="form-control"  placeholder="" name="keywords"  value="{$arts.keywords}" type="text">
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">描述</label>
                            <div class="col-sm-6">
                                <textarea name="desc" class="form-control">{$arts.desc}</textarea>
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">缩略图</label>
                            <div class="col-sm-6">
                                <input style="float:left;"  placeholder="" name="thumb" type="file">
                                {if condition="$arts['thumb'] neq ''"}
                                <img style="float:left;" src="{$arts.thumb}" width="30">
                                {else /}
                                暂无缩略图
                                {/if}
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">是否推荐</label>
                            <div class="col-sm-6">
                                <label style="margin-right:15px;">
                                    <input {if condition="$arts['rec'] eq 1"} checked="checked" {/if} name="rec" value="1" type="radio">
                                    <span class="text">是</span>
                                </label>
                                <label style="margin-right:15px;">
                                    <input {if condition="$arts['rec'] eq 0"} checked="checked" {/if} class="inverted" name="rec" value="0" type="radio">
                                    <span class="text">否</span>
                                </label>
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>

                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">所属栏目</label>
                            <div class="col-sm-6">
                                <select name="cateid">
                                    {volist name="cateres" id="cate"}
                                    <option {if condition="$cate['id'] eq $arts['cateid']"}selected="selected"{/if} value="{$cate.id}">{if condition="$cate['level'] neq 0"}|{/if}<?php echo str_repeat('-', $cate['level']*4)?>{$cate.catename}</option>
                                    {/volist}
                                </select>
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>
                        <div class="form-group">
                            <label for="username" class="col-sm-2 control-label no-padding-right">内容</label>
                            <div class="col-sm-6">
                                <textarea id="content"  required="" name="content">{$arts.content}</textarea>
                            </div>
                            <p class="help-block col-sm-4 red">* 必填</p>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-10">
                                <button type="submit" class="btn btn-default">保存信息</button>
                            </div>
                        </div>
                    </form>

标签: 无

发表评论:

whatsapp营销