目標(biāo)
本篇明也,我們來實現(xiàn)加載控制器铭段、數(shù)據(jù)查詢和頁面渲染。
加載控制器
控制器
在app目錄下胧华,新建ctrl目錄寄症,ctrl目錄下新建indexCtrl.php文件宙彪,內(nèi)容如下:
<?php
namespace app\ctrl;
class indexCtrl{
public function index(){
echo 'index ctrl';
}
}
調(diào)用控制器
在根目錄下的index.php文件中,繼續(xù)添加:
include CORE.'/autoload.php';
spl_autoload_register('\core\autoload::load');
$route = new \core\route();
$ctrl = $route->ctrl;
$action = $route->action;
$params = $route->params;
$ctrl_file = APP.'/ctrl/'.$ctrl.'Ctrl.php';
$ctrl_class = '\\app\\ctrl\\'.$ctrl.'Ctrl';
if(is_file($ctrl_file)){
include $ctrl_file;
$ctrl_obj = new $ctrl_class;
$ctrl_obj->$action();
}else {
throw new \Exception('找不到控制器'.$ctrl_file);
}
訪問地址 http://vkphp.dev 有巧,即可看到“index ctrl”释漆。
數(shù)據(jù)查詢
1、在mysql中篮迎,新建數(shù)據(jù)庫vkphp男图。
2、在vkphp數(shù)據(jù)庫中甜橱,新建表vk_user逊笆,字段包括id、username和password岂傲。
3难裆、在common文件夾下,新建db.php譬胎,內(nèi)容如下:
<?php
namespace core\common;
class db extends \PDO{
public function __construct(){
$dsn = 'mysql:host=localhost;dbname=vkphp';
$username = 'root';
$passwd = '';
try{
parent::__construct($dsn,$username,$passwd);
// echo 'database connect success';
}catch (\Exception $e){
echo $e->getMessage();
}
}
}
4差牛、在indexCtrl.php中,添加:
public function data(){
$db = new \core\common\db();
$sql = 'select * from vk_user';
$result = $db->query($sql);
p($result);
p($result->fetchAll());
}
訪問地址 http://vkphp.dev/index/data 堰乔,即可看到從數(shù)據(jù)庫中查詢出的數(shù)據(jù)。
頁面渲染
頁面渲染脐恩,主要有兩部分工作:賦值和顯示镐侯。我們需要實現(xiàn)兩個函數(shù):assign和display。
1驶冒、在app目錄下新建view目錄苟翻,view目錄下新建index目錄,index目錄中新建render.html骗污,內(nèi)容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Render</title>
</head>
<body>
<p>第一個視圖</p>
<p>用戶名:<?php echo $username; ?></p>
</body>
</html>
2崇猫、在core目錄中,添加render.php需忿,內(nèi)容如下:
<?php
namespace core;
class render{
public $params = array();
public function assign($name,$value){
$this->params[$name] = $value;
}
public function display($file){
$file = APP.'/view/'.$file;
if(is_file($file)){
extract($this->params); //把數(shù)組變成變量
include $file;
}
}
}
3诅炉、修改indexCtrl.php如下:
<?php
namespace app\ctrl;
class indexCtrl extends \core\render{
// 其他
public function render(){
$this->assign('username','voidking');
$this->display('index/render.html');
}
}
訪問地址 http://vkphp.dev/index/render ,即可看到渲染出的頁面屋厘。
頁面渲染進(jìn)階
直接在頁面echo涕烧,難以體現(xiàn)水平,我們來安裝一個模板引擎――smarty汗洒。
命名空間
接下來smarty的使用议纯,牽涉到命名空間這個知識點,在此學(xué)習(xí)一下溢谤。
首先聲明:命名空間和文件路徑?jīng)]有關(guān)系瞻凤,沒有關(guān)系憨攒,沒有關(guān)系!雖然阀参,在使用命名空間時經(jīng)常參考文件路徑浓恶,但是,它們沒有必然關(guān)系结笨。
命名空間的作用:解決重名問題包晰。不同的命名空間中,可以存在相同類名和函數(shù)名炕吸。我們在使用一個類和函數(shù)時伐憾,必須明確指出使用的是哪一個命名空間中的類和函數(shù)。
上文我們說到赫模,在文件系統(tǒng)中訪問一個文件有三種方式树肃,PHP命名空間中的元素使用同樣的原理。例如瀑罗,類名可以通過三種方式引用:
1胸嘴、非限定名稱,或不包含前綴的類名稱斩祭,例如 $a=new foo();
或 foo::staticmethod();
劣像。如果當(dāng)前命名空間是 currentnamespace,foo 將被解析為 \currentnamespace\foo
摧玫;如果當(dāng)前沒有指定命名空間耳奕,則foo會被解析為 \foo
。
2诬像、限定名稱屋群,或包含前綴的名稱,例如 $a = new subnamespace\foo();
或 subnamespace\foo::staticmethod();
坏挠。如果當(dāng)前的命名空間是 currentnamespace芍躏,則 foo 會被解析為 \currentnamespace\subnamespace\foo
;如果當(dāng)前沒有指定命名空間降狠,foo 會被解析為\subnamespace\foo
对竣。
3、完全限定名稱喊熟,或包含了全局前綴操作符的名稱柏肪,例如,$a = new \currentnamespace\foo();
或 \currentnamespace\foo::staticmethod();
芥牌。在這種情況下烦味,foo 總是被解析為代碼中的文字名(literal name) \currentnamespace\foo
。
下面舉個栗子:
<?php
namespace A\B\C;
class Exception extends \Exception {}
$a = new Exception('hi'); // $a 是類 A\B\C\Exception 的一個對象
$b = new \Exception('hi'); // $b 是類 Exception 的一個對象
$c = new ArrayObject; // 致命錯誤, 找不到 A\B\C\ArrayObject 類
?>
下載安裝smarty
1、訪問smarty官方下載 谬俄,下載smarty柏靶,小編下載的是3.1.30版本。
2溃论、在根目錄下新建lib屎蜓,解壓smarty到lib目錄下,重命名文件夾為smarty钥勋。
使用smarty
1炬转、在app目錄下新建smarty目錄,smarty目錄下新建templates算灸、template_c扼劈、configs、cache四個目錄菲驴。
2荐吵、在templates目錄下新建index目錄,index目錄中新建render2.html赊瞬,內(nèi)容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Smarty</title>
</head>
<body>
<p>第一個Smarty頁面</p>
<p>用戶名:{{$username}}</p>
</body>
</html>
3先煎、修改core目錄下的render.php如下:
<?php
namespace core;
class render{
public $smarty;
public function __construct(){
require_once(LIB.'/smarty/libs/Smarty.class.php');
$this->smarty = new \Smarty();
$this->smarty->setTemplateDir(APP.'/smarty/templates/');
$this->smarty->setCompileDir(APP.'/smarty/templates_c/');
$this->smarty->setConfigDir(APP.'/smarty/configs/');
$this->smarty->setCacheDir(APP.'/smarty/cache/');
}
public $params = array();
public function assign($name,$value){
$this->params[$name] = $value;
}
public function display($file){
$file = APP.'/view/'.$file;
if(is_file($file)){
extract($this->params); //把數(shù)組變成變量
include $file;
}
}
}
4、修改indexCtrl.php如下:
<?php
namespace app\ctrl;
include CORE.'/render.php';
class indexCtrl extends \render{
// 其他
public function render2(){
$this->smarty->assign('username','voidking');
$this->smarty->display('index/render2.html');
}
}
訪問地址 http://vkphp.dev/index/render2 巧涧,即可看到渲染出的頁面薯蝎。
源碼分享
https://github.com/voidking/vkphp/releases/tag/v1.2.0