This article briefly introduces how to create a thinkphp-based project from scratch, configure the project, and introduce some basic principles, you can download the core package from the official thinkphp website.
This article briefly introduces how to create a thinkphp-based project from scratch, configure the project, and introduce some basic principles, you can go to the thinkphp official website to download the core package or complete package and help document for details.
In this project, we can directly download the core package and decompress it to any location. as long as our project can be accessed, assume that our project is in the coomy folder, the core folder name of the statistics directory is thinkphp.
Portal file
The PHP file accessed during Project Access for the first time. before the project starts, you can create a php file anywhere, introduce the thinkphp core file into the file, and automatically generate the project directory, after accessing the entry file in the browser, the "Hello!" dialog box appears! Welcome to thinkPHP !" The following example creates the create. php file in the coomy folder and adds the following code:
Require '../thinkphp/ThinkPHP. php ';
App: run ();
After accessing http: // localhost/coomy/create. php in the browser, the following folder is generated in the coomy Directory:
| -- Common public function area
| -- Conf configuration | -- Lang language pack | -- Lib | -- Action Controller | -- Model | -- Runtime cache, etc. | -- Tpl Template
The controller is used to receive user input and call models and views to fulfill user requirements. The model defines the database-related business logic of a website. this is the basic concept of the MVC framework, I will not introduce it too much here.
URL access description
Http: // localhost/index. php/ActionName/FunctionName
The above URL indicates accessing the FunctionName function under the ActionName controller of the project, then you only need to create the controller.
Create a controller:
Define class files-define class methods, storage location Lib/Action.
Naming rules:
Controller name + Action. class. php
Create a CoomyAction. class. php file in the Lib/Action folder and open the file to create the controller:
- Class CoomyAction extends Action {
- Function index (){
- Echo "hello world! ";
- }
- Function hello (){
- Echo "hello php ";
- }
- }
After the creation, if you want to output "hello php", you can use localhost/index. php/coomy/hello. if you want to output "hello world !" You can directly use localhost/index. php/coomy, because the index function is default.
Note:Index. php must be an entry file and can be named freely.
Project configuration file
The default values of all thinkphp configuration items are in the Common/convention of the core code. php, and if we want to customize, we can find the config in the Conf folder in our project directory. php, and configure our project in this file, such as database connection configuration:
-
- True, // whether debugging mode is enabled
- 'Db _ type' => 'mysql', // Database TYPE
- 'Db _ host' => 'localhost', // server address
- 'Db _ name' => 'test', // database NAME
- 'Db _ user' => 'root', // USER name
- 'Db _ pwd' => '', // password
- 'Db _ port' => 3306, // PORT
- 'Db _ prefix' => '', // database table PREFIX
- 'Db _ SUFFIX '=> '', // database table SUFFIX
- 'Db _ FIELDTYPE_CHECK '=> false, // whether to check the field type
- 'Db _ FIELDS_CACHE '=> true, // enable field caching
- 'Db _ charset' => 'utf8', // The database encoding uses utf8 by default.
- 'Db _ DEPLOY_TYPE '=> 0, // Database deployment method: 0 centralized (single server), 1 distributed (master/slave server)
- 'Db _ RW_SEPARATE '=> false, // whether the master-slave mode is valid for database read/write splitting
- );
- ?>
The above configuration is made for our project database. with these configurations, we can delete and modify the database in the controller, for example:
- M ("tableName")-> add ($ data) // Insert data to the tableName table
- M ("tableName")-> save ($ data) // update data to the tableName table
- M ("tableName")-> select () // read data from the tableName table
- M ("tableName")-> delete ($ id) // delete data with id = $ id from the tableName table
Use Template
After talking for a long time, they all interact with the database. this is generally the background code. Where can I write the front-end code? Of course, in the template, the template separates the UI of a page from the response handler so that they can do their own thing. this is also the essence of MVC. in ThinkPHP, the template and controller must correspond one to one.
Storage location:The template is stored in the Tpl/default directory;
Naming rules (corresponding): create a folder with the same name as the controller, and create an html file with the same name as the controller function;
Localhost/Lib/Action/TestAction. class. php (function myTestFun ..)
Localhost/Tpl/test/mytestfun.html
Template:Call the display () method in any function of the controller to access the function display template through url. in this way, we can also use the template function, the data is processed or the template output is controlled.
In addition, thinkphp also has a built-in template engine that supports many template tags, such as variable tags. we can use tags similar to {$ attriName} anywhere in an HTML file, to get the value from the backend controller, you need to assign this variable in the controller. The simplest way is to assign the value through the attribute:
1 $ this-> attriName = "value ";
In this way, the template engine outputs attriName through the variables, so that the template is responsible for displaying the variables, and the controller is responsible for processing. we can start our development through thinkphp.