ThinkPHP3.1 quick start (1) basics. Introduction ThinkPHP is a fast and simple lightweight PHP development framework based on MVC and object-oriented. it complies with the Apache2 open source protocol and has been adhering to the concise and practical design principles since its birth.
ThinkPHP is a fast and simple lightweight PHP development framework based on MVC and object-oriented. it complies with the Apache2 open source protocol and has been adhering to simple and practical design principles since its birth, while maintaining excellent performance and simplified code, the system pays special attention to the development experience and ease of use. it also has many original functions and features, providing powerful support for WEB application development.
Directory structure
The latest ThinkPHP version can be downloaded from the official website (http://thinkphp1.cn/down/framework.html) or Github (https://github.com/liu21st/thinkphp/downloads.
Decompress the downloaded compressed file to your WEB Directory (or any directory). the directory structure of the framework is as follows:
Javas── ThinkPHP. php framework entry file
Common‑common framework public file
├-Conf framework configuration file
Extended catalog
├ ── Lang core language pack Directory
├ ── Lib core class library directory
│ Descri── Behavior core Behavior class library
│ ├-Core Base Class Library
│ ├ ── Driver built-in Driver
│ ─ ── Cache built-in Cache driver
│ ─ ── Db built-in database driver
│ ─ ── TagLib built-in label driver
│ └ ── Template built-in Template engine driver
│ └ ── Template built-in Template engine
└ ── Tpl system Template directory
Note: The public portal file of the framework is ThinkPHP. php cannot be executed directly. This file can only be called in the project entry file to run properly (as will be discussed later). This is a very easy mistake for many new users.
Portal file
Before you start, you need a Web server and PHP runtime environment, we recommend that you use 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, create an app subdirectory under the WEB root directory (this app is our project name), create an index. php file under the Directory, and add a simple line of code:
Require '/ThinkPHP framework directory/ThinkPHP. php ';
The role of this line of code is to load ThinkPHP framework's entry file ThinkPHP. php, which is the first step for all applications developed based on ThinkPHP.
Then, access the entry file in the browser.
Http: // localhost/app/
The default file of the Web server is index. php. Therefore, you do not need to add index. php to the URL. After running, we will see the welcome page,
The project directory has been automatically generated. the directory structure is as follows:
─ ── Index. php project portal 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 │ Runtime-Model class library directory │ Runtime-Widget class library directory Runtime-Runtime Project Directory │ Runtime-Cache template Cache directory │ Runtime-Data Cache directory │ Runtime -Logs log file directory │ └ ── Temp temporary cache directory └ ── Tpl project template directory
If you want the project's entry file to be moved out of the app directory, you only need to modify the index. php content of the entry file:
Define ('app _ name', 'app ');
Define ('app _ path', './APP /');
Require '/ThinkPHP framework directory/ThinkPHP. php ';
APP_NAME and APP_PATH are used to define the project name and project directory. the project name usually refers to the project directory name.
After moving and modifying the project's entry file, we can
Http: // localhost/
Access the app project. Of course, you can also create multiple subdirectories under the Web root directory to deploy multiple projects.
Debugging Mode
The running modes of ThinkPHP include the debugging and deployment modes. by default, ThinkPHP runs in the deployment mode. In deployment mode, the performance is preferred, and as few error messages as possible are thrown. in debugging mode, the priority is given to debugging convenience, and any cache is disabled, and as many error messages as possible are thrown, therefore, it has a certain impact on performance. The deployment mode adopts the project compilation mechanism. during the first operation, the core and project-related files are compiled and cached, after compilation, the configuration files, function files, and database modifications in the development process will be affected (unless you manually clear the cached files under Runtime ). Therefore, to avoid the above problems, we strongly recommend that new users use the debugging mode during ThinkPHP development, so that they can better get error prompts and avoid unnecessary problems and troubles.
To enable the debugging mode, you only need to add a constant definition code at the beginning of the entry file:
After the development is complete, you can delete the constant definition code or change it:
Define ('app _ debug', false); // disable the debugging mode.
Configuration
Each project has an independent configuration file (Conf/config. php in the project directory). The definition format of the configuration file adopts the PHP return array method, for example:
// Project configuration file
Return array ('configuration parameters' => 'configuration value', // More configuration parameters //...);
Once necessary, we can add related configuration items in the project configuration file. The Add configuration item we mentioned usually refers to adding it to the project configuration file:
'Configuration parameter '=> 'configuration value ',
Configuration values can support data including strings, numbers, boolean values, and arrays. generally, we recommend that you set parameters in uppercase. If necessary, we can also define other configuration files for the project.
Controller
A controller class needs to be defined for each module. the naming rules for the controller class are as follows:
Module name + Action. class. php (the module name adopts the hump method and the first letter is capitalized)
The default module of the system 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 (that is, the welcome page we saw earlier). we will change the index method to the following code:
class IndexAction extends Action { public function index(){ echo 'hello,world!'; } }
The controller must inherit the Action Class. a module can contain multiple operation methods. If your operation method is protected or private, you cannot directly access the operation through the URL.
URL request
The portal file is a single portal of the project. all requests to the project are directed to the portal file of the project. The system will parse the modules and operations of the current request from the URL parameter, the previously Accessed URL address does not contain any parameters. Therefore, the system will access the default Index operation of the default module. Therefore, the following access is equivalent to the previous one:
Http: // localhost/app/index. php/Index/index
This URL mode is the default PATHINFO mode. different URL modes have different retrieval modules and operation methods. ThinkPHP supports four URL modes: common mode, PATHINFO, REWRITE, and compatibility mode.
Normal mode: the traditional GET parameter passing method is used to specify the currently accessed modules and operations, for example:
Http: // localhost/app /? M = module & a = action & var = value
M parameter indicates the module, a operation indicates the operation (the module and the URL parameter name of the operation can be configured), and other GET parameters are described later.
PATHINFO mode: it is the default URL mode of the system and provides the best SEO support. The system has been compatible with the environment, so it can support most host environments. Corresponding to the above URL mode, the URL access address in PATHINFO mode is:
Http: // localhost/app/index. php/module/action/var/value/
The first parameter of the PATHINFO address indicates the module, and the second parameter indicates the operation.
In the PATHINFO mode, URLs can be customized. for example, you can use the following configuration:
'URL _ PATHINFO_DEPR '=>'-', // modify the PATHINFO parameter delimiter
We can also support the following URL access:
Http: // localhost/app/index. php/module-action-var-value/
REWRITE mode: This mode adds support for REWRITE rules based on the PATHINFO mode. you can remove the entry file index. php in the URL address, but you need to configure additional REWRITE rules for the WEB server.
For Apache, you must add the. htaccess file at the same level as the entry file. the content is as follows:
RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
Next, you can use the following URL to access:
Http: // localhost/app/module/action/var/value/
Compatibility Mode: This mode is used in special environments that do not support PATHINFO. the URL address is:
Http: // localhost/app /? S =/module/action/var/value/
The compatibility mode works with the Web server REWRITE rule definition to achieve the same URL effect as the REWRITE mode.
View
ThinkPHP has a built-in compiled template engine and supports native PHP templates. It also provides template engine drivers including Smarty. Unlike Smarty, if ThinkPHP does not specify a template when rendering the template, it will adopt the default positioning rules of the system. The definition specification is Tpl/module name/operation name .html. therefore, the default template file for Index operations of the index module is located in Tpl/Index/index.html under the Project Directory.
For example:
hello {$name} hello, {$name}!
To output a view, you must perform the template rendering and output operations in the controller method. for example:
Class IndexAction extends Action {
Public function index (){
$ This-> name = 'thinkphp'; // assign values to template variables
$ This-> display ();
}
}
No template is specified in the display method, so the Index/index.html template file is output according to the default system rules.
Next, enter
Http: // localhost/app/
Output in the browser
Hello, thinkphp!
Read Data
Before starting, create a think_data table in the database thinkphp (take mysql database as an example ):
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');
To read data from the database, add the following database connection information to the project configuration file:
// Add database configuration information
'Db _ type' => 'mysql', // Database TYPE
'Db _ host' => 'localhost', // server address
'Db _ name' => 'thinkphp', // database NAME
'Db _ user' => 'root', // USER name
'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'
You can use DB_DSN to define configuration parameters. the DSN parameter format is as follows:
Database type: // user name: password @ database address: database port/database name
If both configuration parameters exist, DB_DSN configuration parameters take precedence.
Next, modify the controller method and add the code for reading data:
Class IndexAction extends Action {public function index () {$ Data = M ('data '); // instantiate the Data data model $ this-> Data = $ Data-> select (); $ this-> display ();}}
The M function is used here as the built-in instantiation model method of ThinkPHP, and the corresponding model class does not need to be created to instantiate the model using the M method, you can understand that the M method operates the underlying Model class directly, and the Model class has the basic CURD operation method.
After M ('data') is instantiated, you can perform operations (including CURD) on the think_data table (think _ is the Data table prefix defined in the project configuration file, the usage of M functions is much more advanced.
After the controller is defined, modify the template file and add the data output tag as follows:
Select Data
{$vo.id}--{$vo.data}
The volist tag is the tag used by the built-in template engine to output the dataset. The usage of {$ vo. id} and {$ vo. data} is similar to that of Smarty. it is the field used to output data. here, it indicates the id of the output think_data table and the value of the data field.
We access
Http: // localhost/app/
Output
1 -- thinkphp
2 -- php
3 -- framework
If an error occurs, check whether debugging mode is enabled or the cached files under the Runtime directory are cleared.
If you see the output above, congratulations! you have obtained the ThinkPHP key!
Summary
In this article, we learned about ThinkPHP's directory structure and URL mode, how to create the project's entry file and enable the debugging mode, as well as the basic understanding of controllers, templates, and models, we will continue to learn about the data CURD operations later.
ThinkPHP is a fast and simple lightweight PHP development framework based on MVC and object-oriented. it complies with the Apache2 open source protocol and has been adhering to simple and practical design principles since its birth...