Use ThinkPHP framework to quickly develop multiple websites

Source: Internet
Author: User
: This article mainly introduces how to use ThinkPHP framework to quickly develop multiple websites. For more information about PHP tutorials, see. Good articles

Use ThinkPHP framework to quickly build a website

This week, I have been busy with lab websites, and the basic functions have been completed. I have learned about the ThinkPHP framework. Write something to commemorate it. If you are a newbie in the Web field, you can do a little better.

In the past, I used PHP to make a very poor website. why do I say this? it's all about dead code. After that, I felt very tired. I had to deal with p + css at the front end and php and mysql at the js backend. it was a headache to do so many things. Therefore, after receiving a website task, I first thought that I must use the development framework to do it. it must not be as tired as before.

I chose the ThinkPHP framework of PHP. To be honest, it's really good. I instantly felt that Web development was quite efficient.

Hyperlink: ThinkPHP

According to the routine, first paste the experiment environment:

  • 1. WAMP (the kind of integration, which is the easiest and almost does not need to be configured)
  • 2. ZendStudio7.2)
  • 3. ThinkPHP framework + Baidu UEditor plug-in
  • 4. Win7-64bit operating system
  • 5. browsers (Firefox, IE, Chrome), and Firefox FireBug plug-ins for debugging and stealing styles




-------------------------------------------------------------




Step 1: Find a webpage template (no artist, PS, sorry ..)

I am looking for a university lab on the front-end page. I will not post it here, which is very common. The template directly downloaded in the background feels pretty, as shown in the following figure:



Step 2: Get to know Baidu UEditor plug-in

Because I want to do a Lab website. The main content of the lab website is as follows:

  • Introducing lab members
  • Lab projects and achievements
  • Lab News and academic exchange information

It can be seen that the information mainly focuses on the publishing of background news and articles, while there is almost no editing at the front-end. Therefore, we should focus on implementing a convenient article publishing system in the background.

I chose Baidu's UEditor WYSIWYG editor. It is mainly implemented by JS. after integration into the background, there are many features:



For more information about how to integrate UEditor into your website, see the UEditor official website, which provides detailed tutorials.



Step 3: first understand the ThinkPHP framework

Next we will introduce the ThinkPHP framework. First, we will take a look at the directory structure of the ThinkPHP project to enhance our perceptual knowledge:



Folder:

  • Admin is the background project folder
  • Home is a front-end project
  • Public is used to store CSS files, JS files, and images on webpages.
  • ThinkPHP framework
  • Ueditor is a Baidu editor.

The following three php files:

  • Admin. php is the portal file of the background project,
  • Index. php is the entrance file of the foreground project.
  • Config. inc. php, because almost all the data of this small project is stored in the database, and the front and back of the project are connected to the database, all the database configuration code is put in it.

The remaining buildpath,. settings, and. project are generated by Zend IDE regardless of them.


------------------------------------------------------ Gorgeous split -------------------------------------------------------------



Because I actually have two projects (home and admin) here, as long as I understand one of them, I will only introduce the background, that is, the admin section.


Next, let's take a look at the admin. php file. this is the first file to be understood, that is, the portal file in the background.




Several macro definitions indicate:

  • 1. ThinkPHP framework path
  • 2. background application directory
  • 3. background application name
  • 4. enable debugging mode
  • 5. include the ThinkPHP. php file under the ThinkPHP framework (important !)

Create a small project to demonstrate the development process:

For example, if the project's big Directory is xxx, copy the ThinkPHP framework and add the admin. php file (such as the code in)



Enter http: // localhost/xxx/admin. php in the browser to automatically generate the admin directory, as shown in figure



Open the admin directory with the following content:



There are four main folders:

1. Conf stores the project configuration file (for example, defining some constants or something)

2. Lib (most important! Store model classes and controller classes in MVC mode)

3. Tpl (store the template file. in principle, the template we downloaded should be placed here)

4. Runtime (stores Runtime files, such as cache and database table cache)


Finally, paste the running result on the browser .. Sorry, it's a bit late.




If you can see the above picture, it means ThinkPHP can run normally. Let's continue to improve it.


-------------------------------------------------------------- Gorgeous split -----------------------------------------------------


Step 4: Get started with the MVC model


Here, I have to mention the MVC concept in ThinkPHP. MVC is famous for its name. it is called the Model-View-Controller mode.

In ThinkPHP:

A Model can be considered as a database table. For my project:

For an article (news), its attributes include the ID number, title, author, creation time, last modification time, and article content. This is the model of an article. Then, follow these steps to create the corresponding database table:





A View can be viewed as a template or skin.

For example, the backend template posted above is a view in the ThinkPHP concept.

