Home > Backend Development > PHP Tutorial > ThinkPHP3.1 Basic Knowledge Quick Start_PHP Tutorial

ThinkPHP3.1 Basic Knowledge Quick Start_PHP Tutorial

WBOY
Release: 2016-07-13 10:24:39
Original
835 people have browsed it

Among the many MVC frameworks today, ThinkPHP is a fast, simple and lightweight PHP development framework based on MVC and object-oriented. It is released under the Apache2 open source protocol. Since its birth, it has been adhering to simple and practical design principles. While maintaining excellent performance and minimal code, it pays special attention to development experience and ease of use, and has many original functions and features, providing strong support for WEB application development. It is the first choice of many PHP developers. This article gives a brief introduction to the basic knowledge of ThinkPHP3.1.

1. Directory structure

The latest version of ThinkPHP can be downloaded from the official website (http://thinkphp.cn/down/framework.html) or Github (https://github.com/liu21st/thinkphp/downloads).
Extract the downloaded compressed file to your WEB directory (or any directory). The directory structure of the framework is:

├─ThinkPHP.php 框架入口文件
 ├─Common 框架公共文件
 ├─Conf 框架配置文件
 ├─Extend 框架扩展目录
 ├─Lang 核心语言包目录
 ├─Lib 核心类库目录
 │ ├─Behavior 核心行为类库
 │ ├─Core 核心基类库
 │ ├─Driver 内置驱动
 │ │ ├─Cache 内置缓存驱动
 │ │ ├─Db 内置数据库驱动
 │ │ ├─TagLib 内置标签驱动
 │ │ └─Template 内置模板引擎驱动
 │ └─Template 内置模板引擎
 └─Tpl 系统模板目录

Copy after login

Note that the public entry file of the framework ThinkPHP.php cannot be executed directly . This file can only be called in the project entry file to run normally (will be discussed later) , this is a mistake that many novices easily make.

2. Entry file

Before you start, you need a web server and PHP running environment. If you don’t have one yet, we recommend using the integrated development environment WAMPServer (it is a development kit that integrates Apache, PHP and MySQL and supports multiple PHP versions. , MySQL version and Apache version switching) to use ThinkPHP for local development and testing.
Next, we first create an app subdirectory under the WEB root directory (this app is our project name), then create an index.php file under the directory and add a simple line of code:

<&#63;php
 require '/ThinkPHP框架所在目录/ThinkPHP.php';

Copy after login

The function of this line of code is to load the entry file ThinkPHP.php of the ThinkPHP framework. This is the first step for all applications developed based on ThinkPHP.
Then, access this entry file in the browser.

http://localhost/app/

Copy after login

The default file of general web servers is index.php, so we don’t need to add index.php to the URL address. After running, we will see the welcome page,

And the project directory has been automatically generated. The directory structure is as follows:

├─index.php 项目入口文件
 ├─Common 项目公共文件目录
 ├─Conf 项目配置目录
 ├─Lang 项目语言目录
 ├─Lib 项目类库目录
 │ ├─Action Action类库目录
 │ ├─Behavior 行为类库目录
 │ ├─Model 模型类库目录
 │ └─Widget Widget类库目录
 ├─Runtime 项目运行时目录
 │ ├─Cache 模板缓存目录
 │ ├─Data 数据缓存目录
 │ ├─Logs 日志文件目录
 │ └─Temp 临时缓存目录
 └─Tpl 项目模板目录
Copy after login

If you want the entry file of the project to be moved outside the app directory, then you only need to modify the content of the entry file index.php to:

<&#63;php
define('APP_NAME','app');
define('APP_PATH','./app/');
 require '/ThinkPHP框架所在目录/ThinkPHP.php';
Copy after login

The APP_NAME and APP_PATH sections are used to define the project name and project directory. The project name usually refers to the directory name of the project.
After moving and modifying the entry file of the project, we can pass

http://localhost/
Copy after login

Accessed the app project. Of course, you can also create multiple subdirectories under the Web root directory to deploy multiple projects.

3. Debug mode

The running modes of ThinkPHP include debugging mode and deployment mode. By default, it runs in deployment mode. In deployment mode, performance is prioritized and as few error messages are thrown as possible. In debugging mode, debugging convenience is prioritized, any cache is turned off, and as many error messages are thrown as possible, so it has a certain impact on performance. The deployment mode adopts the project compilation mechanism. The first run will compile and cache the core and project-related files. Since compilation will affect the effectiveness of configuration files, function files and database modifications during the development process (unless you manually clear the Runtime after modifications) cache file below). Therefore, in order to avoid the above problems, we strongly recommend that novices use debugging mode during development with ThinkPHP, so that they can better obtain error prompts and avoid unnecessary problems and annoyances.
Turning on debugging mode is very simple. We only need to add a line of constant definition code at the beginning of the entry file:

