当前位置:网站首页>(1) introduction to Thinkphp6, installation view, template rendering, variable assignment
(1) introduction to Thinkphp6, installation view, template rendering, variable assignment
2022-08-02 03:57:00 【stealth rookie】
目录
二.Single application mode access
Thinkphp支持传统的MVC(Model-View-Controller)模式以及流行的MVVM(Model-View-ViewModel)模式的应用开发.
一、MVC
MVC 软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)
ThinkPHP6 是一个典型的 MVC 架构
控制器—控制器,用于将用户请求转发给相应的Model进行处理,并根据ModelThe calculation results to the user to provide the corresponding response of view—为用户提供使用界面,与用户直接进行交互.
模型—承载数据,并对用户提交请求进行计算的模块.
MVC架构程序的工作流程:
(1)用户通过View 页面向服务端提出请求,可以是表单请求、超链接请求、AJAX 请求等
(2)服务端Controller 控制器接收到请求后对请求进行解析,找到相应Model 对用户请求进行处理
(3)Model处理后,将处理结果再交给 Controller
(4)Controller在接到处理结果后,根据处理结果找到要作为向客户端发回的响应View页面.页面经渲染(数据填充)后,再发送给客户端.
二.Single application mode access
项目访问路径:域名/index.php/index/index
index.php 入口文件(Can be configured pseudo static omit)
index 控制器
index 方法
文件名With the file类名要一致,You can't get automatic loading
三.安装视图
The new framework by default can only supportPHP原生模板,如果需要使用think Template模板引擎,需要安装think-view扩展(Extensions will automatically installthink Template依赖库)
composer require topthink/think-view
The view can be in the root directory,也可以在app应用目录,可以通过config目录下的view.php修改视图配置
四. 模板渲染
要使用View,必须先引入think\facade\View 门面类
Template to render the most typical usage is used directlyfetch方法,不带任何参数,The system will automatically according to the rules of the default location view(view)目录下的模板文件,其规则是:控制器名(小写+下划线)/操作名(方法名字).html
controller代码 >> index.php
<?php
namespace app\controller;
use think\facade\View;
class Index{
public function index(){
return View::fetch();
}
}
view代码(注:在app目录下,viewCatalog we need to manually create)
A controller interface file(index.php)需要对应viewUnder the same name in the directory a nameHTML文件.
即:
创建文件:view/index/index.html,And in the controller interface file corresponding,This controller interface file of the rendering code will find thisHTML模板进行渲染.
注:The controller inside classview下面的目录
注:Controller inside method corresponding toview下面的目录里的静态文件
五.模板变量
使用assignMethods for global template variable assignment
模板输出{$name}
controller代码
<?php
namespace app\controller;
use think\facade\View;
class Index{
public function index(){
// 模板变量赋值
View::assign('name','小明');
View::assign('qq','1233344444');
// 或者批量赋值
View::assign([
'name' => '小明',
'qq' => '1233344444'
]);
// 模板输出
return View::fetch();
}
}
view代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ThinkPHP6</title>
</head>
<body>
姓名:{$name}
<br>
QQ:{$qq}
</body>
</html>
assign方法赋值属于全局变量赋值,If you need word assignment,可以直接用fetch方法传入
namespace app\controller;
use think\facade\View;
class Index{
public function index(){
//模板输出并变量赋值
//fetch(模板,赋值)
return View::fetch('index'.[
'name'=>'小明',
'qq'=>'1233344444'
]);
}
}
助手函数,如果使用viewHelper function to render the output,Use the following method to a template variable assignment
#controller代码
namespace app\controller;
use think\facade\View;
class Index{
public function index(){
//模板输出并变量赋值
return View('index'.[
'name'=>'小明',
'qq'=>'1233344444'
]);
}
}
边栏推荐
- js 原型和原型链
- [sebastian/diff] A historical change extension library for comparing two texts
- [symfony/finder] The best file manipulation library
- 关于tp的apache 的.htaccess文件
- Using PHPMailer send mail
- 4.表单与输入
- Phpstudy安装Thinkphp6(问题+解决)
- [vite] Failed to parse source for import analysis because the content contains invalid JS syntax.
- PHP图片压缩到指定的大小
- 每日五道面试题总结 22/7/21
猜你喜欢
随机推荐
uniapp | 开发中遇到的兼容性问题(待续)
[league/flysystem]一个优雅且支持度非常高的文件操作接口
(8) requests, os, sys, re, _thread
2. PHP variables, output, EOF, conditional statements
PHP实现搜索框的自动反查提示
[campo/random-user-agent]随机伪造你的User-Agent
宝塔邮局邮箱设置成功后能发送不能接收问题处理
js 正则中 replace() 使用
(2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合
v-on基本使用、参数传递、修饰词
Scrapy爬虫遇见重定向301/302问题解决方法
每日五道面试题总结 22/7/26
你的本地创建的项目库还在手动创建远端代码仓库再推送吗,该用它了
使用PHPMailer发送邮件
[sebastian/diff] A historical change extension library for comparing two texts
TypeScript error error TS2469, error TS2731 solution
DVWA drone installation tutorial
如何根据地图上的两个坐标点来确定方向
三元判断再三元判断
数组的高级操作