View的職責范圍:
view 干的事, 不多也不少, 就是通過view加載視圖, 同時分配變量到視圖, 所以, 他所做的工作基本包括:
- 加載視圖, 所以需要有個加載視圖的方法, 我們定義為
make()
- 分配變量, 我們需要有個方法可以承載變量, 定義為
with()
- 我們要讓他能夠以json的格式作為api返回
示例
還是 TestController.php 的 test2 方法
<?php
namespace App\Controller;
use Fizzday\FizzDB\DB;
use Fizzday\FizzView\FizzView as View;
class TestControlelr extends BaseController;
{
public function test()
{
$users = DB::table('users')->where('id', 1)->first();
print_r($users);
}
public function test2()
{
View::make('test.tes2')
->with('name', 'xiaoming')
->withAge(22);
}
}
模板中使用變量
<span>名字: <?=$name;?> </span>
<span>年齡: <?=$age;?> </span>
說明: 這里使用了魔術方法解析 withAge 為 age. 同時指定模板的時候,可以用點語法(test.test2),或者(test/test2),當然可以不指定,自動模板會找對應的目錄下的文件
完善類
綜合這些, 我們來完善一個簡單的View類:
<?php
namespace Fizzday\FizzView;
// 自動分配模板的時候, 需要引入路由
use Fizzday\FizzRoute\Route;
class View
{
// 模板文件路徑
public static $viewPath = [];
// 分配的變量數組
public static $data = [];
// 是否返回解析后的文本(發郵件等情況下會使用到), 默認false
public static $return = false;
/**
* 分配模板
* @param sting $viewName 模板名字, 如: admin.index或者admin/index
* @param boolean $type 是否返回為文本
* @return mixed 輸出或返回文本
*/
public static function make($viewName = null, $type = false)
{
if (!defined('VIEW_PATH')) die("未定義 VIEW_PATH 常量");
if (!$viewName) { // 自動分配模板
if (!class_exists("Route")) die('沒有保存 autoView 數據對應的類 Route ');
// 檢查是否存在 $autoView 變量
if (!array_key_exists('autoView', get_class_vars("Route"))) die('獲取不到保存自動模板的變量, 檢查 Route::$autoView 是否存在');
$autoView = Route::$autoView;
$viewName = str_replace('\\', '.', str_replace('controller', '', strtolower($autoView['class']))) . '.' . $autoView['function'];
}
$viewFilePath = VIEW_PATH . str_replace('.', '/', $viewName) . '.php';
if (is_file($viewFilePath)) static::$viewPath[] = $viewFilePath;
else die("View file does not exist!");
if ($type) static::$return = true;
return new static;
}
/**
* 分配變量
* @param sting $key 變量key
* @param string $value 變量值
* @return obj 返回鏈式操作對象
*/
public static function with($key, $value = null)
{
if (is_array($key) && !empty($key)) {
foreach ($key as $k => $v) {
static::$data[$k] = $v;
}
} else static::$data[$key] = $value;
return new static;
}
/**
* 捕獲未定義的方法
* @param sting $method 變量key和with組合
* @param mixed $parameters 變量值
* @return obj [description]
*/
public function __call($method, $parameters)
{
if (start_with($method, 'with')) static::with(lcfirst(substr_replace($method, '', 0, 4)), $parameters[0]);
else die("Function [$method] does not exist!");
return new static;
}
/**
* 渲染變量到模板
* @return mixed 最終頁面
*/
public static function run()
{
// 獲取模板文件
$viewPath = static::$viewPath;
$data = static::$data;
$return = static::$return;
if ($viewPath) {
// 分配變量
extract($data); // 抽取數組中的變量
if (ob_get_contents()) ob_end_clean(); //關閉頂層的輸出緩沖區內容
ob_start(); // 開始一個新的緩沖區
foreach ($viewPath as $v) {
require $v; //加載解析后的文件
}
$content = ob_get_contents();// 獲得緩沖區的內容
if (ob_get_contents()) ob_end_clean(); // 關閉緩沖區
ob_start(); //開始新的緩沖區,給后面的程序用
// 重置變量
static::reset();
// 處理返回
if ($return) return $content; // 返回文本。
else echo $content;
}
}
/**
* 重置模板輸出信息
* @return [type] [description]
*/
private static function reset()
{
static::$viewPath = [];
static::$data = [];
static::$return = false;
}
}
這里為了使用方便, 加入了自動模板功能, 也就是使用 make()
方法未指定模板時, 會自動到控制器名字對應的目錄下自動去尋找方法名對應的文件. 同時, 在FizzRoute.php
中添加了自動模板的支持
到這里, 一個模板功能就加入了框架中, 但讓, 這里為了簡單, 沒有做模板簡化定義解析, 用的php原生模板語法, 后邊會一點點探究
配置
同樣的, 為了更加簡單, 以及做好分離, 我們加入可配置選項, 通過一個配置控制是否啟用模板, 減少不必要的模板初始化加載消耗
- 更改
~/fizzday/bootstrap/boot.php
文件, 前邊講過了, 這是統一驅動文件, 所以, 我們把 view 的驅動也放到這里, 方便統一管理
- 更改
<?php
// 項目的源碼根目錄
define('BASE_PATH', __DIR__ . '/../');
// 配置目錄
define('CONF_PATH', BASE_PATH . 'config/');
// 請求composer入口文件
require BASE_PATH . 'vendor/autoload.php';
// 路由目錄
define('ROUTE_PATH', BASE_PATH.config('config.path.route').'/');
// 緩存目錄
define('CACHE_PATH', BASE_PATH.config('config.path.cache').'/');
// 取別名, 這樣就不需要在 routes/routes.php 中 use FizzRoute 了
class_alias('\\Fizzday\\FizzRoute\\Route', 'Route');
// 引入路由
require ROUTE_PATH . 'route.php';
// 驅動路由
Route::dispatch();
// 模板啟用
if (config('config.switch.view') == 'on') {
// 模板目錄
define('VIEW_PATH', BASE_PATH.config('config.path.view').'/');
class_alias('\\Fizzday\\FizzView\\View', 'View');
// 驅動 view
View::run();
}
我們可以看到, 尾部加入了 view 的相關驅動, 同時在公共配置文件~/fizzday/config/config.php
配置view的開關
到此, 一個包含mvc結構的框架就完全完成了, 可以做基本的使用了, 同時可以高度自由的DIY
, 如果只是學習交流, 看到這里就可以了.
后續的文章會涉及到高級應用和優化, 比如: webapi常用的JWT認證來滿足異步通信, 緩存路由,配置,數據庫信息等提高執行效率, 增加高可用性, 配置集群機器支持多slave
數據庫等等等等......