Home >Backend Development >PHP Tutorial >Use the ThinkPHP framework to quickly develop multiple images on your website

Use the ThinkPHP framework to quickly develop multiple images on your website

WBOY
WBOYOriginal
2016-08-08 09:19:141040browse

Collect good articles

Use the ThinkPHP framework to quickly build a website

This week I have been busy working on the laboratory website, and the basic functions are completed. What is more rewarding is having a general understanding of the ThinkPHP framework. Write something as a souvenir. If there is any help for you who are also new to the Web, that would be even better.

I used to make a very crappy website using PHP. Why do I say that, because it was all dead code. After finishing it, I felt really tired. The front end required div+css, js, and the back end required php, mysql. There were so many things to do, it was a headache. Therefore, after receiving the task of making a website, I immediately thought that I must use a development framework to do it, and I must not be as tired as before.

I chose PHP’s ThinkPHP framework. To be honest, it's really pretty good. I instantly felt that web development was quite efficient.

Hyperlink: ThinkPHP Chinese website

Follow the routine and first post the experimental environment:

  • 1. AMP (the integrated one, the easiest Almost no configuration required)
  • 2. ​ ZendStudio7.2 (Chinese website http://www.zendstudio.net/, which provides downloads, online registration machines and usage tutorials)
  • 3. ThinkPHP framework+Baidu UEditor editor plug-in
  • 4. (Firefox, IE, Chrome), plus Firefox
  • FireBug
  • plugin is used for debugging and stealing styles



----------------------------- ------------------------------------------------Gorgeous segmentation--- -------------------------------------------------- --------




Step 1: Find a web template (no art, PS, sorry...)

I’m looking for a front-end page from a university laboratory, so I won’t post it here, it’s very ordinary. The template downloaded directly in the background looks quite beautiful. It looks like this after running:



Step 2: Get to know the Baidu UEditor plug-in

Because what I want to do is the website of the laboratory. The main content of the laboratory website is as follows:

Introduction to the members of the laboratory

Lab projects, results, etc.
  • Laboratory news and academic exchange information
  • It can be seen that the information mainly focuses on the news and article information release in the background, while there are almost no editors
in the front desk. Therefore, the focus is to implement a

convenient article publishing system in the background. I chose the UEditor WYSIWYG editor produced by Baidu. It is mainly implemented by JS. After being integrated into the backend, it is as shown below. There are really many functions:


For how to integrate UEditor into the website, please refer to the official website of UEditor , which contains detailed tutorials.



Step 3: Get to know the ThinkPHP framework for the first time

Let’s start with the introduction of the ThinkPHP framework. First, in order to strengthen the perceptual understanding, let’s take a look at the directory structure of the ThinkPHP project:



Folder:

  • admin is the background project folder
  • home is the front-end project
  • public is used to store CSS files, JS files and images in web pages
  • ThinkPHPframework
  • ueditor is Baidu editor

The following three PHP files:

  • admin.php is the entry file for the backend project,
  • index.php is the entry file for the frontend project.
  • config.inc.php, since almost all the data of this small project is placed in the database, and the front and backends need to connect to the database, all the database configuration code is placed in it.

And the remaining buildpath, .settings, .project are generated by Zend IDE, regardless of them.


---------------------------------------- ---------------Gorgeous segmentation---------------------------------- --------------------------



Because there are actually two of them here For projects (home and admin), you only need to understand one of them, so only introduces the background, that is, the admin part.


Then look down and take a look at the admin.php file. This is the first file you need to know, which is the entrance file of the backend.




Several macro definitions respectively specify:

  • 1. ThinkPHP framework path
  • 2. Backend application directory
  • 3. Backend application name
  • 4. Turn on debugging mode
  • 5. Contains the ThinkPHP.php file under the ThinkPHP framework ( Important )

Let’s create a new small project to demonstrate the development process:

For example, the large directory of the entire project is xxx, and then copy the ThinkPHP framework in, And add the admin.php file (like the code in the picture above)



Enter in the browser at this time: http://localhost/xxx /admin.php will automatically generate the admin directory, as shown below



Open the admin directory, the content is as follows:



Mainly focus on four folders:

1. Conf stores project configuration files (such as defining some constants and so on)

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

3 . Tpl (storage template files, logically the templates we downloaded earlier should be placed here)

4.

Finally post the running results on the browser. . Sorry for posting a bit late.



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


---------------------------------------- --------------------Gorgeous segmentation----------------------------- ------------------------


Step 4: Get to know the MVC pattern


At this point, I have to mention the concept of MVC in ThinkPHP. MVC is famous for its model-view-controller pattern.

In ThinkPHP:

Model can be thought of as a database table . As for the project I am doing:

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




View can be thought of as What users see, That is the template, or skin.

For example, the background template posted earlier is a view in the ThinkPHP concept.

The controller (Action) can be considered as the bridge between the view and the model. Because the content displayed on the website basically comes from

many models (database tables), and the controller is responsible for deciding which data in which models to display under which circumstances. Let’s take my example. The controller corresponding to the home page of my background project is the Index controller. (The Index controller is the default controller for all projects). But if you think about it, the homepage of a website usually has a lot of buttons, hyperlinks, etc. that can jump to other places. Post a picture:



This is my backend homepage. You can see that there is a navigation bar on the left, and the current article information in the database is listed on the lower right. You can see that there are three articles in total. For example, what I might want to do now is:


1. Delete the article "1111 Test Article Publishing System"


2. Change the article "James holds the 3rd MVP trophy" to "James Holding high the 4th MVP trophy”

3. Added another article

This corresponds to the different functions of the Index controller, that is, the different methods of the Index controller class IndexAction (such as naming the edit() method, delete() method, and add() method). Of course, the reason why you see the page display is because the Index() method of the Index controller is executed by default, and the display() method is called in this method to display the template (View). If you don’t believe it, you can look at the default file. The Index controller’s Index method implements the welcome page of HelloThinkPHP.


For example:

IndexAction directory is:

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

The content is:



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

(Note that IndexAction inherits the Action class, and the display method is the Action class method);

After calling the display() method, ThinkPHP will find the index.html file under the Index file under the Tpl folder of the corresponding project. The former Index folder corresponds to the Index controller, and index.html corresponds to the index() method.

So, A controller class corresponds to a template folder. The specific number of corresponding templates depends on how many methods the controller class has and how many methods require display.

Therefore, the corresponding template file path at this time is:

/xxx/admin/Tpl/Index/index.html


Careful friends may I have to ask, you only talked about views (templates) and controllers here, what about models?How do you know the information of the three articles in the database? Actually, I did some things in the index method. The following picture is a simplified version of the index method:



As you can see, in the first step, I instantiated it a model. The name of the model is Article. As mentioned earlier, the model is a database table. Let’s take a look at the database tables:



The first table name is think_article, you can see There is still one prefix missing, think_. In fact, this is specified in the configuration file. Do you remember the config.inc.php mentioned above? The configuration code is as follows:


return array(

​​​​​​ =>'mysql', H'db_host ' = & gt; 'localhost'

,

'db_name' = & gt; ,P'db_pwd '

= & gt; password ,

'db_port'

= & gt; '3306', 'db_prefix '

= & gt;

'Think_', );

?>


The second statement is query database

. ThinkPHP provides many methods for querying the database. I use the coherent operation method.

After executing the second statement, the $new_list variable stores the information of all articles, so how to display it on the interface?


Look at the third sentence, it assigns the variable to a variable called 'new_list' (it seems to have the same name...but it doesn't matter, the key is the assign method), and then we add it in the template file It's OK to replace it inside. By default, writing {$new_list} in HTML is OK. Of course, the new_list here is a composite variable, not a simple 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 article information in the database in the browser according to the rules defined in the view (template).

---------------------------------------- -----------------------Gorgeous segmentation-------------------------- ----------------------------------

Source code download

Finally, the source code of the sample project xxx is given. Friends in need can download it and quickly understand the general principles of ThinkPHP.

Source code functions:

1. Backend administrator login
  • 2. Add articles, edit articles, delete articles
  • 3. Front desk display Article


How to use:

1. Unzip to the root directory of the website, the default is the xxx folder under the www folder

    :


2. Create a new database in MySQL, such as

    rubydb
  • , organize it into utf8-general-ci


3. Import the two database tables

    think_article
  • and think_user under the database table folder. After importing, the following picture will appear:


4. ConfigurationConfig.inc.php

    file
  • [php] view plaincopy

  1. "font-size:16px;">
  2. return array (
  3. 'DB_TYPE'                                                                                                                                     'localhost',
  4. 'DB_NAME' = > 'Build a database by yourself',//You need to create a new database! The name is
  5. 'DB_USER'                                                        'Your database username' ,     //Database username        
  6.  
  7.                                                                                                         using using ’ ’s ’ using ’s db's out's ’ out’s' ‐ out's''your database's out's'‐out's' ,//Database login password​​
  8. 'DB_PORT' => '3306', 'DB_PREFIX'
  9. "white-space:pre"> Visible, above The DB_NAME, DB_USER, DB_PWD need to be modified
  10. respectively 'DB_NAME'=>'rubydb' ,'DB_ USER' =>' Your mysql login account
  11. ' ,
  12. 'DB_PWD' =>'
  13. Your mysql login password'


5. Run http://localhost/xxx/admin .php, the background login page pops up:

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


    Choose the Write News button and
  • add an article yourself. Then go to
  • http://localhost/xxx

to see the results!

Source code download link


Reprinted from: http:// blog.csdn.net/ruby97/article/details/7574851 /

The above has introduced the use of the ThinkPHP framework to quickly develop multiple images on the website, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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