基於PSR-0編碼規範開發一套PHP-MVC框架(二)

1、composer依賴包管理工具。composer.jsonphp

{
    "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插件html

2、入口文件。index.php 引入vendor/autoload.php文件,開啓whoops錯誤提示插件mysql

<?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();
?>

3、數據庫配置文件。configs/Database.phpgit

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

4、數據庫操做類。frame/Libs/Model.phpgithub

<?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);
    }
}

5、模型層操做數據庫。app/Models/Users.phpredis

<?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);
    }
}

6、視圖層基類。app/Controllers/Controller.phpsql

<?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);
        }
    }
}

7、控制器操做數據並顯示到頁面。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]);
    }
}

8、視圖文件。app/Views/Home/index.htmljson

<!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/...app

相關文章
相關標籤/搜索