基于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错误提示插件

<?php
header('Content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
require_once(__DIR__.'/configs/Website.php');
require_once(__DIR__.'/frame/Common/Function.php');
require_once(__DIR__.'/frame/App.php');
require_once(__DIR__.'/vendor/autoload.php');
if(DEBUG){
    $whoops = new \Whoops\Run;
    $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

<?php
return array(
    'database_type' => 'mysql',
    'database_name' => 'redis',
    'server' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
    'port' => 3306,
    'prefix' => 'redis_',
);

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

<?php
namespace frame\Libs;
use frame\Libs\Config;
use Medoo\medoo;

class Model extends medoo
{
    public function __construct()
    {
        $databaseConfigs = Config::get('Database',NULL,TRUE);
        parent::__construct($database);
    }
}

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

<?php
namespace app\Models;
use frame\Libs\Model;
class Users extends Model
{
    public $table = 'users';
    
    //获取用户列表
    public function lists()
    {
        return $this->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

<?php
namespace app\Controllers;
class Controller
{
    public function view(string $file,array $data = [] )
    {
        $views = APP.'/Views/'.$file.'.html';
        if(is_file($views)) {
            $twigConfig = CACHE ? array( 'cache' => 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

<?php
namespace app\Controllers\Home;
use app\Controllers\Controller;
use app\Models\Users;

class IndexController extends Controller
{
    public function __construct()
    {
        $this->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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>twig模版引擎</title>
</head>
<body>
    {% for item in users %}
        用户名:{{item.user}} <br>
        密码: {{item.pass}} <br>
    {% endfor %}
</body>
</html>

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

    原文作者:骆驼祥子
    原文地址: https://segmentfault.com/a/1190000014413273
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