基于PSR-0编码规范开发一套PHP-MVC框架(二)

一、composer依赖包管理工具。composer.json

{
    "name":"PHP-FRAME",
    "author":"Guoming.Zhang",
    "type":"FRAME",
    "version":"v1.0.0",
    "date":"2018-4-16 16:18:00",
    "keywords":["php","frame","PHP框架","MVC"],
    "description":"采用PSR-0编码规范开发的一套PHP框架,纯面向对象开发,composer依赖包管理、twig模版引擎、Medoo数据库类、Whoops错误输出等......",
    "require":{
        "twig/twig":"*",
        "catfan/medoo":"*",
        "filp/whoops":"*"
    }
}

在项目根目录下使用命令执行 composer install 安装twig、medoo、whoops插件

二、入口文件。index.php 引入vendor/autoload.php文件,开启whoops错误提示插件

pushHandler(new \Whoops\Handler\PrettyPageHandler);
    $whoops->register();
    ini_set('display_error','On');
} else {
    ini_set('display_error','Off');
}
spl_autoload_register('frame\App::autoload');
frame\App::run();
?>

三、数据库配置文件。configs/Database.php

 'mysql',
    'database_name' => 'redis',
    'server' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
    'port' => 3306,
    'prefix' => 'redis_',
);

四、数据库操作类。frame/Libs/Model.php

五、模型层操作数据库。app/Models/Users.php

select($this->table,'*');
    }
    //增加用户
    public function addOne(array $data)
    {
        return $this->insert($this->table,$data);
    }
    //删除用户
    public function delOne(array $where)
    {
        return $this->delete($this->table,$where);
    }
    //修改用户
    public function editOne(array $data, array $where)
    {
        return $this->update($this->table,$data,$where);
    }
}

六、视图层基类。app/Controllers/Controller.php

 BASEPATH.'/storage/cache/' ) : [];
            $loader = new \Twig_Loader_Filesystem(APP.'/Views/');
            $twig = new \Twig_Environment($loader, $twigConfig);
            $template = $twig->loadTemplate($file.'.html');
            $template->display($data);
        } else {
            throw new \Exception($file."视图模版不存在", 1);
        }
    }
}

七、控制器操作数据并显示到页面。app/Controllers/Home/IndexController.php

model = new Users;
    }
    public function index()
    {
        //获取所有用户
        $users = $this->model->lists();
        //删除用户
        // $del = $this->model->delOne(['id'=>1]);
        //修改用户
        // $update = $this->model->editOne(['user'=>'张三'],['id'=>2]);
        
        //视图模版渲染
        $this->view('Home/index',['users'=>$users]);
    }
}

八、视图文件。app/Views/Home/index.html




    
    twig模版引擎


    {% for item in users %}
        用户名:{{item.user}} 
密码: {{item.pass}}
{% endfor %}

medoo数据库操作类官方文档:https://medoo.lvtao.net/doc.php
twig模版引擎官方文档:https://www.kancloud.cn/yunye...
github地址:https://github.com/305515319/...

你可能感兴趣的:(php,psr-0,composer,medoo,mvc)