<&#63;php
define('APP_DEBUG',TRUE); // 开启调试模式
 require '/ThinkPHP框架所在目录/ThinkPHP.php';
Copy after login

After the development is completed, when we actually deploy the project, just delete this line of constant definition code, or change it to:

define('APP_DEBUG',false); // 关闭调试模式
Copy after login


4. Configuration

Each project has an independent configuration file (located in Conf/config.php in the project directory). The definition format of the configuration file adopts the method of PHP returning an array, for example:

// 项目配置文件
 return array(
 '配置参数' => '配置值', 
 // 更多配置参数
 //...
 );
Copy after login

Once necessary, we can add relevant configuration items to the project configuration file. Usually when we mention adding configuration items, we mean adding:

in the project configuration file
'配置参数' => '配置值', 

Copy after login

配置值可以支持包括字符串、数字、布尔值和数组在内的数据,通常我们建议配置参数均使用大写定义。如果有需要,我们还可以为项目定义其他的配置文件。

5.控制器

需要为每个模块定义一个控制器类,控制器类的命名规范是:
模块名+Action.class.php (模块名采用驼峰法并且首字母大写)
系统的默认模块是Index,对应的控制器就是项目目录下面的Lib/Action/IndexAction.class.php,类名和文件名一致。默认操作是index,也就是控制器的一个public方法。初次生成项目目录结构的时候,系统已经默认生成了一个默认控制器(就是之前看到的欢迎页面),我们把index方法改成下面的代码:

class IndexAction extends Action {
 public function index(){
 echo 'hello,world!';
 }
 }

Copy after login

控制器必须继承Action类,一个模块可以包括多个操作方法。如果你的操作方法是protected或者private类型的话,是无法直接通过URL访问到该操作的。

6.URL请求

入口文件是项目的单一入口,对项目的所有请求都定向到项目的入口文件,系统会从URL参数中解析当前请求的模块和操作,我们之前访问的URL地址中没有任何参数,因此系统会访问默认模块(Index)的默认操作(index),因此下面的访问和之前是等效的:

http://localhost/app/index.php/Index/index

Copy after login

这种URL模式就是系统默认的PATHINFO模式,不同的URL模式获取模块和操作的方法不同,ThinkPHP支持的URL模式有四种:普通模式、PATHINFO、REWRITE和兼容模式。

普通模式:也就是传统的GET传参方式来指定当前访问的模块和操作,例如:

http://localhost/app/&#63;m=module&a=action&var=value

Copy after login

m参数表示模块,a操作表示操作(模块和操作的URL参数名称是可以配置的),后面的表示其他GET参数。

PATHINFO模式:是系统的默认URL模式,提供了最好的SEO支持,系统内部已经做了环境的兼容处理,所以能够支持大多数的主机环境。对应上面的URL模式,PATHINFO模式下面的URL访问地址是:

http://localhost/app/index.php/module/action/var/value/

Copy after login

PATHINFO地址的第一个参数表示模块,第二个参数表示操作。
PATHINFO模式下面,URL是可定制的,例如,通过下面的配置:

'URL_PATHINFO_DEPR'=>'-', // 更改PATHINFO参数分隔符
Copy after login

我们还可以支持下面的URL访问:

http://localhost/app/index.php/module-action-var-value/

Copy after login

REWRITE模式:是在PATHINFO模式的基础上添加了重写规则的支持,可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则。
如果是Apache则需要在入口文件的同级添加.htaccess文件,内容如下:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
 </IfModule>

Copy after login

接下来,就可以用下面的URL地址访问了:

http://localhost/app/module/action/var/value/

Copy after login

兼容模式:是用于不支持PATHINFO的特殊环境,URL地址是:

http://localhost/app/&#63;s=/module/action/var/value/

Copy after login

兼容模式配合Web服务器重写规则的定义,可以达到和REWRITE模式一样的URL效果。

