Introduction to ThinkPHP5 quick start method. Download
Download address: http://www.thinkphp.cn/
This time using thinkphp5, I used github to install it.
Github
Application project: https://github.com/top-think/think
Core framework: https://github.com/top-think/framework
In addition:
Code Cloud:
Application project: https://git.oschina.net/liuIntroduction to ThinkPHP5 quick start methodIntroduction to ThinkPHP5 quick start methodst/thinkphp5.git
Core framework: https://git.oschina.net/liuIntroduction to ThinkPHP5 quick start methodIntroduction to ThinkPHP5 quick start methodst/ framework.git
Coding:
Application project: https://git.coding.net/liuIntroduction to ThinkPHP5 quick start methodIntroduction to ThinkPHP5 quick start methodst/thinkphp5.git
Core framework: https://git.coding.net/liuIntroduction to ThinkPHP5 quick start methodIntroduction to ThinkPHP5 quick start methodst/framework.git
Downloaded directory:
1 2 3 4 5 6 7 8 9 10 11 12 | tp5
├─application 应用目录
├─extend 扩展类库目录(可定义)
├─ public 网站对外访问目录
├─runtime 运行时目录(可定义)
├─vendor 第三方类库目录(Composer)
├─thinkphp 框架核心目录
├─build.php 自动生成定义文件(参考)
├─composer.json Composer定义文件
├─LICENSE.txt 授权说明文件
├─README.md README 文件
├─think 命令行工具入口
|
Copy after login
The structure of the core framework directory is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | ├─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 框架引导文件
|
Copy after login
Introduction to ThinkPHP5 quick start method. RunI use It is the apacheIntroduction to ThinkPHP5 quick start method server that comes with kali. Use service apacheIntroduction to ThinkPHP5 quick start method start
to start. You need to put the entire project downloaded from git into the server running directory. The default for Linux is:
and then on the browser side Enter: http://localhost/tp5/public/
You will see the welcome page:

If you don’t want to install any WEB server, you can also directly use the WebServer that comes with PHP and run router.php to run the test.
Enter the command line, enter the tp5/public directory, and enter the following command:
1 | php -S localhost:8888 router.php
|
Copy after login
You can then directly access

