当前位置:网站首页>(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'
]);
}
}
边栏推荐
- 14.JS语句和注释,变量和数据类型
- Dom实现input的焦点触发
- MySql Advanced -- Constraints
- Batch replace file fonts, Simplified -> Traditional
- 2.PHP变量、输出、EOF、条件语句
- [league/flysystem]一个优雅且支持度非常高的文件操作接口
- Several interesting ways to open PHP: from basic to perverted
- v-bind用法:类动态绑定对象 数组 style样式 及函数方法
- (7) 浅学 “爬虫” 过程 (概念+练习)
- 2. PHP variables, output, EOF, conditional statements
猜你喜欢
随机推荐
Thread Pool (Introduction and Use of Thread Pool)
点名系统和数组元素为对象的排序求最大值和最小值
解决5+APP真机测试无法访问后台(同局域网)
微信小程序开发视频加载:[渲染层网络层错误] Failed to load media
稳定好用的短连接生成平台,支持API批量生成
JS objects, functions and scopes
SQL分类、DQL(数据查询语言)、以及相应SQL查询语句演示
Solve the problem of Zlibrary stuck/can't find the domain name/reached the limit, the latest address of Zlibrary
(6) 学生信息管理系统设计
DVWA drone installation tutorial
js eventLoop 事件循环机制
IO流、字节流、字节缓冲流
PHP入门(自学笔记)
批量替换文件字体,简体-&gt;繁体
12.什么是JS
IP access control: teach you how to implement an IP firewall with PHP
你的本地创建的项目库还在手动创建远端代码仓库再推送吗,该用它了
Using PHPMailer send mail
Advanced Operations on Arrays
TCP communications program