摘要:前台模块就是查询操作,以各种条件的查询操作,然后将查询的结果,遍历输出到模板中即可.<?php namespace app\index\controller; use app\admin\model\System; use think\Controller; use app\admin\model\Slide; use app\admin\m
前台模块就是查询操作,以各种条件的查询操作,然后将查询的结果,遍历输出到模板中即可.
<?php
namespace app\index\controller;
use app\admin\model\System;
use think\Controller;
use app\admin\model\Slide;
use app\admin\model\Product;
use app\admin\model\News;
use think\facade\Request;
class Index extends Controller
{
//首页
public function index()
{
//查询所有轮播
$slides = Slide::all();
$this->view->assign('slides',$slides);
//查询所有产品
$products = Product::order('update_time','desc')->limit(0,4)->all();
$this->view->assign('products',$products);
//查询头牌美女
$NewProduct = Product::where('sort',3)->limit(0,1)->find();
$this->view->assign('newProduct',$NewProduct);
//查询最新资讯
$News = News::limit(4)->all();
$this->view->assign('news',$News);
//渲染首页模板
return $this->view->fetch();
}
//关于我们页
public function about()
{
//查询系统表数据
$system = System::find();
$this->assign('system',$system);
//渲染关于我们模板
return $this->view->fetch();
}
//产品列表页
public function product()
{
//根据新增时间进行查询
$products = Product::order('create_time')->paginate(4);
$page = $products->render();
//查询总数
$count = Product::count();
//模板赋值
$this->view->assign('products',$products);
$this->view->assign('page',$page);
$this->view->assign('count',$count);
//渲染产品列表模板
return $this->view->fetch();
}
//新闻列表页
public function news()
{
//查询新闻内容
$news = News::order('create_time')->paginate(4);
$page = $news->render();
//热门新闻
$hotNew = News::order('update_time','desc')->find();
//最新新闻
$earNew = News::order('create_time','desc')->limit(6)->select();
//赋值
$this->view->assign('news',$news);
$this->view->assign('page',$page);
$this->view->assign('hotNew',$hotNew);
$this->view->assign('earNew',$earNew);
//渲染新闻列表模板
return $this->view->fetch();
}
//新闻详情页
public function conNew()
{
//通过id获取新闻信息
$newId = Request::param('id');
//热门新闻
$hotNew = News::order('update_time','desc')->find();
//最新新闻
$earNew = News::order('create_time','desc')->limit(6)->select();
//模板赋值
$this->view->assign('hotNew',$hotNew);
$this->view->assign('earNew',$earNew);
$new = News::get($newId);
$this->view->assign('new',$new);
//渲染新闻详情模板
return $this->view->fetch();
}
//产品内容详情页
public function conPro()
{
//获取id
$proId = Request::param('id');
$product = Product::where('id',$proId)->find();
//模板赋值
$this->view->assign('product',$product);
//渲染产品详情模板
return $this->view->fetch();
}
}





来个总结吧!
后台模块主要是对数据表的增删改查操作,然后将查询结果输出到视图中,再由视图返回给控制器来进行更新,删除等,删除的话使用软删除,其实本质就是新建一个软删除字段,删除时将当时的时间戳传入到此字段中,查询时如果此字段中有值,则不显示此条数据.
一张模型对应一张数据表,一个控制器的操作对应一个视图(也可以不渲染视图),视图传输到控制器中,主要通过Request类来获取客户端传来的数据.
前台模块中体来说就是查询操作,查询然后渲染到模板,要比后台模块简单的多
批改老师:韦小宝批改时间:2018-12-26 09:21:42
老师总结:对于简单的项目像企业站或者是个人博客等站点,这样写还是比较方便快捷的!