##Introduction to ThinkPHP5 quick start method. Directory structure
What we pay most attention to is the application directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ├─application 应用目录(可设置)
│ ├─index 模块目录(可更改)
│ │ ├─config.php 模块配置文件
│ │ ├─common.php 模块公共文件
│ │ ├─controller 控制器目录
│ │ ├─model 模型目录
│ │ └─view 视图目录
│ │
│ ├─command.php 命令行工具配置文件
│ ├─common.php 应用公共文件
│ ├─config.php 应用配置文件
│ ├─tags.php 应用行为扩展定义文件
│ ├─database.php 数据库配置文件
│ └─route.php 路由配置文件
|
Copy after login
The 5.0 version adopts a modular design architecture. There is only one index module directory under the default application directory. If you want to add a new module, you can use the control command to generate. Switch to the command line mode, enter the application root directory (under tp5) and execute the following instructions:
1 | php think build --module demo
|
Copy after login
will generate a default demo module, including the following directory structure:
1 2 3 4 5 6 7 | ├─demo
│ ├─controller 控制器目录
│ ├─model 模型目录
│ ├─view 视图目录
│ ├─config.php 模块配置文件
│ └─common.php 模块公共文件
同时也会生成一个默认的 Index 控制器文件。
|
Copy after login
4. Template rendering
First is the Controller:
Located at
application/index/controller/Index.php there is a default Index class:
Originally it returned the start page, but now it returns hello world.
1 2 3 4 5 6 | <?phpnamespace app\index\controller; class Index{
public function index()
{
return &#Introduction to ThinkPHP5 quick start method9;Hello,World!&#Introduction to ThinkPHP5 quick start method9;;
}
}
|
Copy after login
Then we inherit the Controller class:
1 2 3 4 5 6 | <?phpnamespace app\index\controller; use think\Controller;
public function index( $name =&#Introduction to ThinkPHP5 quick start method9;world&#Introduction to ThinkPHP5 quick start method9;)
{
$this ->assign(&#Introduction to ThinkPHP5 quick start method9;name&#Introduction to ThinkPHP5 quick start method9;, $name ); return $this ->fetch();
}
}
|
Copy after login
We pass a parameter name with a default value to the page.
Then View:
thinkphph uses template rendering. The template is stored in the View folder. There is no View folder by default. We create it ourselves:
Create a view directory under the
application/index directory, create an index directory under the view directory, and then add the template file hello.html, the entire path:
view/index/hello.html
1 2 | <html><head><title>hello { $name }</title></head><body>
hello { $name }!</body></html>
|
Copy after login
Then we can access:

or use the omitted path:
http://localhost/tp5/public/
More advanced ones can configure URL routing.
5. Access the database
Mysql database is used here, and a database is built under the test table:
1 | create table if not exists think_data( id int(8) not null auto_increment primary key, data varchar(Introduction to ThinkPHP5 quick start method55) not null )engine=MyISAM default charset=utf8;
|
Copy after login
Just insert a few more pieces of data;
Then configure it under
application/database.php:
1 2 3 4 5 6 7 8 9 10 11 12 | return [
&#Introduction to ThinkPHP5 quick start method9;type&#Introduction to ThinkPHP5 quick start method9; => &#Introduction to ThinkPHP5 quick start method9;mysql&#Introduction to ThinkPHP5 quick start method9;,
&#Introduction to ThinkPHP5 quick start method9;hostname&#Introduction to ThinkPHP5 quick start method9; => &#Introduction to ThinkPHP5 quick start method9;Introduction to ThinkPHP5 quick start methodIntroduction to ThinkPHP5 quick start method7.0.0.Introduction to ThinkPHP5 quick start method&#Introduction to ThinkPHP5 quick start method9;,
&#Introduction to ThinkPHP5 quick start method9;database&#Introduction to ThinkPHP5 quick start method9; => &#Introduction to ThinkPHP5 quick start method9;test&#Introduction to ThinkPHP5 quick start method9;,
&#Introduction to ThinkPHP5 quick start method9;username&#Introduction to ThinkPHP5 quick start method9; => &#Introduction to ThinkPHP5 quick start method9;root&#Introduction to ThinkPHP5 quick start method9;,
&#Introduction to ThinkPHP5 quick start method9;password&#Introduction to ThinkPHP5 quick start method9; => &#Introduction to ThinkPHP5 quick start method9;&#Introduction to ThinkPHP5 quick start method9;,
&#Introduction to ThinkPHP5 quick start method9;hostport&#Introduction to ThinkPHP5 quick start method9; => &#Introduction to ThinkPHP5 quick start method9;&#Introduction to ThinkPHP5 quick start method9;,
&#Introduction to ThinkPHP5 quick start method9;dsn&#Introduction to ThinkPHP5 quick start method9; => &#Introduction to ThinkPHP5 quick start method9;&#Introduction to ThinkPHP5 quick start method9;,
&#Introduction to ThinkPHP5 quick start method9;params&#Introduction to ThinkPHP5 quick start method9; => [],
&#Introduction to ThinkPHP5 quick start method9;charset&#Introduction to ThinkPHP5 quick start method9; => &#Introduction to ThinkPHP5 quick start method9;utf8&#Introduction to ThinkPHP5 quick start method9;,
&#Introduction to ThinkPHP5 quick start method9;prefix&#Introduction to ThinkPHP5 quick start method9; => &#Introduction to ThinkPHP5 quick start method9;think_&#Introduction to ThinkPHP5 quick start method9;,
&#Introduction to ThinkPHP5 quick start method9;debug&#Introduction to ThinkPHP5 quick start method9; => true,
|
Copy after login
Modify the Index class under the controller:
1 2 3 4 5 6 7 8 9 | <?phpnamespace app\index\controller; use think\Controller; use think\Db;
public function index( $name =&#Introduction to ThinkPHP5 quick start method9;world&#Introduction to ThinkPHP5 quick start method9;)
{
$this ->assign(&#Introduction to ThinkPHP5 quick start method9;name&#Introduction to ThinkPHP5 quick start method9;, $name ); return $this ->fetch();
} public function dbtest()
{
$data = Db::name(&#Introduction to ThinkPHP5 quick start method9;data&#Introduction to ThinkPHP5 quick start method9;)->find(); $this ->assign(&#Introduction to ThinkPHP5 quick start method9;result&#Introduction to ThinkPHP5 quick start method9;, $data ); return $this ->fetch();
}
}
|
Copy after login
Then build a dbtest in the index directory under the view. HTML rendering:
1 2 | <html><head><title></title></head><body>
{ $result .id--- $result .data}</body></html>
|
Copy after login
Then visit
http://localhost/tp5/public/index.php/index/index/dbtest.
This article explains the quick start method of ThinkPHP5. For more related content, please pay attention to the php Chinese website.
Related recommendations:
Introduction to the steps for using ThinkPHP
Unlockable queries
Explain the relevant knowledge of update lock (U) and exclusive lock (X)
The above is the detailed content of Introduction to ThinkPHP5 quick start method. For more information, please follow other related articles on the PHP Chinese website!