Linux 全局安装 composer

将目录切换到/usr/local/bin/目录
cd /usr/local/bin/
在 bin 目录中下载 composer
curl -sS https://getcomposer.org/installer | php
通过 composer.phar -v 查看 composer
修改为中国镜像
composer.phar config -g repo.packagist composer https://packagist.phpcomposer.com
最后我把composer.phar复制了一份,重命名为composer
以后可以直接用composer

MVC架构模式
控制器(Controller)- 负责转发请求,对请求进行处理
视图(View) - 界面设计人员进行图形界面设计
模型(Model) - 程序员编写程序应有的功能(实现算法等等)、数据库专家进行数据管理和数据库设计(可以实现具体的功能)

实现简易模板引擎:

composer.json

    {
// 自动加载
// 可以在composer.json的autoload字段找那个添加自己的autoloader
"autoload": {
"psr-4": {
"App\\Controllers\\": "Controllers/",
"App\\Models\\": "Models/",
"Tools\\": "Tools/"
}
}
}

Models/Users.php

    <?php
// model层数据库操作演示
namespace App\Models; class Users
{
// 数据存入数据库演示
public function store()
{
echo 'store into database';
} // 查询数据库演示
public function getUsername()
{
// 查询数据库
return 'test-data';
}
}

Controllers/UserController.php

    <?php
namespace App\Controllers; use Tools\Tpl;
use App\Models\Users; class UserController extends Tpl
{
public function create()
{
echo 'User create';
} public function getUser()
{
// 通过Model查询数据
$userModel = new Users;
$username = $userModel->getUsername(); // 将$username显示在对应的一个HTML文件当中,并且显示出来
// 表现层 user/user.html
// 将变量发送给模板(html文件)
$this->assign('username', $username);
$this->assign('age', );
// 显示模板
$this->display('user/user.html');
}
}

Views/user/user.html

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>
{$username}
</h2>
<h3>
{$age}
</h3>
</body>
</html>

Tools/Tpl.php

    <?php
// 分配变量
// 将变量发送给模板
namespace Tools; class Tpl
{
protected $assign_vars = []; public function assign($tpl_var_name, $val)
{
$this->assign_vars[$tpl_var_name] = $val;
} public function display($tpl_name)
{
// Views/user/$tpl_name
$className = get_called_class(); // App\Controllers\UserController
$dirName = strtolower(substr(substr($className, ), , -)); // user
$dir = dirname(__DIR__) . '/Views/' . $dirName . '/' . $tpl_name;
// file_get_contents
$content = file_get_contents($dir);
// preg_replace
foreach ($this->assign_vars as $tpl_var_name => $val) {
$content = preg_replace('/\{\$' . $tpl_var_name . '\}/', '<?php echo $this->assign_vars["' . $tpl_var_name . '"]; ?>', $content);
}
// compile
$compile = dirname(__DIR__) . '/runtime/Compile/' . md5($tpl_name) . '.php';
file_put_contents($compile, $content);
// include
include $compile;
}
}

Smarty模板引擎的使用

服务端开发部分演示
Smarty引擎的安装
变量的分配和加载显示模板
以插件形式扩展Smarty
缓存控制技术


smarty.php

    <?php
