Home > Article > Backend Development > thinkphp summary transfer, thinkphp summary_PHP tutorial
After doing several projects with ThinkPHP, I feel that this framework is quite good and very suitable for my logical habits. It is also fast to develop, haha, I have summarized some commonly used stuff in projects, I hope it will be helpful to friends who are new to TP!
Promise:
Such as:
Define controller class
Recorded changes:
Record query
$User->getDbFields() //Get the current data fields
$Model = new Model() // Instantiate a model object that does not correspond to any data table
$objrs = $Model->query("select * from think_user
where status=1") //Custom query
Template:
$this->assign('name',$value); //Used in Action class The assign method assigns values to template variables. Regardless of the variable type, assign is used uniformly
$this->display() // Output template file
Batch assignment
Template tag:
{ } or {//Comment content } //Template comment
In order to facilitate template definition, regardless of whether the output template variable is an array or an object, it can be output in the following unified way:
Use function:
System variables
Quick output
Include external files
Loop output
Template assignment:
Template definition:
Pay attention to the meaning of name and id
// Output even number records
// Output key
//Sub-loop output
Switch Tag
Compare Tags
If tag
C operation
A operation
D operation
S operation
F operation
(IN parameter1 INTEGER)
BEGIN
DECLARE variable1 CHAR(10);
IF parameter1 = 17 THEN
SET variable1 = 'birds';
ELSE
SET variable1 = 'beasts';
END IF;
Insert INTO table1 VALUES (variable1);
END
ThinkPHP system constants
THINK_PATH // ThinkPHP
System directory
CONFIG_PATH
//Project configuration file directory
MEMORY_LIMIT_ON // Is there a memory usage limit
Predefined constants
Upload Overview
Basic upload function
Batch upload
It should be noted that the UploadFile upload class is not used for multi-file upload
Ajax file upload
Automatically generate thumbnails
After setting, the system will automatically generate thumbnails in the same format after uploading. The system's default thumbnail path is the directory where the uploaded file is located, and _thumb is added to the end of the file to identify the thumbnail file. The thumbnail path can be configured in the project configuration file.
Generate multiple thumbnails
The above example indicates that thumbnails of three sizes are generated, and the suffix added after the thumbnail file name is specified, as well as the width and height dimensions of the three thumbnails.
More upload settings
ThinkPHP also provides an upload setting interface with the UploadFile class in Action, which facilitates more parameter settings on the client for upload control.
The main parameters are listed below. For more parameters, please refer to the _upload method in the Action class of the framework.
5.15 Redirection
The redirect method of the Action class can implement the redirection function of the page.
The parameter usage of the redirect method is consistent with the usage of the U function (refer to the URL generation part above), for example:
The above usage is to jump to the category operation of the News module after staying for 5 seconds. , and displays the words page redirection, and the current URL address will be changed after redirection.
If you just want to redirect to a specified URL address, rather than to the operation method of a certain module, you can directly use the redirect method to redirect, for example:
The third step of the Redirect method One parameter is a URL address.
5.14 Page Jump
In application development, you often encounter some jump pages with prompt information, such as successful operation or operation error pages, and automatically jump to another target page. The system's Action class has two built-in jump methods, success and error, for page jump prompts, and can support ajax submission. The method of use is very simple, for example:
Success and error methods have corresponding templates and can be set. The default setting is that the templates corresponding to both methods are:
template Files can use template tags, and the following template variables can be used:
$msgTitle: operation title
$message: page prompt information
$status: operation status 1 indicates Success 0 means failure. Specific rules can also be defined by the project itself
$waitSecond: Jump waiting time unit is seconds
$jumpUrl: Jump page address
success and error The method will automatically determine whether the current request is an Ajax request. If it is an Ajax request, the ajaxReturn method will be called to return information. For details, please refer to the AJAX return section later.
These things are in the thinkphp3.0 manual, download the manual and take a look
How to connect to the database in ThinkPHP. Before operating the database, we need to create a Model. Before talking about Model and Action, let me first explain the storage locations of Model and Action. Model is saved in the lib/Model folder in the program directory, and Action is saved in the lib/Action folder in the program directory. The default Model rules of the ThinkPHP system are as follows: the civil name of the Model file is similar to "Model class name+Model.class.php, and the default operating database table name of the Model is DB_PREFIX+Model class name we defined in config.php. Model class names and file names need to be capitalized." In the Model file, define a class and extend the Model class. The general writing method is as follows: class class name Model extends Model{} So, now let's define a Model. Since the name of our database table is cms_article, the class ArticleModel extends Model{} file is saved as ArticleModel.class.php. There is no need to write anything, a Model has been defined. So now, let's continue our Action knowledge. Many rules of Action and Model are very similar. The difference is that Action does not directly operate the database, but needs to operate the database through Model. Now we define an Action to complete the operation. class IndexAction extends Action{function index(){$Article = D("Article");}} Save the file as IndexAction.class.php. OK, now let's refresh the homepage. If there are no prompts, congratulations. The database connection Model and Action definition are all normal. The D method in Action is to call Model, and Article is the Model class in ArticleModel.class.php that we just defined~ In other words, while defining Model, we have completed the connection to the database and preparation for database table operations~