1、实现效果。引入smarty, 实现assign和 display
2、下载smarty,创建缓存目录cache和扩展extend
点击下面查看具体下载使用,下载改名后放到extend
PHP之Smarty使用以及框架display和assign原理_PHP隔壁老王邻居的博客-CSDN博客
3、当前控制器方法和缓存目录cache、扩展extend 定义,KJ.php
//运行控制器中方法public static function _run(){$c=strtolower(isset($_GET['c'])?$_GET['c']:'index');$a=strtolower(isset($_GET['a'])?$_GET['a']:'index');define('CRL',$c); //当前控制器define('ACTION',$a);//当前方法$c.='Crl';if(!class_exists($c)){die("控制器".$c."不存在");}$obj=new $c();if(!method_exists($obj,$a)){die("控制器".$c."下".$a."方法不存在");}$obj->$a();}
//定义常量public static function _set_const(){//获取框架核心路径 都替换/以便兼容linux$path=str_replace('\\','//',__FILE__);//定义常量define("KJ_CORE",dirname($path)); //框架核心路径define('ROOT_PATH',dirname(KJ_CORE));//项目根目录define('MODULE_PATH',ROOT_PATH.'/'.MODULE);//模块define('CONTROLLER',MODULE_PATH.'/controller');//定义控制器define('MODEL',MODULE_PATH.'/model');//定义模型define('VIEW',MODULE_PATH.'/view');//定义显示define('EXTEND',KJ_CORE.'/extend');//定义扩建路径define('CACHE',ROOT_PATH.'/cache');//定义缓存路径//模板编译目录define('APP_COMPILE_PATH',CACHE.'/view/'.MODULE.'/Compile');//模板缓存define('APP_CACHE_PATH',CACHE.'/view/'.MODULE.'/Cache');}
4、创建SmartyBase.php,引入smarty和封装
<?php
include_once EXTEND.'/smarty/Smarty.class.php';
class SmartyBase{private static $smarty;public function __construct(){if(!is_null(self::$smarty)) return;$smarty=new Smarty();self::$smarty=$smarty;$smarty->template_dir=VIEW.'/'.CRL.'/';$smarty->compile_dir=APP_COMPILE_PATH;$smarty->cache_dir=APP_CACHE_PATH;$smarty->left_delimiter='{';//模板标签左$smarty->right_delimiter='}';//模板标签右$smarty->caching=true; //缓存开启$smarty->cache_lifetime=60;//缓存时间}//显示模板protected function display($tpl){self::$smarty->display($tpl,$_SERVER['REQUEST_URI']);}//参数赋值protected function assign($var,$value){self::$smarty->assign($var,$value);}//模板缓存protected function is_cached($tpl=NULL){$tpl=$this->get_tpl($tpl);return self::$smarty->isCached($tpl,$_SERVER['REQUEST_URI']);}}
5、创建控制器基类,继承smarty及封装,
CrlBase.php
<?php
class CrlBase extends SmartyBase
{private $var;public function __construct(){parent::__construct();}protected function display($tpl=NULL){$path=$this->get_tpl($tpl);if(!is_file($path)) die($path.'模板文件不存在');if($this->var){extract($this->var);}parent::display($path);}protected function assign($var,$value){parent::assign($var,$value);}protected function get_tpl($tpl){if(is_null($tpl)){$path=VIEW.'/'.CRL.'/'.ACTION.'.html';}else{$suffix=strrchr($tpl,'.');$tpl=empty($suffix)?$tpl.'.html':$tpl;$path=VIEW.'/'.CRL.'/'.$tpl;}return $path;}}
6、控制器indexCrl.php,assign和display
<?php
class indexCrl extends CrlBase {public function index(){if($this->is_cached()){$this->assign('a',time());}$this->assign('a',time());$this->display();}
}
7、模板显示index.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
smarty参数显示:{$a}
</body>
</html>
8、完整KJ.php代码
<?phpfinal class KJ{public static function run(){//定义常量self::_set_const();//创建模块目录self::_mk_module();//类自动加载spl_autoload_register(array(__CLASS__,'_autoload'));//运行框架self::_run();}//运行控制器中方法public static function _run(){$c=strtolower(isset($_GET['c'])?$_GET['c']:'index');$a=strtolower(isset($_GET['a'])?$_GET['a']:'index');define('CRL',$c); //当前控制器define('ACTION',$a);//当前方法$c.='Crl';if(!class_exists($c)){die("控制器".$c."不存在");}$obj=new $c();if(!method_exists($obj,$a)){die("控制器".$c."下".$a."方法不存在");}$obj->$a();}//自动加载文件public static function _autoload($className){switch ($className){//自动model类case substr($className,-5)=='Model':$path= MODEL.'/'.$className.'.php';if(is_file($path)) include $path;break;//自动加载控制器case substr($className,-3)=='Crl':$path= CONTROLLER.'/'.$className.'.php';if(is_file($path)) include $path;break;//自动加载基类case substr($className,-4)=='Base':$path= KJ_CORE.'/base/'.$className.'.php';if(is_file($path)) include $path;break;default :break;}}//定义常量public static function _set_const(){//获取框架核心路径 都替换/以便兼容linux$path=str_replace('\\','//',__FILE__);//定义常量define("KJ_CORE",dirname($path)); //框架核心路径define('ROOT_PATH',dirname(KJ_CORE));//项目根目录define('MODULE_PATH',ROOT_PATH.'/'.MODULE);//模块define('CONTROLLER',MODULE_PATH.'/controller');//定义控制器define('MODEL',MODULE_PATH.'/model');//定义模型define('VIEW',MODULE_PATH.'/view');//定义显示define('EXTEND',KJ_CORE.'/extend');//定义扩建路径define('CACHE',ROOT_PATH.'/cache');//定义缓存路径//模板编译目录define('APP_COMPILE_PATH',CACHE.'/view/'.MODULE.'/Compile');//模板缓存define('APP_CACHE_PATH',CACHE.'/view/'.MODULE.'/Cache');}//自动创建模块目录public static function _mk_module(){$arr=[MODULE_PATH,CONTROLLER,MODEL,VIEW,];foreach ($arr as $v){is_dir($v) || mkdir($v,0777,true);}}}
KJ::run();