/**
* Created by PhpStorm.
*/
session_start();
require './libs/Smarty.class.php'; $smarty = new Smarty(); // 简单配置 初始化设置
$smarty->setTemplateDir('./Views');
$smarty->setCompileDir('./runtime/Compile');
$smarty->setConfigDir('./Config');
$smarty->addPluginsDir('./Plugins');
$smarty->setCacheDir('./runtime/Cache'); $smarty->caching = 1;//开启缓存
$smarty->setCacheLifetime(60*60*24);
$smarty->left_delimiter = '{';
$smarty->right_delimiter = '}'; // 缓存机制
if (!$smarty->isCached('extends.html', $_SERVER['REQUEST_URI'])) { // 数据库查询
$data = [[]]; // 使用
$smarty->assign([
'username' => 'test-data',
'age' => 20
]); // 数组
$smarty->assign('arr1', [1, 2, 3]);
$smarty->assign('arr2', ['id' => 1, 'username' => 'zhangsan', 'age' => 30]);
$smarty->assign('users', [
['id' => 1, 'username' => 'zhangsan', 'age' => 30],
['id' => 2, 'username' => 'lisi', 'age' => 40]
]); $smarty->assign('hobby_ids', [1, 2, 3]);
$smarty->assign('hobby_output', ['看书', '敲代码', '看视频']);
$smarty->assign('options', [
1 => '看书',
2 => '敲代码',
3 => '看视频'
]); // 注册function
$smarty->registerPlugin('function', 'font', function ($attributes) {
$text = $attributes['text'];
$color = $attributes['color'] ?? 'black';
return '<span style="color: ' . $color . '">' . $text . '</span>';
}); // 注册变量修饰器
$smarty->registerPlugin('modifier', 'link', function ($text, $href, $isCapitalize = false) {
$return = '<a href="' . $href . '">' . $text . '</a>';
if ($isCapitalize) {
return ucwords($return);
}
return $return;
}); // 注册块状函数
$smarty->registerPlugin('block', 'link', function ($attributes, $text) {
$href = $attributes['href'];
if (!is_null($text)) {
return '<a href="' . $href . '">' . $text . '</a>';
}
}); // 对象
$smarty->assign('obj', $smarty); $smarty->assign('text', 'This is a paragraph!'); $smarty->display('smarty.html');
$smarty->display('loop.html');
$smarty->display('single_tag_func.html');
$smarty->display('modifier.html');
$smarty->display('block_func.html');
$smarty->display('plugins.html');
$smarty->assign('users', $data);
}
$smarty->clearCache('extends.html', $_SERVER['REQUEST_URI']);
$smarty->clearAllCache();
$smarty->display('extends.html', $_SERVER['REQUEST_URI']);

前端开发部分演示