A controller (Action) can be considered as a bridge between a view and a model. Because the content displayed on the website basically comes from a large number of models (database tables), the controller is responsible for deciding under which situations to display the data in which models. In my example, the controller corresponding to the homepage of my background project is the Index controller. (The Index controller is the default controller for all projects ). However, you may think that the home page of a website usually has many buttons, hyperlinks, or something that can be jumped to other places. Pasting:




This is my background homepage. You can see that there is a navigation bar on the left, and the current article information of the database is listed on the lower right. you can see three articles in total. For example, I may want:


1. delete the article "1111 test article publishing system"

2. change the article "James holds 3rd MVP trophies" to "James holds 4th MVP trophies"

3. add another article


This corresponds to different functions of the Index controller, that is, different methods of the Index controller class IndexAction (such as the edit () method, delete () method, and add () method ). Of course, the page is displayed because the Index () method of the Index controller is executed by default. In this method, the display () method is called to display the template (view ). If you don't believe it, you can check the default file. The Index method of the Index controller in the file is implemented on the welcome page of HelloThinkPHP.


For example:

The IndexAction directory is:

/Xxx/admin/Lib/Action/IndexAction. class. php

Content:



We can see that $ this-> display () is called in the index method by default ()

(Note that IndexAction inherits the Action Class. the display method is the Action class method );

After display(, thinkphp will find the index.html file under the indexfile under the tplfile folder of the project. The first indexfolder corresponds to the indexcontroller, and index.html corresponds to the index () method.

Therefore, a controller class corresponds to a template folder. The specific number of templates is related to the number of methods in the controller class and the number of methods that need to be displayed.

Therefore, the corresponding template file path is:

/Xxx/admin/Tpl/Index/index.html


A careful friend may ask, here you only talk about Views (templates) and controllers. what about models? How do you know the information of the three articles in the database? Otherwise, I have done some things in the index method, which is the lite version of the index method:



As you can see, the first step is to instantiate a model. The model name is Article. As mentioned earlier, the model is a database table. check which database tables are available:



The first table name is think_article. we can see that there is still a different prefix think _, which is actually specified in the configuration file. do you still remember the config. inc. php mentioned above? The configuration code is as follows:


Return array (

'Db _ type' => 'mysql ',

'Db _ host' => 'localhost ',

'Db _ name' => database NAME,

'Db _ user' => USER name,

'Db _ pwd' => password,

'Db _ port' => '123 ',

'Db _ prefix' => 'think _',

);

?>


The second statement is to query the database. ThinkPHP provides many ways to query databases. I use the consistent operation method.

After the second statement is executed, the $ new_list variable stores the information of all articles. how can we display it on the interface?


In the third sentence, it assigns the variable to a variable named 'New _ list' (as if it had the same name .. But it doesn't matter. The key is the assign method). then we can replace it in the template file. By default, it is okay to write {$ new_list} in HTML. Of course, new_list is a composite variable, not simply a number or string .. However, ThinkPHP provides many loop methods for us to use, which is very convenient.


The last statement is to display the corresponding view file. We can display the document information in the database in the browser according to the rules defined in the view (template.

-----------------------------------------------------------

Source code download

Finally, the source code of the sample project xxx is provided. Users who need ThinkPHP can download ThinkPHP and quickly understand the principle of ThinkPHP.

Source code function:

  • 1. log on to the background administrator
  • 2. add, edit, and delete an article
  • 3. front-end posts


Usage:

  • 1. decompress the package to the root directory of the website, which is the xxx folder under the www folder by default:


  • 2. create a new database in MySQL, such as rubydb, organized into a utf8-genaral-ci


  • 3. import the two database tables think_article and think_user in the database table folder. import the following data:


  • 4. configure the Config. inc. php file

[Php] view plaincopy

  1. "Font-size: 16px;">
  2. Return array (
  3. 'Db _ type' => 'mysql ',
  4. 'Db _ host' => 'localhost ',
  5. 'Db _ name' => 'Create a database by yourself ', // you need to create a new database! The name is
  6. 'Db _ user' => 'Your database username', // database username
  7. 'Db _ pwd' => 'Database password', // database logon password
  8. 'Db _ port' => '123 ',
  9. 'Db _ prefix' "white-space: pre" >=> 'think _ ', // database table name PREFIX
  10. );
  11. ?>


The preceding DB_NAME, DB_USER, and DB_PWD must be modified.

They are

'Db _ name' => 'rubydb ',

'Db _ user' => 'Your mysql logon account ',

'Db _ pwd' => 'Your mysql logon password'


  • 5. run http: // localhost/xxx/admin. php. the background logon page is displayed:


Enter the user information in database think_user: ruby97, password: ruby97, and then enter the verification code to log on.


Select the "write news" button and add an article by yourself. Go to http: // localhost/xxx and check the result!



Source code download link



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.