Phpthinkphp framework quick start

Source: Internet
Author: User
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:

  1. Class CoomyAction extends Action {
  2. Function index (){
  3. Echo "hello world! ";
  4. }
  5. Function hello (){
  6. Echo "hello php ";
  7. }
  8. }

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:

  1. True, // whether debugging mode is enabled
  2. 'Db _ type' => 'mysql', // Database TYPE
  3. 'Db _ host' => 'localhost', // server address
  4. 'Db _ name' => 'test', // database NAME
  5. 'Db _ user' => 'root', // USER name
  6. 'Db _ pwd' => '', // password
  7. 'Db _ port' => 3306, // PORT
  8. 'Db _ prefix' => '', // database table PREFIX
  9. 'Db _ SUFFIX '=> '', // database table SUFFIX
  10. 'Db _ FIELDTYPE_CHECK '=> false, // whether to check the field type
  11. 'Db _ FIELDS_CACHE '=> true, // enable field caching
  12. 'Db _ charset' => 'utf8', // The database encoding uses utf8 by default.
  13. 'Db _ DEPLOY_TYPE '=> 0, // Database deployment method: 0 centralized (single server), 1 distributed (master/slave server)
  14. 'Db _ RW_SEPARATE '=> false, // whether the master-slave mode is valid for database read/write splitting
  15. );
  16. ?>

The above configuration is made for our project database. with these configurations, we can delete and modify the database in the controller, for example:

  1. M ("tableName")-> add ($ data) // Insert data to the tableName table
  2. M ("tableName")-> save ($ data) // update data to the tableName table
  3. M ("tableName")-> select () // read data from the tableName table
  4. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.