注释和变量的使用smarty.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>
<!-- 模板注释被*星号包围,而两边的星号又被定界符包围 -->
{*$username*}
{$username}
</h2>
<h3>
<!-- 变量 -->
{$age}
</h3>
<hr>
<!-- 索引数组 -->
arr1:
{$arr1[1]}
<hr>
<!-- 关联数组 -->
arr2:
{$arr2['username']}
{$arr2.username}
<hr>
<!-- 对象 -->
Object:
{var_dump($obj->getTemplateDir())}
<hr>
<!-- 变量的运算 -->
{$var = 100}
{$var}
{$foo = $var + 200}
{$foo}
<hr>
{$foo}
<hr>
<!-- 保留变量的使用 -->
$_GET:
{var_dump($smarty.get)}
<hr>
$_POST:
{var_dump($smarty.post)}
<hr>
$_REQUEST:
{var_dump($smarty.request)}
<hr>
COOKIE:
{var_dump($smarty.cookies)}
<hr>
SESSION:
{var_dump($smarty.session)}
<hr>
SERVER:
{var_dump($smarty.server)}
<hr>
ENV:
{var_dump($smarty.env)}
<hr>
{time()}
{$smarty.now}
<hr>
<!-- 加载配置文件后,配置文件中的变量需要用两个井号“#”包围或者是 smarty的保留变量$smarty.config.来调用 -->
{config_load file='base.conf'}
{#FONT_SIZE#}
{$smarty.config.FONT_COLOR}
</body>
</html>

流程控制的使用loop.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>流程控制</title>
</head>
<body>
<!-- 使用{if}处理分支结构 -->
{$number = 200}
{if $number === 100}
gt
{else if $number == 200}
This number is 200
{else}
This number is not 100
{/if} {$bool = false}
{if not $bool}
not
{/if} {if $number is not even}
odd
{/if} {if $number is not odd}
even
{/if} {if $number mod 2 == 0}
even
{/if} {if $number is not odd by 3}
odd
{/if} <!-- 使用{for}处理循环 -->
{for $i = 5 to 4 step 2}
{$i}
{forelse}
no loop
{/for} <!-- 使用{while}处理循环 -->
{while $number > 195}
{$number--}
{/while} <!-- 使用{foreach}遍历数组 -->
{foreach $arr2 as $key => $val}
{if $val@first}
{*break*}
{continue}
{/if}
{$key}:{$val}
{$val@key}
{$val@index}
{$val@iteration}
{$val@first}
{$val@last}
{$val@show}
{$val@total}
{foreachelse}
data does not exist
{/foreach} <!-- 使用{section}遍历数组 -->
{section name=key loop=$arr1}
{$arr1[key]}
{/section} {section name=key loop=$users2 step=-1 max=2}
id: {$users[key].id}
username: {$users[key].username}
age: {$users[key].age}
{$smarty.section.key.index}
{$smarty.section.key.iteration}
{$smarty.section.key.rownum}
{$smarty.section.key.index_prev}
{$smarty.section.key.index_next}
{sectionelse}
no loop
{/section} </body>
</html>

常用标签函数的使用single_tag_func.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>常用标签函数的使用</title>
</head>
<body>
{assign var="name" value="Jason"}
{assign "name" "Jason Lee"}
{$name} {append var="arr1" value=4 index="3"}
{var_dump($arr1)} {ldelim}$name{rdelim} {html_checkboxes name="hobby" values=$hobby_ids output=$hobby_output selected=$hobby_ids}
{html_checkboxes name="hobby" options=$options selected=$hobby_ids}
{html_image width="50" height="50" alt="Google" href="http://www.google.com" file="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"}
{html_options name="hobby" values=$hobby_ids output=$hobby_output selected=2}
{html_options name="hobby" options=$options selected=2}
{html_radios name="hobby" options=$options selected=2}
{html_select_date}
{html_select_time}
{html_table loop=$arr1 cols=2 rows=3}
{mailto address="86267659@qq.com" subject="test" text="给我发邮件" cc="123123@qq.com"}
{math equation="x + y" x = 100 y = 200}
</body>
</html>

变量修饰器的使用modifier.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>变量修饰器的使用</title>
</head>
<body>
{$str="123123\nthIs is string."}
<hr>
{$str|capitalize:true:true}
<hr>
{$str|capitalize:true:true|cat:'.'}
<hr>
{$str|count_characters}
<hr>
{$str|count_paragraphs}
<hr>
{$str|count_sentences}
<hr>
{$str|count_words}
<hr>
{$str2|default:'Not Data Yet'}
<hr>
{time()|date_format:'%Y-%m-%d %H:%M:%S'}
<hr>
{$chinese = '中文'}
{$chinese|from_charset:'utf-8'|to_charset:'gb2312'}
<hr>
{$str|indent:10:'---'}
<hr>
{$str|lower|upper}
<hr>
{$str2="This is p1.\nThis is p2."}
{$str2|nl2br}
<hr>
{$str|regex_replace:'/\d+/':' '}
<hr>
{$str|replace:'123123':'000'}
<hr>
{$str|spacify:'-'}
<hr>
{$float='10.0020398475'}
{$float|string_format:'%.2f'}
<hr>
{$str3='a b c'}
{$str3|strip:'-'}
<hr>
{$tag='<b>Font</b>'}
{$tag|strip_tags}
<hr>
{$bigstr='123123123123123123ahjfdashfksdhfkjsdhjkfshfjkhsd'}
{$bigstr|truncate:10:'---':true:true}
<hr>
{$tag|escape|unescape}
<hr>
{$bigstr|wordwrap:10:"\n":true}
</body>
</html>

块函数的使用block_func.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>块函数的使用</title>
</head>
<body>
{textformat indent='4' indent_first='10' indent_char='-' wrap='10' wrap_char='<hr>' wrap_cut=true assign='var'}
aaaaaaaaaaaaaaa
aaaaaaaaaaaaaaa
aaaaaaaaaaaaaaa
aaaaaaaaaaaaaaa
aaaaaaaaaaaaaaa
aaaaaaaaaaaaaaa
aaaaaaaaaaaaaaa
{/textformat} {*$var*} {nocache}
{time()}
{/nocache}
<hr>
{time()}
</body>
</html>

插件的开发plugins.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{font text=$text color='#123123'}
{$text|link:'http://www.baidu.com'}
{link href='http://www.baidu.com'}
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaa
{/link}
</body>
</html>

Smarty模板引擎插件的开发:
1、使用registerPlugin( )方法扩充插件格式


2、在smarty模板的libs/plugins/目录下创建函数插件文件

block.link.php

function smarty_block_link($attributes, $text)
{
$href = $attributes['href'];
if (!is_null($text)) {
return '<a href="' . $href . '">' . $text . '</a>';
}
}

function.font.php

function smarty_function_font($attributes)
{
$text = $attributes['text'];
$color = $attributes['color'] ?? 'black';
return '<span style="color: ' . $color . '">' . $text . '</span>';
}

modifier.link.php

function smarty_modifier_link($text, $href, $isCapitalize = false)
{
$return = '<a href="' . $href . '">' . $text . '</a>';
if ($isCapitalize) {
return ucwords($return);
}
return $return;
}

模板继承的使用
extends.html

!-- 使用{extends}函数实现模板继承
合并子模板和父模板的{block}标签内容 --> {extends file="layout.html"}
{block name="title"}
Article {$smarty.block.parent}
{/block} {block name="content"}
Article List
{$smarty.get.page}
{*nocache*}
{time()}
{*/nocache*}
{time()|date_format:'%H:%M:%S' nocache}
{/block}

layout.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{block name="title"} - Imooc{/block}</title>
</head>
<body>
<header>
menu
</header>
{block name="content"}{/block}
<footer>
copyright
</footer>
</body>
</html>

缓存机制
开启缓存
  $smarty -> caching = 1|2|0;
  $smarty -> setCacheDir("./cache");
  $smarty->setCacheLifetime(300); // 5分钟,以秒为单位,-1永不过期
  $smarty -> display('index.tpl');
  $smarty -> display('index.tpl', $_SERVER['REQUEST_URI']);

相关函数
  isCached()
  clearCache()
  clearAllCache()

MVC开发模式以及Smarty模板引擎的使用的更多相关文章

  1. 写一个迷你版Smarty模板引擎,对认识模板引擎原理非常好(附代码)

    前些时间在看创智博客韩顺平的Smarty模板引擎教程,再结合自己跟李炎恢第二季开发中CMS系统写的tpl模板引擎.今天就写一个迷你版的Smarty引擎,虽然说我并没有深入分析过Smarty的源码,但是 ...

  2. Smarty模板引擎的使用

    Smarty模板引擎的使用 Smarty是PHP中一个基于MVC模式的模板引擎. Download: http://www.smarty.net/download   特点 1.  最快速度的程序开发 ...

  3. smarty模板引擎(一)基础知识

    一.基本概念 1.什么是mvc?     mvc是一种开发模式,核心思想是:数据的输入.数据的处理.数据显示的强制分离. 2.什么是smarty?     smarty是一个php的模板引擎.更明白的 ...

  4. 深入浅出之Smarty模板引擎工作机制(二)

    源代码下载地址:深入浅出之Smarty模板引擎工作机制 接下来根据以下的Smarty模板引擎原理流程图开发一个自己的模板引擎用于学习,以便加深理解. Smarty模板引擎的原理,其实是这么一个过程: ...

  5. 深入浅出之Smarty模板引擎工作机制(一)

    深入浅出Smarty模板引擎工作机制,我们将对比使用smarty模板引擎和没使用smarty模板引擎的两种开发方式的区别,并动手开发一个自己的模板引擎,以便加深对smarty模板引擎工作机制的理解. ...

  6. Smarty模板引擎技术

    Smarty模板引擎技术 什么是模板引擎? 什么是Smarty模板引擎? 为何选择Smarty模板引擎? 如何使用Smarty模板引擎? 一.历史背景 场景一:回顾之前编写PHP项目的方式 //链接数 ...

  7. 迷你版 smarty --模板引擎和解析

    http://blog.ipodmp.com/archives/php-write-a-mini-smarty-template-engine/ 迷你版Smarty模板引擎目录结构如下: ① 要开发一 ...

  8. Extjs 6 MVC开发模式(二)

    1.Extjs MVC开发模式 在JS的开发过程中,大规模的JS脚本难以组织和维护,这一直是困扰前端开发人员的头等问题.Extjs为了解决这种问题,在Extjs4.x版本中引入了MVC开发模式,开始将 ...

  9. 前端学PHP之Smarty模板引擎

    前面的话 对PHP来说,有很多模板引擎可供选择,但Smarty是一个使用PHP编写出来的,是业界最著名.功能最强大的一种PHP模板引擎.Smarty像PHP一样拥有丰富的函数库,从统计字数到自动缩进. ...

随机推荐

  1. thinkphp远程执行漏洞的本地复现

    thinkphp远程执行漏洞的本地复现 0X00漏洞简介 由于ThinkPHP5 框架控制器名 没有进行足够的安全监测,导致在没有开启强制路由的情况下,可以伪装特定的请求可以直接Getshell(可以 ...

  2. 06-Spring03-事务管理

    今日知识 1. Spring事务管理 2. 转账案例 Spring事务管理 1. 事务特性(ACID) 1. 原子性:整体 [原子性是指事务包含的所有操作要么全部成功,要么全部失败] 2. 一致性:数 ...

  3. 【问题】多重继承时,super函数只初始化继承的第一个类,不初始化第二个类。

    class A(object): def __init__(self): print("init class A") class B(object): def __init__(s ...

  4. iOS9下的Map Kit View下的使用

    最近有个任务是关于地理位置上的标注开发,经过一些资料的查找和对比,现总结一些经验,给读者也是给自己. iOS9下的Map Kit View实际是以前MapKit,只不过换了一个名字,实际是指同一个UI ...

  5. Windows AD日志分析平台WatchAD安装教程

    目录 WatchAD介绍 安装环境 WatchAD安装(日志分析端服务) 基础环境配置 安装WatchAD 运行WatchAD WatchAD-web安装(Web监控端服务) 下载WatchAD-We ...

  6. Blazui 常见问题:我更新了数据,为什么界面没刷新?

    首发于:http://www.blazor.group:8000/topic/reply?tpid=9 开门见山,不介绍,不废话 建议食用本文前先食用 https://www.cnblogs.com/ ...

  7. 《Java 8 in Action》Chapter 12:新的日期和时间API

    在Java 1.0中,对日期和时间的支持只能依赖java.util.Date类.同时这个类还有两个很大的缺点:年份的起始选择是1900年,月份的起始从0开始. 在Java 1.1中,Date类中的很多 ...

  8. StarUML之一、UML的相关基本概念

    为什么用UML 项目需要,在项目开发实现前期进行框架技术设计(条条大路通罗马通罗马,画图或者写代码都可以,适合就可以!). 用户的交互我们通常用Axure(原型设计)体现, 框架和功能结构设计则用UM ...

  9. Android中DatePicker日期选择器的使用和获取选择的年月日

    场景 实现效果如下 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改 ...

  10. window 下如何恢复被删除的mysql root账户及密码(mysql 8.0.17)

    不久前自学完完sql,下了mysql8.0.17,安装配置好后探索着,想着用root账户登上去能不能删除root账户呢,然后就想给自己一巴掌,,, 如何快速恢复root: 1.关闭mysql服务:wi ...