Home > Article > Backend Development > PHP - detailed introduction and use of thinkphp5
This article is suitable for friends who have just learned PHP but have not yet come into contact with the framework.
Preface:
I wonder if you all feel this way. After a period of learning PHP, I found that this language is not difficult and it is easy to get started. Just write a web interface casually, and then mess with the contents of the database. It doesn't seem to be difficult. The editor at the time was also so stupid and thought that building a website was just that—just so so( ̄_, ̄). However, as my studies continued to deepen, I suddenly discovered that this was not the case. Looking back on my original thoughts, I could only silently lament that I was too young and too simple. As far as the editor is concerned, what I learned at the beginning was the introductory stage of PHP. In short, I just added the code to that pile, and then adjusted the database display to implement the function. As the number of functions I wanted to write increased, the editor gradually found that it took more than 20 pages to write a design and half a day to change the code. However, the editor did not have the concept of a framework at the time. It wasn't until I learned the Struts2 framework of Java EE that the editor realized that he was such a good person - he exploded on the spot instantly. In order to strengthen the learning of PHP, the editor spent some time studying one of the many PHP frameworks->thinkPHP5.
Afterword:
I wrote this article to make a study note, and by the way, I will give a simple introduction to the friends who want to learn thinkphp5, and put it in the database. The editor will not introduce the operation, because there are really many, but there is a more detailed explanation in the development manual. If the writing is not good, please correct it. In addition, the editor personally feels that the quick start manual of tp5 is also worth reading (paid).
1. What is the thinkphp5 framework?
Before introducing thinkphp5, let’s first talk about what a framework is. In short, someone else has built a house for you but it has not yet been decorated. You need to decorate it in the apartment designed by others, saving you the repetitive operation of moving bricks. (For an in-depth understanding of the framework, you can Baidu on your own).
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. Since its birth in 2006, it has been adhering to simple and practical design principles while maintaining excellent performance. While keeping the simplest code, it pays special attention to development experience and ease of use, and has many original functions and features, providing strong support for WEB application and API development. (For beginners, I think it is useless to understand the concept. It is better to feel it directly during the project)
2. Preliminary preparations
(1 ) Selection of programming tools: Open tools: phpStorm Server tools: phpStudy
The choice of tools mainly depends on personal preferences, but it is best to choose mainstream editing tools.
phpstrom download and installation tutorial address: http://www.php.cn/xiazai/gongju/122
phpStudy download and installation tutorial address: http://www.php.cn/xiazai/gongju/845
If you have not used phpstudy, you can link to the above address to learn the basic operation methods of phpsyudy
* Below This step is not needed for the time being
Build PhpStorm PhpStudy development environment:https://blog.csdn.net/u012861467/article/details/54692236
(2) Download and install thinkphp5
Download address: http://www.thinkphp.cn/donate/download/id/1155.html
Installation method: Unzip the compressed package Just put phpStudy/ww under the project
Enter the URL: localhost/thinkphp_5/public/ When the following interface appears, the installation is successful
(3) Knowledge required to learn thinkphp5
Knowledge at the introductory stage of php PHP object-oriented programming (==>You can look at the namespace part first) mvc design pattern
3. Basic usage of thinkphp5
(1) Reference materials: thinkphp5 complete development manual: https://www.kancloud.cn/manual/thinkphp5/ 118003 There is a lot of content in the development manual and it is not necessary to read it directly. For those who are new to tp5, you can look up which part of the knowledge you need.
(2) Directory structure: (You can have a rough understanding)
There are many directory files in tp5. Here we focus on the underlined parts. For other parts, you can refer to the development manual. Let’s talk about application first. Simply put, this part is where we write code. As shown in the picture above, the index module is the front end, and we usually create the admin module and back-end administrator ourselves.
Under the index module and the admin module are the familiar MVC, datebase.php is the file for configuring the database, and config.php under the application affects all modules. Configuration file, while the config.php of index and admin only affects files under their own modules. The static under public is a file that contains static resources, and index.php is the entry file, which can only be accessed through this file.
(3) MVC
Model (model) is the part of the application used to process application data logic.
Usually the model object is responsible for accessing data in the database.
View (View) is the part of the application that handles data display.
Usually views are created based on model data.
Controller is the part of the application that handles user interaction.
Usually the controller is responsible for reading data from the view, controlling user input, and sending data to the model.
Model (model)
a: Database configuration:
Open database.php, generally the underlined part needs to be changed . In addition, in the thinkphp5.0 complete development manual, we can see the development specifications in the basic directory, which we need to understand.
b: Establishing a model: (This part is also explained in more detail in the development manual)
<?php namespace app\admin\model; use think\Model; class User extends Model { } ?>
In most cases, we do not need to define anything for the model The properties and methods can complete basic operations. The model will automatically correspond to a data table. The specification is: database prefix current model class name (excluding namespace). Because the model class naming is camel case, when obtaining the actual data table, it will automatically be converted into a data table name named with lowercase underscores, so just create a data table name with the data Just model with the same name as the table. If your data table is not named with a prefix and an underscore like tp_user, you can specify the data table in the model.
<?php namespace app\admin\model; use think\Model; use think\Db; class Swe extends Model { // 设置完整的数据表(包含前缀) protected $table = 'swe'; }
The following is an example to explain:
First create the following files under the admin module, create User.php under the controller, create User.php under the Model, and create it under the view user folder, create user.html
and create user table fields with id, clickname, email, birthday
The code is as follows:
Under the controller User.php
<?php namespace app\admin\controller; use think\Controller; use app\admin\model\User as UserModel; class User extends Controller { // // 创建用户数据页面 // public function create() // { // return $this->fetch('user');//加载模板,会自动找到view下的user的user.html并显示 // } // 创建用户数据页面 public function create() { return view('user');//加载模板,会自动找到view下的user的user.html } // 新增用户数据 public function add() { $user = new UserModel; if ($user->allowField(true)->save(input('post.'))) {//input('post.'))为表单提交的数据 return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功'; } else { return $user->getError(); } } // model的助手函数新增用户数据 public function add3() { // 使用model助手函数实例化User模型 $user = model('User'); // 模型对象赋值 $user->data([ 'nickname' => 'SWE', 'email' => 'thinkphp@qq.com' ]); if ($user->save()) { return '用户[ ' . $user->nickname . ':' . $user->id . ' ]新增成功'; } else { return $user->getError(); } }
User.php
<?php namespace app\admin\model; use think\Model; class User extends Model { }
user.html
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>创建用户</title> <style> </style> </head> <body> <h2>创建用户</h2> <FORM method="post" class="form" action="{:url('admin/user/add')}"> 昵 称:<INPUT type="text" class="text" name="nickname"><br/> 邮 箱:<INPUT type="text" class="text" name="email"><br/> 生 日:<INPUT type="text" class="text" name="birthday"><br/> <input type="hidden" name="__token__" value="{$Request.token}" /> <INPUT type="submit" class="btn" value=" 提交 "> </FORM> </body> </html>
url:localhost/tp5/public/index.php/admin/ user/create
Execute the create method under user.php
return $this->fetch('user'); //Load the template, the user.html of user under view will be automatically found and Display
user.html Submit data to the add method
Instantiate the Model class, $user->allowField(true)->save(input('post.')) submit data Go to the database
and for more related questions, please visit the PHP Chinese website: PHP Video Tutorial
The above is the detailed content of PHP - detailed introduction and use of thinkphp5. For more information, please follow other related articles on the PHP Chinese website!