Front and backend configuration:
Create a Conf folder
in the root folder Create aconfig.php file under the Conf folder, which stores public configuration information for easy front-end and back-end calls.
Simple definition of 404Page
Pseudo-static removal.html
Config中 URL_HTML_SUFFIX => ‘’
Assign template
1:$this ->assign(‘XXX’,$XXX);
2:$this->a = 111;
3:$this->assign(‘xxx’,$xxx)->display();
Time stamp processing
{$v.time|date='y-m-d H:i',###}
Group:
After grouping common the file name used individually should be function.php instead of common. php
JS External address cannot be resolved UFunction :
HtmlPage:
Non-application group writing method:
How to write application groups
The last empty space is used to process pseudo-static .html Remove .html
JsPage:
Error page customization:
Conf folder
Data storage:
1)
2):
Data reading:
1):
2):
Random number:
mt_rand(min,max);
U method to remove .htmlend
Verification code production:
SESSION stored in database :
1): In configuration file
2):Create database table:
ThinkPHPCommonly used various methods
A() LoadActionClass
D() LoadModel Class
S() Global cache configuration
L() Get language definition
C() Get configuration value
F() Fast file data reading and saving For simple type data string, array
U() is used to complete the pair URLAssembly of addresses
I() Quickly create an object instance
1.A Quickly CreateActionObject
$action=new UserAction();// is equivalent to the following writing :$action=A("User");And, if the current UserAction class has not been introduced yet, the A method will be introduced automatically. And with the support of singleton mode, the same Action object will not be created repeatedly.
TheA method supports cross-project calls, for example:
$action=A("User",'Admin'); //InstantiationAdmin project’s UserAction class
2.D Quickly create model data objects
First define the model class, such as UserModel, then you can use D() function operates on data. For example:
First create a file named "your project "/Lib/Model >UserModel.class.php’s PHP script, the content is as follows:
class UserModel extends Model{}
$User=D("User"); //
InstantiateUserObject, User is an object you created in the database named "prefix _user" The data table of can also be replaced by $User=new UserModel() to instantiate the object. After instantiation, you can perform a series of operations such as adding, deleting, checking, and modifying the data, such as:
$User->find(1); //
The search primary key is 1’s record
1 to the specified field. At this time, I can write like this
$User->score='(score 1)';$s->save();This will save us a lot of steps.
If you want to modify a specified field, it can be abbreviated as follows:D('User')->setField('name','hehe','id=2');
The main difference between the
Dmethod and the M method is: The
Mmethod does not need to create a model class file. The M method does not read the model class, so automatic verification is invalid by default, but it can be dynamically It is implemented by assignment; and the D method must create a model class. We can use the following two methods to create a mapping object of a data table.
First type:$Test=D('Test');
Second type:$Test=new Model('Test');
Although both of these can performselect, insert, delete, udpate operations on data, there is a big difference in data verification. Using the first method to instantiate a model will There is a data checking function. For example, you can define that if title is not filled in, it will prompt “Please enter the title ” (This is an automatic verification function provided by tp. Of course, it also needs to be in the corresponding model Define the verification conditions ); The
Dmethod can automatically detect the model class, and it will throw an exception if it does not exist. At the same time, the instantiated model will not be repeatedly instantiated (single case). The default D method can only support calling the current project ( or application )Model below. For example:
$user=new UserModel();Equivalent to
$user=D('user');
If an empty model is instantiated, for example:$Demo=new Model();
Then it is equivalent to:
$Demo=M();
3.S
Quick operation caching method
ThinkPHP
Abstract various caching methods into a unified cache class to call, and ThinkPHP unifies all caching mechanisms into one S method to operate, so different caches are used There is no need to pay attention to specific caching details when using this method. Such as:
S('data',$Data); //Use data to identify cache$Datadata
S('data',$Data,3600); //Cache$DataData3600Seconds
$Data=S('data'); //Get cached data
S('name',null); // Delete cache identifiername
4.L Quickly operate language variables
L method provides multi-language support and can quickly set and obtain language definitions.
L('USER_INFO','User information '); //Set the language named USER_INFO Variable
L('USER_INFO'); //Get the language variable value of USER_INFO
//Batch assignment
$array['USER_INFO']='User information';$array['ERROR_INFO']='Error message';
L($array);
5.C To quickly operate configuration variables, the usage is C("Fill in the subscript of the array in the configuration file here ")
C('USER_AUTH_ON',true); //Set the configuration parameter named USER_AUTH_ON
C('USER_AUTH_ON'); / /Get the variable value of USER_AUTH_ON
Same as L, C also supports batch assignment
Note: Configuration parameters are not case-sensitive
In addition, starting from the 1.5 version, the C method also supports the operation of two-dimensional arrays, for example:
C('USER.USER_TYPE',1);
C('USER.USER_AUTH_ON');
6. F File data saving method
TheF method is mainly used for writing, changing and deleting project file data. Its working mechanism is similar to the S method. The difference lies in its different uses. The directory where the data is saved is also different, and the caching method cannot be specified because the data is saved in file form by default. The F method uses the var_export method, so it can only support simple data types and does not support object caching.
7. U is used to complete the assembly of URL addresses. The feature is that it can automatically be based on the current URL Modes and settings generate corresponding URLaddress
The format of this function is: U('Address','Parameters','Pseudo-static','Whether to jump','Display domain name');The advantage of using the U method instead of hard-coding the URL address is that once your environment changes Or parameter settings change, you don't need to change any code in the template. The calling format in the template needs to be {:U('address', 'parameter'… )} way.
Usage example ofU method:
U('User/add') // Generate the User module’s add operation address
Can also support group calls:
U('Home/User/add') // Generate Home grouped User moduleaddoperation address
Of course, you can also just write the operation name to indicate calling the current module
U('add') // Generate the add operation address of the current access module
In addition to group, module and operation names, we can also pass in some parameters:
U('Blog/read?id=1') // Generate Blog module's read operation And id is 1’s URLaddress
The second parameter of theU method supports incoming parameters and supports two definition methods: array and string. If only string parameters can be defined in the first parameter, the following methods are Equivalent:
U('Blog/cate',array('cate_id'=>1,'status'=>1))
U('Blog/cate','cate_id=1&status=1')
U('Blog/cate?cate_id=1&status=1')
Import CSS/JS File