<file_sep><?php



namespace core\mybase;
use core\Application;


//视图基类，显示数据
class View
{
    public $view_dir;//视图模板路径
    //定义一个数组，用来存储数据
    public $data = array();//视图数据

    //打开视图方法
    public function display($view_name=null){
        //include_once "application/home/views/index.html";
        //视图名称默认为方法名称
        $view_name=isset($view_name)?$view_name:Application::$action;
        include_once $this->view_dir.$view_name.".html";
    }

    //写一个数据填充方法，放到定义好的数组里面去
    public function assign($name,$value){
        //关联数组
        $this->data[$name] = $value;
    }
}<file_sep><?php


//入口文件
//1.注册自动加载方法
require "core/MyLoad.class.php";
\core\Myload::registerAutoLoad();

//3.加载配置文件和常亮
require "configs/config.php";
require "configs/constants.php";

//2.启动核心运行类(前端处理器)
\core\Application::run();

<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 9:25
 */

namespace application\admin\models;


use core\mybase\Model;

class SubscribeModel extends Model
{
    public function list($args=null){
        $sql="SELECT * FROM subscribe";
        return $this->query($sql,$args);
    }

    public function count(){
        $sql="SELECT COUNT(1) AS total FROM subscribe";
        $result=$this->query($sql,null);
        return $result[0]['total'];
    }
}<file_sep><?php


namespace core\mybase;
use core\Application;
use core\MySession;


//前台权限验证控制器类
class HomeGroupController extends Controller
{
    //定义不需要验证的方法
    private $pass=array('register','<PASSWORD>user','login');

    public function __construct()
    {
        //先调用父类的构造，调用父类的初始化视图的方法，加载视图
        parent::__construct();
        //1.session初始化
        $this->initSession();
        //2.权限验证，登录验证
        $this->checkLogin();
    }

    //1.封装session初始化，便于扩展
    private function initSession(){
        MySession::startSeesion();
    }

    //登录验证
    private function checkLogin(){
        //确定需要验证的方法
        if (in_array(Application::$action,$this->pass)){//不需要验证的方法
            //如果在数组里，则不处理
        }else{//不在则需要验证的方法
            //判断是否保持会话
            //复选框的值只要登录后，不管有没有选中，都会保存在会话里面。这里拿其他的值id或者name都可以
            if (isset($_SESSION['remember'])){
                //说明已经登录不处理
            }else{
                //否则未登录，跳转到登录页面
                header("location:index.php?g=home&c=login&a=index");
            }
        }
    }

    //操作成功
    public function success($msg,$url){
        echo "<h1>(#^.^#)</h1>";
        echo $msg."<br>";
        header("refresh:2,url=index.php".$url);
    }

    //操作失败
    public function fail($msg,$url){
        echo "<h1>o(╥﹏╥)o</h1>";
        echo $msg."<br>";
        header("refresh:2,url=index.php".$url);
    }
}<file_sep><?php

namespace core;

class Myload
{
    //定义静态自动加载函数
    public static function myAutoLoad($className){
        //完成由命名空间构成的类路径信息的类文件的加载
        //例如：Application\Admin\Controller\AdminController
        //问题：include等加载函数使用的是正斜杠路径，需要进行转换为：Application/Admin/Controller/AdminController.".class.php"
        $path = str_replace("\\","/",$className);
        $filePath = $path.".class.php";
        if (file_exists($filePath)){
            include_once $filePath;
        }else{
            die('无法加载文件：'.$filePath);
        }
    }

    //注册自动加载的函数
    public static function registerAutoLoad(){
        spl_autoload_register("self::myAutoLoad");
    }

}<file_sep><?php


namespace application\home\controllers;


use core\mybase\Controller;

class LoginController extends Controller
{
    //打开登录页面方法
    public function index()
    {
        $this->display();
    }

    //打开注册页面方法
    public function signup(){
        $this->display();
    }

}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 9:28
 */

namespace application\admin\controllers;


use application\admin\models\SubscribeModel;
use core\mybase\Controller;

class SubscribeController extends Controller
{
    public function list(){
        $sm=new SubscribeModel();
        $result=$sm->list($agrs=null);
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }
}<file_sep><?php

namespace core;

//启动session类
class MySession
{
    //启动session
    public static function startSeesion(){
        if (!isset($_SESSION)){
            session_start();
        }
    }

    //销毁session
    public static function destorySession(){
        //清空session的值
        session_unset();
        //清空客户端的cookie
        if (isset($_COOKIE[session_name()])){
            setcookie(session_name(),null,time()-1,"/");
        }
        //销毁session
        session_destroy();
    }

    //session添加数据
    public static function setSession($name,$value){
        $_SESSION[$name] = $value;
    }

    //session获取数据
    public static function getSession($name){
        if (isset($_SESSION[$name])){
            return $_SESSION[$name];
        }else{
            return null;
        }
    }

