Blogger Information
Blog 33
fans 0
comment 0
visits 25858
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Thinkphp5快速入门
非常缪
Original
791 people have browsed it

PHP >= 5.4.0
PDO PHP Extension
MBstring PHP Extension
CURL PHP Extension

 

安装

MacOs安装Composer

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer

   

使用Composer初始化tp5

composer create-project topthink/think blog --prefer-dist

   

结构

project 应用部署目录
├─application 应用目录(可设置)
│ ├─common 公共模块目录(可更改)
│ ├─index 模块目录(可更改)
│ │ ├─config.php 模块配置文件
│ │ ├─common.php 模块函数文件
│ │ ├─controller 控制器目录
│ │ ├─model 模型目录
│ │ ├─view 视图目录
│ │ └─ ... 更多类库目录
│ ├─command.php 命令行工具配置文件
│ ├─common.php 应用公共(函数)文件
│ ├─config.php 应用(公共)配置文件
│ ├─database.php 数据库配置文件
│ ├─tags.php 应用行为扩展定义文件
│ └─route.php 路由配置文件
├─extend 扩展类库目录(可定义)
├─public WEB 部署目录(对外访问目录)
│ ├─static 静态资源存放目录(css,js,image)
│ ├─index.php 应用入口文件
│ ├─router.php 快速测试文件
│ └─.htaccess 用于 apache 的重写
├─runtime 应用的运行时目录(可写,可设置)
├─vendor 第三方类库目录(Composer)
├─thinkphp 框架系统目录
│ ├─lang 语言包目录
│ ├─library 框架核心类库目录
│ │ ├─think Think 类库包目录
│ │ └─traits 系统 Traits 目录
│ ├─tpl 系统模板目录
│ ├─.htaccess 用于 apache 的重写
│ ├─.travis.yml CI 定义文件
│ ├─base.php 基础定义文件
│ ├─composer.json composer 定义文件
│ ├─console.php 控制台入口文件
│ ├─convention.php 惯例配置文件
│ ├─helper.php 助手函数文件(可选)
│ ├─LICENSE.txt 授权说明文件
│ ├─phpunit.xml 单元测试配置文件
│ ├─README.md README 文件
│ └─start.php 框架引导文件
├─build.php 自动生成定义文件(参考)
├─composer.json composer 定义文件
├─LICENSE.txt 授权说明文件
├─README.md README 文件
├─think 命令行入口文件

   

模板

首先是Controller:

位于application/index/controller/Index.php有一个默认的Index类:

本来它return的是开始页面,现在改为hello world。

<?php
namespace appindexcontroller;
class Index
{
public function index()
{
return 'Hello,World!';
}
}

   

然后我们再继承Controller类:

<?php
namespace appindexcontroller;
use thinkController;//引入Controller类

class Index extends Controller
{
public function index($name='world')
{
$this->assign('name',$name);
return $this->fetch();
}
}

   

我们向页面传递一个带有默认值的参数name。

然后是View:

thinkphph采用模板渲染,模板存在View文件夹下,默认是没有View文件夹的,我们自己创建:

在application/index 目录下面创建一个 view 目录,在view目录下再建一个index目录,然后添加模板文件hello.html,整个路径: view/index/index.html

<html>
<head>
<title>hello {$name}</title>
</head>
<body>
hello {$name}!
</body>
</html>

   

数据库

这里采用MySQL数据库,在blog表下建一个数据库:

create table if not exists think_data(
id int(8) not null auto_increment primary key,
data varchar(255) not null
)engine=MyISAM default charset=utf8;

   

再插入几条数据就行;

然后在 application/database.php下进行配置:

return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'blog',
// 用户名
'username' => 'root',
// 密码
'password' => '123456',
// 端口
'hostport' => '',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'think_',
// 数据库调试模式
'debug' => true,

   

修改controller下的Index类:

<?php
namespace appindexcontroller;
use thinkController;
use thinkDb;//引入数据库

class Index extends Controller
{
public function index($name='world')
{
$this->assign('name',$name);
return $this->fetch();
}

public function dbtest()
{
$data = Db::name('data')->find();
$this->assign('result',$data);
return $this->fetch();
}
}

   

再在view下的index目录下建一个dbtest.html渲染:

<html>
<head>
<title></title>
</head>
<body>
{$result.id---$result.data}
</body>
</html>

   


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!