ThinkPHP6起步
ThinkPHP支持传统的MVC(Model-View-Controller)模式以及流行的MVVM(Model-View-ViewModel)模式的应用开发
一、MVC
MVC软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)
ThinkPHP6是一个典型的MVC架构。
控制器-控制器。用于将用户请求转发给相应的Model进行处理,并根据Model的计算结果向用户提供相应响应的。
视图-为用户提供使用界面,与用户直接进行交互。
模型-承接数据,并对用户提交请求进行计算的模块。
MVC架构程序的工作流程:
(1)用户通过View页面向服务端提出请求,可以是表单请求。超链接请求。AJAX请求
(2)服务端Controller控制器接收到请求后对请求进行解析,找到相对应的Model对用户请求进行处理。
(3)Model处理后,将处理结果再交给Controller
(4)Controller在接到处理结果后,根据处理结果找到要作为向客户端发回的响应View页面。页面经渲染(数据填充后),再发送到客户端。
二、单应用模式访问
项目访问路径:www.xxx.com/index.php/index/index
index.php 入口文件(可以通过配置伪静态省略)
index控制器
index方法
一个文件里面只能有一个类,并且类名要与文件名相对应。
查看报错信息,将config文件夹下的app.php,‘show_error_msg’=>true
三、安装视图
新版框架默认只能支持PHP原生模板,如果需要使用think Template模板引擎,需要安装think-view扩展(该扩展会自动安装think-template依赖库)
composer require topthink/think-view
视图目录可以在根目录,也可以在app应用目录,可以通过config目录下的view.php修改视图配置。
四、模板渲染
要使用View,必须先引入think/facade/View 门面类
模板渲染的最典型用法是直接使用fetch方法,不带任何参数,系统会按照默认规则自动定位视图(View)目录下的模板文件,其规则是:控制器名(小写+下划线)、操作(方法名字).html
Controller代码
<?php
namespace app\controller;
use app\BaseController; use think\facade\View; //引用门面类
class Index extends BaseController {
public function index() {
return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript"src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello($name = 'ThinkPHP6') {
return 'hello,' . $name;
}
}
view代码(注:在app目录下,view目录需要我们手动创建)
如果想要更改应用视图的位置,在config文件目录下view.php可以修改参数配置实现。
创建视图的过程,我们采取一个简单的例子
假设我们在默认存在的index.php里面创建一个访问视图的方法
同理,我们需要创建一个同名的html页面
在浏览器以控制器名称加方法名访问它
http://www.xxx.com/index.php/index/test
如果我们想使用view::fetch()访问我们指定的页面,则需要在fetch方法中带上指定的函数。
例如我们想访问view视图层底下index目录底下的login.html文件,我们可以这么做
<?php
namespace app\controller;
use app\BaseController;
use think\facade\View; //引用门面类
class Index extends BaseController
{
public function index()
{
return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello($name = 'ThinkPHP6')
{
return 'hello,' . $name;
}
public function test()
{
return view::fetch();
}
//访问指定页面,ibdex目录下的login.html
public function login()
{
return view::fetch('index/login');
}
}
访问成功提示
五、模板变量
- 使用assgin方法进行全局模板变量赋值。
- 模板输出{$name}
Controller代码
<?php
namespace app\controller;
use app\BaseController;
use think\facade\View; //引用门面类
class Index extends BaseController
{
public function index()
{
return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello($name = 'ThinkPHP6')
{
return 'hello,' . $name;
}
public function test()
{
return view::fetch();
}
public function login()
{
return view::fetch('index/login');
}
//模板变量
public function variable(){
//模板变量赋值
view::assign('name','moon');
view::assign('qq','1809620325');
//或者批量赋值
view::assign([
'name' => 'moon',
'qq' => '1809620325'
]);
//模板输出
return view::fetch('index/test');
}
}
View代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
名字:{$name}
<br/>
qq:{$qq}
</body>
</html>
展示效果
- assign方法赋值属于全局变量赋值,如果你需要单次赋值的话,可以直接在fetch方法中传入
controller代码
<?php
namespace app\controller;
use app\BaseController;
use think\facade\View; //引用门面类
class Index extends BaseController
{
public function index()
{
return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello($name = 'ThinkPHP6')
{
return 'hello,' . $name;
}
public function test()
{
return view::fetch();
}
public function login()
{
return view::fetch('index/login');
}
//模板变量
public function variable(){
//模板输出并变量赋值,test为模板名称
return view::fetch('test',[
'name' => 'moon',
'qq' => '1809620325'
]);
}
}
展示效果
助手函数
- 如果使用View助手函数渲染输出的话,可以使用以下的方法进行模板变量赋值
Controller代码
<?php
namespace app\controller;
use app\BaseController;
use think\facade\View; //引用门面类
class Index extends BaseController
{
public function index()
{
return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
}
public function hello($name = 'ThinkPHP6')
{
return 'hello,' . $name;
}
public function test()
{
return view::fetch();
}
public function login()
{
return view::fetch('index/login');
}
//模板变量
public function variable(){
//使用助手函数进行赋值
return view('test',[
'name'=>'moonloon',
'qq'=>'1809620325'
]);
}
}
展示结果