PHPPHP经验分享

从零开始打造自己的PHP框架――第2章

2017-09-13  本文已影响18人  VoidKing

目标

本篇,我们来实现加载控制器、数据查询和页面渲染。

原文地址:http://www.voidking.com/2017/08/30/deve-vkphp-2/

加载控制器

控制器

在app目录下,新建ctrl目录,ctrl目录下新建indexCtrl.php文件,内容如下:

<?php
namespace app\ctrl;

class indexCtrl{
    public function index(){
        echo 'index ctrl';
    }
}

调用控制器

在根目录下的index.php文件中,继续添加:

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”。

数据查询

1、在mysql中,新建数据库vkphp。

2、在vkphp数据库中,新建表vk_user,字段包括id、username和password。

3、在common文件夹下,新建db.php,内容如下:

<?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 ,即可看到从数据库中查询出的数据。

页面渲染

页面渲染,主要有两部分工作:赋值和显示。我们需要实现两个函数:assign和display。

1、在app目录下新建view目录,view目录下新建index目录,index目录中新建render.html,内容如下:

<!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,内容如下:

<?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); //把数组变成变量
            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,难以体现水平,我们来安装一个模板引擎――smarty。

命名空间

接下来smarty的使用,牵涉到命名空间这个知识点,在此学习一下。

首先声明:命名空间和文件路径没有关系,没有关系,没有关系!虽然,在使用命名空间时经常参考文件路径,但是,它们没有必然关系。

命名空间的作用:解决重名问题。不同的命名空间中,可以存在相同类名和函数名。我们在使用一个类和函数时,必须明确指出使用的是哪一个命名空间中的类和函数。

上文我们说到,在文件系统中访问一个文件有三种方式,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,内容如下:

<!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); //把数组变成变量
            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框架

使用命名空间:基础

使用命名空间:后备全局函数/常量

smarty基础安装

smarty进阶安装

上一篇下一篇

猜你喜欢

热点阅读