Thinkphp development tips _ PHP Tutorial

Source: Internet
Author: User
Thinkphp development tips. Some tips for using thinkphp in the development process will be further supplemented later, and more friends are welcome to leave a message below. (1) do not use thinkphp directly in the template to summarize some tips during the development process. I will continue to add thinkphp later. I also welcome more friends to leave a message below.

(1) do not directly use {$ _ GET in the template. id} or {$ Think. get. id}, because {$ _ GET. id} {$ Think. get. neither of the two methods is filtered, and it is easy to be XSS. We recommend that you use the I method, that is, {: I ('Get. id ')}

(2) What should I do if I need to obtain the fields of a table in the database in thinkphp? Example:

$user=M('user');$fields=$user->getDbFields();

The result returns a one-dimensional array composed of table fields.

(3) If we only need to modify the value of a field during data modification, we can use the setField method instead of calling the save method every time. for example:

$user=M('user');$user->where('id=2')->setField('username','www.phpernote.com');

(4) in case of comparison, it is not necessary to use the if condition form. you can also write it in the following form:

 
  
Value
 // The value of the name variable is equal to phpernote and output
 
  
Value
 // The value of the name variable is output if it is not equal to phpernote
 
  
Value
 // Output if the value of the name variable is greater than 5
 
  
Value
 // Output if the value of the name variable is greater than or equal to 5
 
  
Value
 // Output if the value of the name variable is smaller than 5
 
  
Value
 // Output if the value of the name variable is less than or equal to 5

(5) in the delete operation of thinkphp, you can directly use delete without using where to perform the delete operation. for example:

$ User-> delete ('2, 5'); // delete data with a primary key of 2 and 5

(6) instructions on how to use several quick functions of thinkphp.

C operation, Operation (dynamic) configuration: mainly used in the Action method

Get: C ('configuration parameter ')

Set: C ('configuration parameter', new value)

Operation A: quickly create an Action object:

$ Action = A ('user'); equivalent to $ action = new UserAction ();

D operation to quickly create model data objects:

$ Model = D ('user'); equivalent to $ model = new UserModel ();

S operations and quick cache operations

Get: S ('name ')

Set: S ('name', 'value ');

Delete: S ('name', NULL );

The F operation is the same as the S operation.

(7) when the Model is named, it must be consistent with the table name in the database by default, such as PhpernoteUserModel. class. the database name corresponding to php should be 'Prefix _ phpernote_user ', and the call in the program code should be: D ('phpernoteuser ');

(8) If cache is not required during development, the following definitions can be made in the entry file:

define('NO_CACHE_RUNTIME',true);

(9) If you need to know some additional information during development, you can define it in the configuration file. below are several common information definitions:

'Show _ RUN_TIME '=> true, // The running time is displayed as 'show _ ADV_TIME' => true, // The detailed running time is displayed as 'show _ DB_TIMES '=> true, // Display database operation times 'show _ CACHE_TIMES '=> true, // display cache operation times 'show _ USE_MEM' => true, // display memory overhead

(10) customize the Model class and use it when instantiating the Model (the Model is the database operation class). For example:

A. create an IndexModel. class. php under the Model directory under the Lib directory under the project directory, and then write a method play in it. the content of this method is in the loop output of 1 to 10.

B. in the Action method, you can also write it as follows:

$ Index = M ('index', 'indexmodel'); // you can add a custom model class to instantiate $ index-> play ();

The output is 1 to 10.

(11) If you need to change the default template suffix during thinkphp usage, you can make the following definitions in the configuration file:

'Tmpl _ TEMPLATE_SUFFIX '=>'. dwt '// modify the template suffix to dwt.
Articles you may be interested in
  • Summary of query techniques in ThinkPHP
  • Complete parsing of Thinkphp internal function ADSLCFUI shortcuts
  • Thinkphp prints the last SQL statement
  • The last record of the volist loop in the thinkphp Template
  • Summary of common system variables in Thinkphp templates
  • PHP implements MVC development in the simplest way, model thinking
  • Thinkphp automatic verification and automatic filling are invalid solutions
  • Thinkphp method for disabling caching

Bytes. (1) do not use it directly in the template...

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.