從零開始打造自己的PHP框架――第2章

目標

本篇,我們來實現(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 ,即可看到渲染出的頁面。

頁面渲染進階

直接在頁面echo,難以體現(xiàn)水平,我們來安裝一個模板引擎――smarty。

命名空間

接下來smarty的使用,牽涉到命名空間這個知識點,在此學習一下。

首先聲明:命名空間和文件路徑?jīng)]有關系,沒有關系,沒有關系!雖然,在使用命名空間時經(jīng)常參考文件路徑,但是,它們沒有必然關系。

命名空間的作用:解決重名問題。不同的命名空間中,可以存在相同類名和函數(shù)名。我們在使用一個類和函數(shù)時,必須明確指出使用的是哪一個命名空間中的類和函數(shù)。

上文我們說到,在文件系統(tǒng)中訪問一個文件有三種方式,PHP命名空間中的元素使用同樣的原理。例如,類名可以通過三種方式引用:

1、非限定名稱,或不包含前綴的類名稱,例如 $a=new foo();foo::staticmethod(); 。如果當前命名空間是 currentnamespace,foo 將被解析為 \currentnamespace\foo ;如果當前沒有指定命名空間,則foo會被解析為 \foo。
2、限定名稱,或包含前綴的名稱,例如 $a = new subnamespace\foo();subnamespace\foo::staticmethod(); 。如果當前的命名空間是 currentnamespace,則 foo 會被解析為 \currentnamespace\subnamespace\foo ;如果當前沒有指定命名空間,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

書簽

從零開始打造自己的PHP框架

使用命名空間:基礎

使用命名空間:后備全局函數(shù)/常量

smarty基礎安裝

smarty進階安裝

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容