当前位置:网站首页>(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'
]);
}
}边栏推荐
猜你喜欢

1.10今日学习

4.表单与输入

SQL:DDL、DML、DQL、DCL相应介绍以及演示

(1)Thinkphp6入门、安装视图、模板渲染、变量赋值

TCP communications program

SQL classification, DQL (Data Query Language), and corresponding SQL query statement demonstration

4. The form with the input

12.什么是JS

Phpstudy安装Thinkphp6(问题+解决)

Stable and easy-to-use short connection generation platform, supporting API batch generation
随机推荐
1.8今日学习
稳定好用的短连接生成平台,支持API批量生成
1.11今日学习
微信小程序开发视频加载:[渲染层网络层错误] Failed to load media
TCP通信程序
Various ways of AES encryption
批量替换文件字体,简体-&gt;繁体
js 原型和原型链
4. PHP array and array sorting
正则笔记(1)- 正则表达式字符匹配攻略
PHP图片压缩到指定的大小
IP门禁:手把手教你用PHP实现一个IP防火墙
The Error in the render: "TypeError: always read the properties of null '0' (reading)" Error solution
uniapp | 官方提供的map组件使用问题
PHP基金会三月新闻公告发布
3. PHP data types, constants, strings and operators
ES6介绍+定义变量+不同情况下箭头函数的this指向
2.PHP变量、输出、EOF、条件语句
ES6三点运算符、数组方法、字符串扩展方法
你的本地创建的项目库还在手动创建远端代码仓库再推送吗,该用它了