thinkphp Internal function Adslcfui Quick Method Full parsing _php tutorial

Source: Internet
Author: User
Tags how to use curl
Thinkphp defines shortcut methods for some common operations, which have a single-letter method name and have a more memorable feature. Interestingly, the letters of these quick methods contain the ADSL letters, so we call it the ADSL method. A, D, S, L, C, F, U, I these quick methods are all in/thinkphp/common/functions.php this file, below I separately explain their respective function and usage.

A () Load action class
D () Load model class
S () Global cache configuration
L () Get the language definition
C () Get Configuration values
F () Fast file data reading and saving for simple type data strings, arrays
U () to complete the assembly of the URL address
I () Quickly create an object instance

1.A Quick Create Action Object

$action =new useraction ();//is equivalent to the following: $action =a ("User"), and if the current Useraction class is not introduced, the A method is introduced automatically. And with singleton mode support, the same action object is not created repeatedly.

The A method supports cross-project calls, such as:

$action =a ("User", ' Admin '); Instantiate the Useraction class for the admin project

2.D quickly create model data objects

Define the model class first, such as Usermodel, and then you can use the D () function to manipulate the data. For example:

Start by creating a PHP script named UserModel.class.php in the "Your Project"/lib/model, which reads as follows:

Class Usermodel extends model{}

Then, without adding any properties and methods, you can do the following:

$User =d ("User"); Instantiate the user object, the user is a data table named "prefix _user" that you create in the database, or you can use $user=new Usermodel () instead to instantiate the object. After the instantiation, you can make additions and deletions to the data, such as a series of operations, such as:

$User->find (1); Find records with primary key 1

We need to add 1 to the specified field when we are making user coins or points or voting. So I can write that.

$User->score= ' (score+1); $s->save (); This will save us a lot of steps.

If you want to modify a specified field, you can abbreviate it as follows:

D (' User ')->setfield (' name ', ' hehe ', ' id=2 ');

The difference between the D method and the M method is mainly:

The M method does not need to create a model class file, the M method does not read the model class, so the automatic validation is not valid by default, but can be implemented by dynamic assignment, whereas the D method must have a model class created, and we can use the following two methods to create a mapping object for a data table.

The first type: $Test =d (' Test ');

The second type: $Test =new Model (' Test ');

Although both of these can be select,insert,delete,udpate operation of the data, in the data validation is very different, in the first instance of a model will have data inspection function, for example, can be defined if the title is not filled, you will be prompted "Please enter the title" ( This is a TP provides an automatic verification function, of course, the corresponding model must be defined in the validation conditions);

The D method can automatically detect the model class, and he throws an exception when it does not exist. At the same time, the instantiated model is not repeated (singleton). The default D method can only support calls to models under the current project (or application). 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 fast-Action caching method

Thinkphp the various caching methods are abstracted into a unified cache class to invoke, and thinkphp all the caching mechanism unified into an S method to operate, so in the use of different caching methods do not need to pay attention to the specific cache details. Such as:

S (' data ', $Data); Cache $data data using the data identity
S (' Data ', $Data, 3600); Cache $data data for 3,600 seconds
$Data =s (' Data '); Get Cached data
S (' name ', null); Remove Cache identity Name

4.L quick-Action language variables

The L method provides multi-language support to quickly set up and get language definitions.

L (' User_info ', ' user Information '); Set a language variable with the name User_info
L (' User_info '); Gets the language variable value of the User_info
Batch Assignment
$array [' user_info ']= ' user information ', $array [' Error_info ']= ' error message ';
L ($array);

5.C Quick Action configuration variable, using C ("Fill in the index of the array in the configuration file")

C (' user_auth_on ', true); Setting configuration parameters with name user_auth_on
C (' user_auth_on '); Gets the variable value of the user_auth_on

Same as L, C also supports batch assignment

Note: Configuration parameters are case insensitive

In addition, starting with version 1.5, the C method also supports the operation of a two-dimensional array, for example:

C (' USER. User_type ', 1);
C (' USER. User_auth_on ');

6. F File Data Preservation method

The F method is mainly used for writing, changing, and deleting the file data of the project, its working mechanism is similar to the S method, the difference is that the data is saved in different directories, and the cache mode cannot be specified, because the default is to save the data in the form of file. The F method uses the Var_export method, so only simple data types are supported, and object caching is not supported.

7. U is used to complete the assembly of the URL address, the feature is that it can automatically generate the corresponding URL address according to the current URL pattern and settings

The function format is: U (' address ', ' parameter ', ' pseudo-static ', ' Jump ', ' Show domain name '); The advantage of using the U method instead of pinning a dead URL address in a template is that once your environment changes or parameter settings change, you don't need to change any of the code in the template. The call format in the template needs to take the form {: U (' address ', ' Parameters ' ...)}.

Example of the use of the U method:

U (' user/add ')//Generate Add Action address for User module

You can also support grouping calls:

U (' home/user/add ')//Generate the Add action address of the User module for the Home group

Of course, it can be just a write operation name, which means that the current module is called

U (' add ')//Generate Add action address for current access module

In addition to grouping, modules, and operation names, we can also pass in some parameters:

U (' blog/read?id=1 ')//Generate a read operation for the Blog module and a URL address with ID 1

The second parameter of the U method supports passing arguments, supports both array and string definitions, and if only a string-only parameter can be defined in the first argument, the following 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 ')

Articles you may be interested in

    • String processing function in PHP (string Functions) Full summary
    • PHP generates consecutive numbers (Letters) array function range () analysis, PHP lottery program function
    • Summary of system constants in the thinkphp Action controller
    • Differences in PHP after adding the static keyword to variables and functions
    • PHP Filter_var () function Filter function
    • PHP compressed HTML page code reduces network data transfer, clear spaces, tabs, comment tags
    • thinkphp Automatic validation and auto-fill Invalid workaround
    • How to use Curl's post submission data in PHP and get a summary of how to get Web page data

http://www.bkjia.com/PHPjc/764160.html www.bkjia.com true http://www.bkjia.com/PHPjc/764160.html techarticle thinkphp defines shortcut methods for some common operations, which have a single-letter method name and have a more memorable feature. It is very interesting that the words of these quick methods ...

  • Related Article

    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.