ThinkPHP3.1 Quick Start (1) Basics
Introduction
ThinkPHP is a fast and simple lightweight PHP development framework based on MVC and object-oriented. It is released under the Apache2 open source license. It has been adhering to simple and practical design principles since its inception, while maintaining excellent performance and simplicity. While coding, 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.
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 Framework entry file
├─Common framework public file
├─ Conf framework configuration file
├─Extend framework extension directory
├─Lang core language package directory
├─Lib core class library directory
│ ├─Behavior core behavior class library
│ ├─Core core base class library
│ ├─Driver built-in driver
│ │ ├─Cache built-in cache driver
│ │ ├─Db built-in database driver
│ │ ├─TagLib built-in tag driver
│ │ └─Template built-in template engine driver
│ └─Template built-in template engine
└─Tpl system template directory
Note that the framework’s public entry file ThinkPHP.php cannot be executed directly. This file can only be called in the project entry file to run normally (will be discussed later) Speaking of), this is a mistake that many novices make easily.
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 (a development kit that integrates Apache, PHP and MySQL, and Supports switching between multiple PHP versions, MySQL versions and Apache versions) 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:
require '/Directory where the ThinkPHP framework is located/ThinkPHP.php';
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/
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,
automatically generated the project directory, the directory structure is as follows:
├─index.php Project entry file
├─Common project public file directory
├─Conf project configuration Directory
├─Lang project language directory
├─Lib project class library directory
│ ├─Action Action class library directory
│ ├─Behavior behavior class library directory
│ ├─Model model class library directory
│ └─Widget Widget class library directory
├─Runtime project runtime directory
│ ├─Cache template cache directory
│ ├─Data data cache directory
│ ├─Logs log file directory
│ └─Temp Temporary cache directory
└─Tpl project template directory
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:
define( 'APP_NAME','app');
define('APP_PATH','./app/');
require '/ThinkPHP framework directory/ThinkPHP.php';
APP_NAME and APP_PATH sections are used to define Project name and project directory. The project name usually refers to the directory name of the project.
After moving and modifying the project’s entry file, we can access the app project through
http://localhost/
. Of course, you can also create multiple subdirectories under the Web root directory to deploy multiple projects.
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:
// Project configuration file
return array( 'Configuration parameters' = & gt; 'configuration value',
// more configuration parameters
//...);
Once needed, we can add related configuration to the project configuration file to project. Usually when we mention adding configuration items, we mean adding in the project configuration file:
'Configuration Parameters' => 'Configuration Value',
Configuration values can support strings, numbers, Boolean values and arrays. For data, we generally recommend that configuration parameters be defined in uppercase letters. If necessary, we can also define other configuration files for the project.
Controller
You need to define a controller class for each module. The naming convention of the controller class is: module name + Action.class.php (the module name uses camel case and the first letter is capitalized)
The system's default The module is Index, and the corresponding controller is Lib/Action/IndexAction.class.php under the project directory. The class name and file name are consistent. The default operation is index, which is a public method of the controller. When the project directory structure is generated for the first time, the system has generated a default controller by default (the welcome page we saw before). We change the index method to the following code: class IndexAction extends Action {
public function index( ){
echo 'hello, world!';
off out off out off's ('''' out out through out's's right back through out through right back's to right back to right back‐'s way right‐‐‐‐‐‐‐'‐ way out out right's back to back) }
If your operation method is of protected or private type, the operation cannot be accessed directly through the URL.
URL request
The entry file is the single entry point for the project. All requests to the project are directed to the entry file of the project. The system will parse the currently requested module and operation from the URL parameters. The URL address we visited before There are no parameters, so the system will access the default operation (index) of the default module (Index), so the following access is equivalent to the previous one:
http://localhost/app/index.php/Index/index
This URL mode is the system's default PATHINFO mode. Different URL modes have different methods of obtaining modules and operations. ThinkPHP supports four URL modes: normal mode, PATHINFO, REWRITE and compatibility mode. Normal mode: that is, the traditional GET parameter passing method to specify the currently accessed module and operation, for example:
http://localhost/app/?m=module&a=action&var=value
m parameter represents the module, and a operation represents Operation (the URL parameter names of the module and operation are configurable), and the following ones represent other GET parameters. PATHINFO mode: It is the default URL mode of the system, providing the best SEO support. The system has already done environmental compatibility processing internally, so it can support most host environments. Corresponding to the above URL pattern, the URL access address under the PATHINFO pattern is:
http://localhost/app/index.php/module/action/var/value/
The first parameter of the PATHINFO address represents the module, and the first parameter represents the module. The two parameters represent the operation.
Under PATHINFO mode, the URL is customizable, for example, through the following configuration: 'URL_PATHINFO_DEPR'=>'-', // Change the PATHINFO parameter separator We can also support the following URL access:
http://localhost/app/index.php/module-action-var-value/
REWRITE mode: Based on the PATHINFO mode, support for rewriting rules is added, and the entry file index in the URL address can be removed. php, but additional configuration of the WEB server's rewrite rules is required.
If it is Apache, you need to add the .htaccess file at the same level as the entry file, with the following content:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
Next, you can access it with the following URL address:
http:// localhost/app/module/action/var/value/
Compatibility mode: It is used in special environments that do not support PATHINFO. The URL address is:
http://localhost/app/?s=/module/action/var /value/
Compatibility mode cooperates with the definition of web server rewrite rules to achieve the same URL effect as REWRITE mode.
View
ThinkPHP has a built-in compiled template engine, which also supports native PHP templates, and also provides template engine drivers including Smarty. Different from Smarty, if ThinkPHP does not specify a template when rendering a template, it will use the system's default positioning rules. Its definition specification is Tpl/module name/operation name.html. Therefore, the default template file for the index operation of the Index module is located Tpl/Index/index.html under the project directory.
For example:
To output the view, the template rendering output operation must be performed in the controller method, for example:
class IndexAction extends Action {
public function index(){
= 'thinkphp'; // Assign template variables
through using through ‐ ‐ ‐ ‐ off out out of 100% to 100% } } .html template file.
Next, we enter http://localhost/app/The browser will outputhello, thinkphp!
Read the data
Before we start, we first thinkphp in the database Create a think_data data table in (taking mysql database as an example):
CREATE TABLE IF NOT EXISTS `think_data` (
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
INSERT INTO `think_data` (`id`, `data`) VALUES
(1, 'thinkphp'),
(2, 'php'),
(3, 'framework');
If we need to read data from the database, we need to add the database connection information to the project configuration file as follows:
//Add database configuration Information
'DB_TYPE' => 'mysql', // Database type
'DB_HOST' => 'localhost', // Server address
'DB_NAME' => 'thinkphp', // Database name
'DB_USER' => 'root', // Username
'DB_PWD' => '', // Password
'DB_PORT' => 3306, // Port
'DB_PREFIX' => 'think_', // Database table prefix
or use the following configuration
'DB_DSN' => 'mysql://root@localhost:3306/thinkphp'
Using DB_DSN mode definition can simplify configuration parameters, DSN parameter format It is: database type://username:password@database address:database port/database name. If both configuration parameters exist at the same time, the DB_DSN configuration parameter takes precedence.
Next, we modify the controller method and add the code to read the data:
class IndexAction extends Action {
public function index(){
$Data = M('Data'); // Instantiation Data data model
using use using M function, using ’ ’s ’ using ’s ’s ’ using ’s ’ s ‐ ‐ ‐ Method to instantiate a model, and using the M method to instantiate a model does not require the creation of a corresponding model class. You can understand that the M method directly operates the underlying Model class, and the Model class has basic CURD operation methods.
M('Data') After instantiation, you can operate (including CURD) on the think_data data table (think_ is the data table prefix we defined in the project configuration file). There are many uses of the M function. We will Will learn more about it.After defining the controller, we modify the template file and add the data output tag as follows:
{$vo.id}--{$vo.data}
> ;
volist tag is a tag used by the built-in template engine to output the data set. The usage of {$vo.id} and {$vo.data} is similar to that of Smarty. They are fields used to output data. Here, it means outputting the values of the id and data fields of the think_data table.
When we visit
Summary
In this article, we learned about ThinkPHP’s directory structure, URL pattern, and how to create project entry files, controllers, and templates. We will continue to learn about CURD operations on data later.
The above is the basic content of ThinkPHP3.1 Quick Start (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Quick Start: How to install the pandas library in Python requires specific code examples 1. Overview Python is a widely used programming language with a powerful development ecosystem that includes many practical libraries. Pandas is one of the most popular data analysis libraries. It provides efficient data structures and data analysis tools, making data processing and analysis easier. This article will introduce how to install the pandas library in Python and provide corresponding code examples. 2. Install Py

We start this series by learning how to animate HTML elements using mojs. In this second tutorial, we continue using the Shape module to animate built-in SVG shapes. The third tutorial covers more ways to animate SVG shapes using ShapeSwirl and the stagger module. Now we will learn how to animate different SVG shapes in bursts using the Burst module. This tutorial will depend on the concepts we introduced in the previous three tutorials. If you haven't read them yet, I recommend reading them first. Creating a Basic Burst Animation The first thing we need to do before creating any burst animation is to instantiate a Burst object. Afterwards, we can specify different properties

Quick Start: Implementing a Simple Audio Streaming Service Using Go Language Functions Introduction: Audio streaming services are becoming more and more popular in today's digital world, which allow us to play audio files directly over the network without performing a complete download. This article will introduce how to use Go language functions to quickly implement a simple audio streaming service so that you can better understand and use this function. Step 1: Preparation First, you need to install the Go language development environment. You can download it from the official website (https://golan

Quick Start: Use Go language functions to implement simple image recognition functions In today's technological development, image recognition technology has become a hot topic. As a fast and efficient programming language, Go language has the ability to implement image recognition functions. This article will provide readers with a quick start guide by using Go language functions to implement simple image recognition functions. First, we need to install the Go language development environment. You can download the appropriate version on the Go language official website (https://golang.org/)

Title: Get Started Quickly: Recommended Five Common Go Language Frameworks In recent years, with the popularity of the Go language, more and more developers have chosen to use Go for project development. The Go language has received widespread attention for its efficiency, simplicity and superior performance. In Go language development, choosing a suitable framework can improve development efficiency and code quality. This article will introduce five commonly used frameworks in the Go language, and attach code examples to help readers get started quickly. Gin framework Gin is a lightweight web framework that is fast and efficient.

Quick Start: A Guide to Using Five Kafka Visualization Tools 1. Kafka Monitoring Tools: Introduction Apache Kafka is a distributed publish-subscribe messaging system that can handle large amounts of data and provide high throughput and low latency. Due to the complexity of Kafka, visualization tools are needed to help monitor and manage Kafka clusters. 2.Kafka visualization tools: five major choices KafkaManager: KafkaManager is an open source web community

Quick Start: Use Go language functions to implement simple data aggregation functions. In software development, we often encounter situations where we need to aggregate a set of data. Aggregation operations can count, summarize, calculate, etc., to analyze and display data. In the Go language, we can use functions to implement simple data aggregation functions. First, we need to define a data type to represent the data we want to aggregate. Suppose we have a student's grade table, and each student has two fields: name and grade, then we can create the following structure

Quick Start: Implementing a Simple Video Streaming Service Using Go Language Functions Introduction: Video streaming services play an important role in modern applications. This article will introduce how to use Go language functions to implement a simple video streaming service. We will use the net/http package of Go language to handle HTTP requests, and combine it with the FFmpeg library to handle the encoding and decoding of video streams. Step 1: Install FFmpeg Before we start writing code, we need to install the FFmpeg library. Can be accessed through FFmpeg official website
