本篇,咱们来实现加载控制器、数据查询和页面渲染。php
原文地址:http://www.voidking.com/2017/...html
在app目录下,新建ctrl目录,ctrl目录下新建indexCtrl.php文件,内容以下:mysql
<?php namespace app\ctrl; class indexCtrl{ public function index(){ echo 'index ctrl'; } }
在根目录下的index.php文件中,继续添加:git
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”。github
一、在mysql中,新建数据库vkphp。sql
二、在vkphp数据库中,新建表vk_user,字段包括id、username和password。数据库
三、在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(); } } }
四、在indexCtrl.php中,添加:app
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。
一、在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>
二、在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; } } }
三、修改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命名空间中的元素使用一样的原理。例如,类名能够经过三种方式引用:
一、非限定名称,或不包含前缀的类名称,例如 $a=new foo();
或 foo::staticmethod();
。若是当前命名空间是 currentnamespace,foo 将被解析为 \currentnamespace\foo
;若是当前没有指定命名空间,则foo会被解析为 \foo
。
二、限定名称,或包含前缀的名称,例如 $a = new subnamespace\foo();
或 subnamespace\foo::staticmethod();
。若是当前的命名空间是 currentnamespace,则 foo 会被解析为 \currentnamespace\subnamespace\foo
;若是当前没有指定命名空间,foo 会被解析为\subnamespace\foo
。
三、彻底限定名称,或包含了全局前缀操做符的名称,例如,$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官方下载 ,下载smarty,小编下载的是3.1.30版本。
二、在根目录下新建lib,解压smarty到lib目录下,重命名文件夹为smarty。
一、在app目录下新建smarty目录,smarty目录下新建templates、template_c、configs、cache四个目录。
二、在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>
三、修改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; } } }
四、修改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/v...