Home >php教程 >PHP开发 >ThinkPHP3.1 Quick Start (1) Basics

ThinkPHP3.1 Quick Start (1) Basics

黄舟
黄舟Original
2016-12-20 11:29:271176browse

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,

ThinkPHP3.1 Quick Start (1) Basics

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:

RewriteEngine on


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 output

hello, 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,

`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');

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:



Select Data

{$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

http://localhost/app/

it will output

1--thinkphp

2--php

3--framework

If you see the above output, congratulations! Got the key to getting started with ThinkPHP!

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)!


Statement:
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