7.视图

ThinkPHP内置了一个编译型模板引擎,也支持原生的PHP模板,并且还提供了包括Smarty在内的模板引擎驱动。和Smarty不同,ThinkPHP在渲染模板的时候如果不指定模板,则会采用系统默认的定位规则,其定义规范是 Tpl/模块名/操作名.html,所以,Index模块的index操作的默认模板文件位于项目目录下面的Tpl/Index/index.html。
例如:

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

Copy after login

要输出视图,必须在控制器方法中进行模板渲染输出操作,例如:

class IndexAction extends Action {
 public function index(){ 
 $this->name = 'thinkphp'; // 进行模板变量赋值
 $this->display();
 }
 }

Copy after login

display方法中我们没有指定任何模板,所以按照系统默认的规则输出了Index/index.html模板文件。
接下来,我们在浏览器中输入

http://localhost/app/
Copy after login
Copy after login

浏览器中会输出

hello,thinkphp!
Copy after login

8.读取数据

在开始之前,我们首先在数据库thinkphp中创建一个think_data数据表(以mysql数据库为例):

CREATE TABLE IF NOT EXISTS `think_data` (
 `id` int(8) unsigned NOT NULL AUTO_INCREMENT,
 `data` varchar(255) NOT NULL,
 PRIMARY KEY (`id`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
INSERT INTO `think_data` (`id`, `data`) VALUES
 (1, 'thinkphp'),
 (2, 'php'),
 (3, 'framework');

Copy after login

如果我们需要读取数据库中的数据,就需要在项目配置文件中添加数据库连接信息如下:

// 添加数据库配置信息
 'DB_TYPE' => 'mysql', // 数据库类型
 'DB_HOST' => 'localhost', // 服务器地址
 'DB_NAME' => 'thinkphp', // 数据库名
 'DB_USER' => 'root', // 用户名
 'DB_PWD' => '', // 密码
 'DB_PORT' => 3306, // 端口
 'DB_PREFIX' => 'think_', // 数据库表前缀

Copy after login

或者采用如下配置

'DB_DSN' => 'mysql://root@localhost:3306/thinkphp'
Copy after login

使用DB_DSN方式定义可以简化配置参数,DSN参数格式为:
数据库类型://用户名:密码@数据库地址:数据库端口/数据库名
如果两种配置参数同时存在的话,DB_DSN配置参数优先。

接下来,我们修改下控制器方法,添加读取数据的代码:

class IndexAction extends Action {
 public function index(){
 $Data = M('Data'); // 实例化Data数据模型
 $this->data = $Data->select();
 $this->display();
 }
 }

Copy after login

这里用到了M函数,是ThinkPHP内置的实例化模型的方法,而且用M方法实例化模型不需要创建对应的模型类,你可以理解为M方法是直接在操作底层的Model类,而Model类具备基本的CURD操作方法。
M('Data') 实例化后,就可以对think_data数据表(think_ 是我们在项目配置文件中定义的数据表前缀)进行操作(包括CURD)了,M函数的用法还有很多,我们以后会深入了解。
定义好控制器后,我们修改模板文件,添加数据输出标签如下:

<html>
 <head>
 <title>Select Data</title>
 </head>
 <body>
 <volist name="data" id="vo">
 {$vo.id}--{$vo.data}<br/>
 </volist>
 </body>
 </html>

Copy after login

volist标签是内置模板引擎用于输出数据集的标签。{$vo.id} 和 {$vo.data} 的用法和Smarty类似,就是用于输出数据的字段,这里就表示输出think_data表的id和data字段的值。
我们访问

http://localhost/app/
Copy after login
Copy after login

会输出

1--thinkphp
2--php
3--framework
Copy after login

如果发生错误,检查你是否开启了调试模式或者清空Runtime目录下面的缓存文件。
如果你看到了上面的输出结果,那么至此你已经掌握了ThinkPHP入门的基础知识!

总结:

本篇我们学习了ThinkPHP的目录结构、URL模式,如何创建项目的入口文件和开启调试模式,以及控制器、模板和模型的基础认识。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/825385.htmlTechArticle在当今众多的MVC框架中,ThinkPHP是一个快速、简单的基于MVC和面向对象的轻量级PHP开发框架,其遵循Apache2开源协议发布,自从诞生以来一直...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template