    //延长会话保持时间
    //setcookie函数的第四个参数为cookie路径，用来指定cookie被发送到服务器的哪一个目录路径下，其中"/"指的是站点根目录
    public static function extendSession($time=1*60*60){
        setcookie(session_name(),session_id(),time()+$time,"/");
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/6
 * Time: 10:11
 */

namespace application\admin\controllers;


use core\mybase\Controller;

class LoginController extends Controller
{
    public function index()
    {
        $this->display();
    }

}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/28
 * Time: 14:38
 */

namespace core\mybase;

//定义Model类的属性和构造函数。
//Model基类中调用PDO使用全限定类名：\PDO
class Model
{
    private $dsn = "mysql:dbname=education;host=localhost:3306";
    private $username = "root";
    private $password = "123";
    private $option = array(\PDO::ATTR_PERSISTENT=>true,\PDO::ATTR_ORACLE_NULLS=>true,\PDO::ERRMODE_EXCEPTION=>true);
    private $pdo;//pdo对象

    //构造函数从配置文件$C读取数据库配置信息，如果配置为空，则使用Model的初始属性。
    public function __construct()
    {
        global $C;
        //isset，判断配置文件是否有写，如果有，用配置文件的信息，没有就用当前
        $this->dsn=isset($C['db']['dsn'])?$C['db']['dsn']:$this->dsn;
        $this->username=isset($C['db']['username'])?$C['db']['username']:$this->username;
        $this->password=isset($C['db']['password'])?$C['db']['password']:$this->password;
        $this->option=isset($C['db']['option'])?$C['db']['option']:$this->option;
    }

    //定义获取PDO对象的方法
    public function getPDO(){
        //如果当前pdo对象等于空，才创建一个新的对象
        if ($this->pdo == null){
            $this->pdo = new \PDO($this->dsn,$this->username,$this->password,$this->option);
        }
        //然后返回这个对象
        return $this->pdo;
    }

    //销毁PDO对象的方法
    public function destoryPDO(){
        //当当前对象不为空，代表有实例化对象，则销毁，置为空
        if ($this->pdo != null){
            $this->pdo = null;
        }
    }

    //增删改数据的通用方法
    public function execute($sql,$args){
        $result = 0;
        try{
            //连接数据库，并执行预编译
            $pdostmt = $this->getPDO()->prepare($sql);
            if ($args != null){
                for ($i = 0;$i < count($args);$i++){
                    $pdostmt->bindParam($i+1,$args[$i]);
                }
            }
            $pdostmt->execute();
            $result = $pdostmt->rowCount();
        }catch (\PDOException $e){
            echo "持久化异常：".$e;
        }
        return $result;
    }

    //查询的通用方法
    public function query($sql,$args){
        try{
            //连接数据库，并执行预编译
            $pdostmt = $this->getPDO()->prepare($sql);
            if ($args != null){
                for ($i = 0;$i < count($args);$i++){
                    $pdostmt->bindParam($i+1,$args[$i]);
                }
            }
            $pdostmt->execute();
            //获得二维关联数组
            return $pdostmt->fetchAll(\PDO::FETCH_ASSOC);
        }catch (\PDOException $e){
            echo "持久化异常：".$e;
            return null;
        }
    }
}<file_sep><?php

//核心运行类

namespace core;

class Application
{
    public static $group;//模块
    public static $controller;//控制器
    public static $action;//方法

    //前端处理功能方法：路由选择和请求分发
    public static function run(){
        //1.路由信息的提取，
        //http://localhost/MyPHP/Exercise/exercise%2021%20MVC/index.php?g=home&c=home&a=index
        global $C;
        self::$group = isset($_GET['g'])?$_GET['g']:$C['default_route']['group'];
        self::$controller = isset($_GET['c'])?$_GET['c']:$C['default_route']['controller'];
        self::$action = isset($_GET['a'])?$_GET['a']:$C['default_route']['action'];
        //2.路由信息的大小写的处理
        self::$group = strtolower(self::$group);
        self::$controller = ucfirst(strtolower(self::$controller));
        self::$action = strtolower(self::$action);

        //3.初始化控制器
        // new \application\home\controllers\IndexController();
        //3.2.获取控制器名称
        $controllerName = self::getFullClassName();
        //3.3.实例化控制器
        $controllerInstance = new $controllerName();
        //4.调用方法
        //4.1.获得控制器方法
        $method = self::$action;
        //4.2.调用控制器方法
        $controllerInstance->$method();

    }

    //3.1.获取控制器的全限定名，字符串拼接
    public static function getFullClassName(){
        return "\\application\\".self::$group."\\controllers\\".self::$controller."Controller";
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 21:16
 */

namespace application\home\models;


use core\mybase\Model;

class TeacherModel extends Model
{
    //从数据库查询得到老师列表
    public function getteacher(){
        $sql = "select * from teacher";
        return $this->query($sql,null);
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/10
 * Time: 9:35
 */

namespace application\admin\models;

use core\mybase\Model;

class CustomerModel extends Model
{
    public function count($like=null){
        $sql=null;
        if($like!=null){
            $sql="SELECT COUNT(1) AS total FROM customer WHERE cname LIKE '%". $like . "%' ";
        }else{
            $sql="SELECT COUNT(1) AS total FROM customer ";
        }
        $result=$this->query($sql,null);
        return $result[0]['total'];
    }

    public function list($pageIndex=1,$pageSize=10,$like=null){
        $sql=null;
        if($like!=null){
            $sql="SELECT * FROM customer WHERE cname LIKE '%" . $like . "%' 
             OR cphone LIKE '%" . $like . "%'
             OR cemail LIKE '%" . $like . "%'
             ORDER BY cid DESC LIMIT ".($pageIndex-1)*$pageSize.",".$pageSize;
        }else{
            $sql="SELECT * FROM customer ORDER BY cid DESC LIMIT ".($pageIndex-1)*$pageSize.",".$pageSize;
        }
        return $this->query($sql,null);
    }

    public function del($args){
        $sql="DELETE FROM customer WHERE cid=?";
        return $this->execute($sql,$args);
    }

    public function add($args){
        $sql="INSERT INTO customer VALUE(DEFAULT,?,?,?,?,?,DEFAULT)";
        return $this->execute($sql,$args);
    }

    public function update($args){
        $sql="UPDATE customer SET cpassword=?,cphone=?,cemail=? WHERE cid=? ";
        return $this->execute($sql,$args);
    }

    public function ago($args){
        $sql="SELECT * FROM customer WHERE cid=?";
        return $this->query($sql,$args);
    }
}
<file_sep><?php


namespace application\home\controllers;

use core\mybase\Controller;
use application\home\models\GuestBookModel;
use core\MySession;


class IndexController extends Controller
{
    //打开首页方法
    public function index(){
        $this->display();
    }

    //打开联系我们方法
    public function contact(){
        $this->display();
    }

    //打开课程方法
    public function courses(){
        $this->display();
    }

    //打开定价方法
    public function pricing(){
        $this->display();
    }

    //打开讲师方法
    public function message(){
        $this->display();
    }
}<file_sep><?php


namespace application\admin\controllers;

use application\admin\models\CustomerModel;
use application\admin\models\MessageModel;
use application\admin\models\TeacherModel;
use core\mybase\AdminGroupController;
use core\MySession;
use application\admin\models\UserModel;
use application\admin\models\SubscribeModel;



class IndexController extends AdminGroupController
{
    //打开首页方法
    public function charts(){
        $this->display();
    }


    public function forms(){
        $this->display();
    }


    public function gbmgr(){
        $this->display();
    }


    public function main(){
        //用户总数
        $cm=new CustomerModel();
        $ctotal=$cm->count();
        $this->assign("ctotal",$ctotal);
        //留言总数
        $mm=new MessageModel();
        $mtotal=$mm->count();
        $this->assign("mtotal",$mtotal);
        //预约总数
        $sm=new SubscribeModel();
        $stotal=$sm->count();
        $this->assign("stotal",$stotal);
        //教师总数
        $tm=new TeacherModel();
        $ttotal=$tm->count();
        $this->assign("ttotal",$ttotal);
        $this->display();
    }


    public function widgets(){
        $this->display();
    }

    public function login(){
        $feedbook = ['errno' => 500,'mess' => '用户名不存在！'];
        $aname = isset($_POST['account'])?$_POST['account']:null;
        $apassword = isset($_POST['password'])?$_POST['password']:null;
        $sysremember=isset($_POST["sysremember"])?true:false;//null或on
        if ($aname != null && $apassword !=null){
            $args = [$aname,md5($apassword)];
            $um = new UserModel();
            $result = $um->getUserByNameAndPwd($args);
            if ($result != null){
                $feedbook = ['errno' => 200,'mess' => '登录成功！'];
                MySession::setSession("aname",$result["aname"]);
                MySession::setSession("sysremember",$sysremember);
                if ($sysremember){
                    MySession::extendSession();
                }
            }
        }
        echo json_encode($feedbook,JSON_UNESCAPED_UNICODE);
    }


}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9
 * Time: 21:50
 */

namespace application\home\controllers;



use application\home\models\SubscribeModel;
use core\mybase\HomeGroupController;
use core\MySession;

//预约表控制器
class subscribeController extends HomeGroupController
{
    //新增预约信息
    public function addsubscribe()
    {
        $feedback = ["errno" => 200, "mess" => "预约失败"];
        $myname = $_POST['myname'];
        $myphone = $_POST['myphone'];
        $techer = $_POST['teacher'];
        $mydate = $_POST['mydate'];
        $args = [$myname,$myphone,$techer,$mydate];
        $cm = new SubscribeModel();
        $result = $cm->subscribemess($args);
        if ($result > 0) {
            $feedback = ["errno" => 200, "mess" => "预约成功"];
        }
        echo json_encode($feedback, JSON_UNESCAPED_UNICODE);
    }

    //打开预约界面
    public function subscribe()
    {
        $this->display();
    }


    //分页预约数据
    public function list(){
        //从前端获取的信息
        $pageIndex = isset($_GET['pageIndex'])?$_GET['pageIndex']:1;
        $pageSize = isset($_GET['pageSize'])?$_GET['pageSize']:10;
        $like = isset($_GET['like'])?$_GET['like']:null;

        $gm = new SubscribeModel();
        $args = [MySession::getSession('cphone')];
        //获得符合条件的总记录数
        $total = $gm->count($like,$args);
        //获得分页数据
        //print_r($args);
        $list = $gm->list($pageIndex,$pageSize,$like,$args);
        //必须返回json格式的对象，手写的分页表单才能接收，构建数组。返回总的记录数，和行数
        $result = ['total'=>$total,'rows'=>$list];
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    //删除预约
    public function del(){
        //这里的值从前端脚本del函数获得
        $id = isset($_GET['sid'])?$_GET['sid']:-1;
        $gm = new SubscribeModel();
        //接收一个受影响的行数
        $result = $gm->dele([$id]);
        if ($result > 0){
            $this->success("删除<span style='font-size: 30px'>预约信息</span>成功，2秒后跳转回管理视图。","?g=home&c=user&a=main");
        }else{
            $this->fail("删除<span style='font-size: 30px'>预约信息</span>失败，2秒后跳转回管理视图。","?g=home&c=user&a=main");
        }
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9
 * Time: 9:38
 */

namespace application\admin\controllers;


use core\mybase\Model;
use core\MySession;

class AdminController extends Model
{
    public function delsession(){
        MySession::destorySession();
        header("location:index.php?g=admin&c=index&a=main");
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9
 * Time: 10:09
 */

namespace application\admin\models;


use core\mybase\Model;

class MessageModel extends Model
{
    public function count(){
        $sql="SELECT COUNT(1) AS total FROM message ";
        $result=$this->query($sql,null);
        return $result[0]['total'];
    }

    public function list($pageIndex=1,$pageSize=10){
        $sql="SELECT * FROM message ORDER BY mid DESC LIMIT ".($pageIndex-1)*$pageSize.",".$pageSize;
        return $this->query($sql,null);
    }

    public function del($args){
        $sql="DELETE FROM message WHERE mid=?";
        return $this->execute($sql,$args);
    }


}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 21:51
 */

namespace application\home\controllers;


use application\home\models\GuestBookModel;
use core\mybase\HomeGroupController;
use core\MySession;

class GuestController extends HomeGroupController
{
    //添加留言功能
    public function guestbook(){
        $feedback=["errno"=>500,"mess"=>"留言失败"];
        $args=[$_POST['mname'],$_POST['mphone'],$_POST['mcontent']];
        $um=new GuestBookModel();
        $result=$um->add($args);
        if($result>0){
            $feedback=["errno"=>200,"mess"=>"留言成功"];
        }
        echo json_encode($feedback,JSON_UNESCAPED_UNICODE);
    }


    //查询，展示分页留言功能
    public function showgeustbook(){
        //从前端获取的信息
        $pageIndex = isset($_GET['pageIndex'])?$_GET['pageIndex']:1;
        $pageSize = isset($_GET['pageSize'])?$_GET['pageSize']:10;
        $like = isset($_GET['like'])?$_GET['like']:null;

        $gm = new GuestBookModel();
        $args = [MySession::getSession('cphone')];
        //获得符合条件的总记录数
        $total = $gm->count($like,$args);
        //获得分页数据
        //print_r($args);
        $list = $gm->list($pageIndex,$pageSize,$like,$args);
        //必须返回json格式的对象，手写的分页表单才能接收，构建数组。返回总的记录数，和行数
        $result = ['total'=>$total,'rows'=>$list];
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    //删除留言
    public function del(){
        //这里的值从前端脚本del函数获得
        $id = isset($_GET['mid'])?$_GET['mid']:-1;
        $gm = new GuestBookModel();
        //接收一个受影响的行数
        $result = $gm->dele([$id]);
        if ($result > 0){
            $this->success("删除<span style='font-size: 30px'>留言信息</span>成功，2秒后跳转回管理视图。","?g=home&c=user&a=main");
        }else{
            $this->fail("删除<span style='font-size: 30px'>留言信息</span>失败，2秒后跳转回管理视图。","?g=home&c=user&a=main");
        }
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9
 * Time: 10:20
 */

namespace application\admin\controllers;


use application\admin\models\MessageModel;
use core\mybase\Controller;

class MessageController extends Controller
{
    public function list(){
        $pageIndex=isset($_GET['pageIndex'])?$_GET['pageIndex']:1;
        $pageSize=isset($_GET['pageSize'])?$_GET['pageSize']:10;
        $mm=new MessageModel();
        $total=$mm->count();
        $list=$mm->list($pageIndex,$pageSize);
        $result=['total'=>$total,"rows"=>$list];
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    public function del(){
        $mid=isset($_GET['mid'])?$_GET['mid']:-1;
        $mm=new MessageModel();
        $result=$mm->del([$mid]);
        header("location:index.php?g=admin&c=index&a=gbmgr");
    }
}<file_sep><?php


namespace application\home\models;


use core\mybase\Model;

class GuestBookModel extends Model
{
    //添加留言功能
    public function add($args){
        $sql="INSERT INTO message VALUE(DEFAULT,?,?,?,DEFAULT)";
        return $this->execute($sql,$args);
    }

    //查询有多少条留言，模糊查询建议不要用预编译，可能会查不出来
    public function count($like = null,$args)
    {
        $sql = null;
        //构建sql语句，假如说有模糊查询和没有模糊查询，进行一个判断
        if ($like != null) {
            $sql = "select count(1) as total from message where mname like '%" . $like . "%'";
        } else {
            $sql = "select count(1) as total from message where mphone=?";
        }
        //没有参数传进来，所以$args设为null
        $result = $this->query($sql, $args);
        return $result[0]['total'];
    }


    //分页查询留言信息
    public function list($pageIndex = 1, $pageSize = 10, $like = null,$args)
    {
        $sql = null;
        if ($like != null) {
            $sql = "select * from message where mname like '%" . $like . "%' order by mid desc limit " . ($pageIndex - 1) * $pageSize . "," . $pageSize;
        } else {
            $sql = "select * from message where mphone=? order by mid desc limit " . ($pageIndex - 1) * $pageSize . "," . $pageSize;
        }
        return $this->query($sql,$args);
    }

    //删除用户留言信息
    public function dele($args)
    {
        $sql = "delete from message where mid=?";
        //返回一个结果集，受影响的行数
        return $this->execute($sql, $args);
    }
}<file_sep><?php


namespace core\mybase;

use core\Application;

//控制器的基类
class Controller
{
    protected $view;//视图对象

    //子类对象如果没有定义构造，子类在实例化的时候会自动调用父类的构造
    public function __construct()
    {
        //调用初始化视图的方法
        $this->initView();
    }

    //初始化视图的方法，放到外面来，不放到构造里，为了以后扩展方便。不允许外部调用，只允许自己调用。
    private function initView(){
        //初始化视图对象
        $this->view=new View();
        //视图模板的路径：application/home/views/index.html
        //include_once $this->view_dir.$view_name.".html";
        $this->view->view_dir="application/".Application::$group."/views/".strtolower(Application::$controller)."/";
    }

    //调用视图对象打开视图方法
    public function display($viewname=null){
        $this->view->display($viewname);
    }

    //视图数据填充方法，保护类型，只有子类可以访问
    protected function assign($name,$value){
        $this->view->assign($name,$value);
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/10
 * Time: 9:35
 */

namespace application\admin\models;

use core\mybase\Model;

class TeacherModel extends Model
{
    public function count($like=null){
        $sql=null;
        if($like!=null){
            $sql="SELECT COUNT(1) AS total FROM teacher WHERE tname LIKE '%". $like . "%' ";
        }else{
            $sql="SELECT COUNT(1) AS total FROM teacher ";
        }
        $result=$this->query($sql,null);
        return $result[0]['total'];
    }

    public function list($pageIndex=1,$pageSize=10,$like=null){
        $sql=null;
        if($like!=null){
            $sql="SELECT * FROM teacher WHERE tname LIKE '%" . $like . "%' OR tage LIKE '%" . $like . "%' 
             OR tcourse LIKE '%" . $like . "%' ORDER BY tid DESC LIMIT ".($pageIndex-1)*$pageSize.",".$pageSize;
        }else{
            $sql="SELECT * FROM teacher ORDER BY tid DESC LIMIT ".($pageIndex-1)*$pageSize.",".$pageSize;
        }

        return $this->query($sql,null);
    }

    public function del($args){
        $sql="DELETE FROM teacher WHERE tid=?";
        return $this->execute($sql,$args);
    }

    public function add($args){
        $sql="INSERT INTO teacher VALUE(DEFAULT,?,?,?)";
        return $this->execute($sql,$args);
    }

    public function update($args){
        $sql="UPDATE teacher SET tage=?,tcourse=? WHERE tid=? ";
        return $this->execute($sql,$args);
    }

    public function ago($args){
        $sql="SELECT * FROM teacher WHERE tid=?";
        return $this->query($sql,$args);
    }

}
<file_sep><?php

namespace application\home\controllers;

use application\home\models\UserModel;
use core\mybase\HomeGroupController;
use core\MySession;


class UserController extends HomeGroupController
{
    //进入到个人中心页面
    public function main(){
        //填充数据
        $this->assign("cname",MySession::getSession("cname"));
        $this->assign("cemail",MySession::getSession("cemail"));
        $this->assign("cphone",MySession::getSession("cphone"));
        $this->assign("crgtime",MySession::getSession("crgtime"));

        $this->display();
    }

    //用户修改个人信息方法
    public function update(){
        $feedback = ["errno" => 500, "mess" => "修改失败！"];
        $newemail = $_POST['newemail'];
        $newphone = $_POST['newphone'];
        $newpwd = $_POST['newpwd'];
        $cid = MySession::getSession("cid");
        $args = [$newphone,$newemail,md5($newpwd),$cid];
        $um = new UserModel();
        $result = $um->upd($args);
        if ($result > 0) {
            $feedback = ["errno" => 200, "mess" => "修改成功"];
        }
        echo json_encode($feedback, JSON_UNESCAPED_UNICODE);

    }

    //用户注册方法
    public function register()
    {
        $feedback = ["errno" => 500, "mess" => "注册失败！"];
        $uname = $_POST['uname'];
        $upwd = $_POST['upwd'];
        $uemail = $_POST['uemail'];
        $uphone = $_POST['uphone'];
        $ukey = $_POST['ukey'];
        $args = [$uname, md5($upwd),$uphone,$uemail,$ukey];
        $um = new UserModel();
        $result = $um->add($args);
        if ($result > 0) {
            $feedback = ["errno" => 200, "mess" => "注册成功"];
        }
        echo json_encode($feedback, JSON_UNESCAPED_UNICODE);
    }

    /*注册用户名校验*/
    public function checkuser()
    {
        $uname = $_POST['uname'];
        $args = [$uname];
        $um = new UserModel();
        $result = $um->getUser($args);
        echo $result;
    }

    //用户登录方法
    public function login(){
        $feedbook = ['errno' => 500,'mess' => '用户名不存在！'];
        $uname = isset($_POST['account'])?$_POST['account']:null;
        $upwd = isset($_POST['password'])?$_POST['password']:null;
        $remember=isset($_POST["remember"])?true:false;//null或on
        if ($uname != null && $upwd !=null){
            $args = [$uname,md5($upwd)];
            $um = new UserModel();
            $result = $um->getUserByNameAndPwd($args);
            if ($result != null){
                $feedbook = ['errno' => 200,'mess' => '登录成功！'];
                //保持用户信息到session中
                MySession::setSession("cid",$result["cid"]);
                MySession::setSession("cname",$result["cname"]);
                MySession::setSession("cphone",$result["cphone"]);
                MySession::setSession("cemail",$result["cemail"]);
                MySession::setSession("crgtime",$result["crgtime"]);
                //记住我  按钮，当选择后，保存在session中
                MySession::setSession("remember",$remember);
                //判断：当选中复选框的值，为true时，延长1个小时会话保持时间（免登陆）
                if ($remember == true){
                   MySession::extendSession();
                }
            }
        }
        echo json_encode($feedbook,JSON_UNESCAPED_UNICODE);
    }

    //退出登录
    public function logout(){
        MySession::destorySession();
        header("location:index.php?g=home&c=index&a=index");
    }
}<file_sep><?php

namespace application\admin\models;

use core\mybase\Model;

class UserModel extends Model
{
    public function add($args)
    {
        $sql = "INSERT INTO customer VALUES(DEFAULT,?,?,?,?,?,default)";
        return $this->execute($sql, $args);
    }

    public function getUser($args)
    {
        $sql = "SELECT count(1) as result fROM customer WHERE  cname=?";
        $result = $this->query($sql, $args);
        return $result[0]['result'];
    }

    /**根据用户名密码获取用户信息*/
    public function getUserByNameAndPwd($args){
        $sql="SELECT * FROM admin  WHERE aname=? AND apassword=?";
        $result=$this->query($sql,$args);
        if(count($result)>0){
            return $result[0];
        }else{
            return null;
        }
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/11
 * Time: 21:17
 */

namespace application\home\controllers;


use application\home\models\TeacherModel;
use core\mybase\HomeGroupController;

class TeacherController extends HomeGroupController
{
    //获取老师列表
    public function teacherlist()
    {
        $tm = new TeacherModel();
        $result = $tm->getTeacher();
        echo json_encode($result, JSON_UNESCAPED_UNICODE);
    }
}<file_sep># Myproject
二阶段项目
<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9
 * Time: 21:47
 */

namespace application\home\models;


use core\mybase\Model;

class SubscribeModel extends Model
{
    //新增预约信息
    public function subscribemess($args)
    {
        $sql = "insert into subscribe value(default,?,?,?,?)";
        return $this->execute($sql,$args);
    }

    //查询有多少条预约，模糊查询建议不要用预编译，可能会查不出来
    public function count($like = null,$args)
    {
        $sql = null;
        //构建sql语句，假如说有模糊查询和没有模糊查询，进行一个判断
        if ($like != null) {
            $sql = "select count(1) as total from subscribe where sname like '%" . $like . "%'";
        } else {
            $sql = "select count(1) as total from subscribe where sphone=?";
        }
        //没有参数传进来，所以$args设为null
        $result = $this->query($sql, $args);
        return $result[0]['total'];
    }


    //分页查询预约信息
    public function list($pageIndex = 1, $pageSize = 10, $like = null,$args)
    {
        $sql = null;
        if ($like != null) {
            $sql = "select * from subscribe where sname like '%" . $like . "%' order by sid desc limit " . ($pageIndex - 1) * $pageSize . "," . $pageSize;
        } else {
            $sql = "select * from subscribe where sphone=? order by sid desc limit " . ($pageIndex - 1) * $pageSize . "," . $pageSize;
        }
        return $this->query($sql,$args);
    }

    //删除用户预约信息
    public function dele($args)
    {
        $sql = "delete from subscribe where sid=?";
        //返回一个结果集，受影响的行数
        return $this->execute($sql, $args);
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9
 * Time: 10:20
 */

namespace application\admin\controllers;


use application\admin\models\CustomerModel;
use application\admin\models\TeacherModel;
use core\mybase\Controller;

class CustomerController extends Controller
{
    public function list(){
        $pageIndex=isset($_GET['pageIndex'])?$_GET['pageIndex']:1;
        $pageSize=isset($_GET['pageSize'])?$_GET['pageSize']:10;
        $like=isset($_GET['like'])?$_GET['like']:null;
        $mm=new CustomerModel();
        $total=$mm->count($like);
        $list=$mm->list($pageIndex,$pageSize,$like);
        $result=['total'=>$total,"rows"=>$list];
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    public function del(){
        $mid=isset($_GET['cid'])?$_GET['cid']:-1;
        $mm=new CustomerModel();
        $result=$mm->del([$mid]);
        header("location:index.php?g=admin&c=index&a=forms");
    }

    public function add(){
        $feedback = ["errno" => 500, "mess" => "新增失败！"];
        $cname=isset($_POST['cname'])?$_POST['cname']:null;
        $cpassword=isset($_POST['cpassword'])?$_POST['cpassword']:null;
        $cphone=isset($_POST['cphone'])?$_POST['cphone']:null;
        $cemail=isset($_POST['cemail'])?$_POST['cemail']:null;
        $ckey=isset($_POST['ckey'])?$_POST['ckey']:null;
        if($cname==null || $cpassword==null || $cphone==null || $cemail==null || $ckey==null){
            return;
        }
        $args=[$cname,md5($cpassword),$cphone,$cemail,$ckey];
        $mm=new CustomerModel();
        $result=$mm->add($args);
        if($result>0){
            $feedback = ["errno" => 200, "mess" => "新增成功！"];
        }
        echo json_encode($feedback,JSON_UNESCAPED_UNICODE);
    }

    public function update(){
        $feedback = ["errno" => 500, "mess" => "修改失败！"];
        $cpassword=isset($_POST['cpassword'])?$_POST['cpassword']:null;
        $cphone=isset($_POST['cphone'])?$_POST['cphone']:null;
        $cemail=isset($_POST['cemail'])?$_POST['cemail']:null;
        $cid=$_POST['cid'];
        if( $cpassword==null || $cphone==null || $cemail==null){
            return;
        }
        $args=[md5($cpassword),$cphone,$cemail,$cid];
        $mm=new CustomerModel();
        $result=$mm->update($args);
        if($result>0){
            $feedback = ["errno" => 200, "mess" => "修改成功！"];
        }
        echo json_encode($feedback,JSON_UNESCAPED_UNICODE);
    }

    public function ago(){
        $cid=$_POST['cid'];
        $args=[$cid];
        $mm=new CustomerModel();
        $result=$mm->ago($args);
        echo json_encode($result,JSON_UNESCAPED_UNICODE);

    }
}<file_sep><?php

//http://localhost/MyPHP/Exercise/exercise%2021%20MVC/ECI-C/index.php
//定义服务器默认URL
//http://localhost/MyPHP/Exercise/exercise21MVC/ECI-C/index.php
define("SERVER_URL","http://".$_SERVER['SERVER_NAME']."/MyPHP/Exercise/exercise 21 MVC/ECI-C/");

//前台静态文件常量
//<link href="public/home/css/bootstrap.css" type="text/css" rel="stylesheet" media="all">
define("__HOME__","public/home/");
define("__HOME_CSS__",SERVER_URL.__HOME__."css/");
define("__HOME_IMAGES__",SERVER_URL.__HOME__."images/");
define("__HOME_JS__",SERVER_URL.__HOME__."js/");

//定义前台公共代码文件的地址常量
//application/home/views/common
define("__HOME_COMMON__","application/home/views/common/");

//定义前台登录功能静态资源常量
define("__LOGIN__","public/login/");
define("__LOGIN_CSS__",SERVER_URL.__LOGIN__."css/");
define("__LOGIN_IMAGES__",SERVER_URL.__LOGIN__."images/");
define("__LOGIN_JS__",SERVER_URL.__LOGIN__."js/");


//后台静态文件常量
//<link href="public/home/css/bootstrap.css" type="text/css" rel="stylesheet" media="all">
define("__ADMIN__","public/admin/");
define("__ADMIN_CSS__",SERVER_URL.__ADMIN__."css/");
define("__ADMIN_JS__",SERVER_URL.__ADMIN__."js/");


//定义后台公共代码文件的地址常量
//application/admin/views/common
define("__ADMIN_COMMON__","application/admin/views/common/");

<file_sep><?php

namespace application\home\models;

use core\mybase\Model;

class UserModel extends Model
{
    //注册用户方法
    public function add($args)
    {
        $sql = "INSERT INTO customer VALUES(DEFAULT,?,?,?,?,?,default)";
        return $this->execute($sql, $args);
    }

    /*注册用户名校验是否已存在*/
    public function getUser($args)
    {
        $sql = "SELECT count(1) as result fROM customer WHERE  cname=?";
        $result = $this->query($sql, $args);
        return $result[0]['result'];
    }

    /**根据用户名密码获取用户信息，用户登录*/
    public function getUserByNameAndPwd($args){
        $sql="SELECT * FROM customer  WHERE cname=? AND cpassword=?";
        $result=$this->query($sql,$args);
        if(count($result)>0){
            return $result[0];
        }else{
            return null;
        }
    }

    //修改用户信息
    public function upd($args){
        $sql = "update customer set cphone=? , cemail=? , cpassword=?  where cid=?";
        return $this->execute($sql,$args);
    }
}<file_sep><?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/9
 * Time: 10:20
 */

namespace application\admin\controllers;


use application\admin\models\TeacherModel;
use core\mybase\Controller;

class TeacherController extends Controller
{
    public function list(){
        $pageIndex=isset($_GET['pageIndex'])?$_GET['pageIndex']:1;
        $pageSize=isset($_GET['pageSize'])?$_GET['pageSize']:10;
        $like=isset($_GET['like'])?$_GET['like']:null;
        $mm=new TeacherModel();
        $total=$mm->count($like);
        $list=$mm->list($pageIndex,$pageSize,$like);
        $result=['total'=>$total,"rows"=>$list];
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
    }

    public function del(){
        $mid=isset($_GET['tid'])?$_GET['tid']:-1;
        $mm=new TeacherModel();
        $result=$mm->del([$mid]);
        header("location:index.php?g=admin&c=index&a=widgets");
    }

    public function add(){
        $feedback = ["errno" => 500, "mess" => "新增失败！"];
        $tname=isset($_POST['tname'])?$_POST['tname']:null;
        $tage=isset($_POST['tage'])?$_POST['tage']:null;
        $tcourse=isset($_POST['tcourse'])?$_POST['tcourse']:null;
        if($tname==null || $tage==null || $tcourse==null){
            return;
        }
        $args=[$tname,$tage,$tcourse];
        $mm=new TeacherModel();
        $result=$mm->add($args);
        if($result>0){
            $feedback = ["errno" => 200, "mess" => "新增成功！"];
        }
        echo json_encode($feedback,JSON_UNESCAPED_UNICODE);
    }

    public function update(){
        $feedback = ["errno" => 500, "mess" => "修改失败！"];
        $tage=isset($_POST['tage'])?$_POST['tage']:null;
        $tcourse=isset($_POST['tcourse'])?$_POST['tcourse']:null;
        $tid=$_POST['tid'];
        if( $tage==null || $tcourse==null){
            return;
        }
        $args=[$tage,$tcourse,$tid];
        $mm=new TeacherModel();
        $result=$mm->update($args);
        if($result>0){
            $feedback = ["errno" => 200, "mess" => "修改成功！"];
        }
        echo json_encode($feedback,JSON_UNESCAPED_UNICODE);
    }

    public function ago(){
        $tid=$_POST['tid'];
        $args=[$tid];
        $mm=new TeacherModel();
        $result=$mm->ago($args);
        echo json_encode($result,JSON_UNESCAPED_UNICODE);

    }
}