The meaning of large letters in thinkphp

Source: Internet
Author: User
Tags delete cache

thinkphp Single Letter function

A () Internal instantiation controller

D () instantiating a custom model class

M () instantiates an underlying model class

R () Invoke the action method of a controller

L () to set and get the current language definition when multiple languages are enabled

N () Counter method

G () includes marker position and interval statistics two functions

F () is used for simple data caching and can only support file forms

C () to set, get, and save configuration parameters

The method is detailed:

The A method is used to instantiate the controller internally, calling the format:

A (' [ project ://][ Group /] module ', ' controller layer name ')

The simplest usage:

$User = A (' User ');

representing the instantiation of the current project. Useraction controller (this controller corresponding file is located in lib/action/useraction.class.php), if the group mode is used, and to instantiate another admin group controller can be used:

$User = A (' Admin/user ');

Cross-project instantiation is also supported (the catalog of the project is to remain sibling)

$User = A (' Admin://user ');

represents the instantiation of The Useraction controller under the Admin project

Version 3.1 adds support for layered controllers, so you can also instantiate other controllers with the A method, for example:

$User = A (' User ', ' Event ');

instantiation of Userevent controller (the corresponding file is located in lib/event/userevent.class.php).

Once you instantiate the controller, you can call the methods in that controller, but be aware that in the case of cross-project calls, if your method of action There are some unknown problems with the special variable operation for the current controller, so, in general, the official recommendation requires that the controller layer of the public call be developed separately, not having too many dependencies.

The D method should be used in a more numerous way, to instantiate a custom model class, is a thinkphp framework for the model class instantiation of a package, and implemented a singleton mode, support cross-project and group calls, the invocation format is as follows:

D (' [Project://][Group/] model ', ' model layer name ')

The return value of the method is the instantiated model object.

The D method automatically detects the model class and, if there is a custom model class, instantiates the custom model class and, if not, instantiates the model base class, and does not repeat the instantiation for the instantiated models.

The most common use of the D method is to instantiate a custom model of the current project, for example:

instantiating the User model

$User = D (' User ');

is imported under the current project lib/model/usermodel.class.php file, and then instantiate the Usermodel class, so the actual code might be equivalent to the following:

Import (' @. Model.usermodel ');

$User = new Usermodel ();

However, if you use D method, if the Usermodel class does not exist, it is automatically called

New Model (' User ');

And the second call without re-instantiation, you can reduce the cost of some object instantiation.

The D method can support cross-grouping and project instantiation models, such as:

Instantiate the User model of the Admin project

D (' Admin://user ')

Instantiate the User model for the Admin group

D (' Admin/user ')

Note: To implement a cross-project invocation model, you must ensure that the directory structure of the two projects is tied.

Beginning with version 3.1, the D method can also instantiate other models because of the increased support for the layered model, for example:

Instantiate the userservice class

$User = D (' User ', ' Service ');

Instantiate the userlogic class

$User = D (' User ', ' Logic ');

D (' User ', ' Service ');

is imported Lib/service/userservice.class.php, and instantiated, is equivalent to the following code:

Import (' @. Service.userservice ');

$User = new Userserivce ();

The M method is used to instantiate a base model class, and the difference between the D method is:

1, do not need to customize the model class, reduce IO loading, good performance;

2, after instantiation can only invoke the Basic model class (the default is the Model Class) in the method;

3, you can specify the table prefix, database and database connection information at the time of instantiation;

The power of the D method is reflected in how strong the custom model classes you encapsulate, but as the new thinkphp framework's underlying model classes become more powerful,the M method is more practical than the D method.

The invocation format of the M method:

M (' [Base model name:] Model name ', ' data table prefix ', ' database connection information ')

let's see . The M method has specific uses:

1. Instantiation of the Basic model class

When no model is defined, we can instantiate a model class using the following method:

Instantiating the user model

$User = M (' User ');

Perform additional data operations

$User->select ();

This approach is the simplest and most efficient because there is no need to define any model classes, so cross-project calls are supported. The disadvantage is also that because there is no custom model class, the related business logic cannot be written and only basic curd operations can be done.

$User = M (' User ');

is actually equivalent to:

$User = new Model (' User ');

represents the Action think_user table. The M method has a singleton function as well as the D method, and multiple invocations do not repeat the instantiation. The model name parameter of the M method is automatically converted to lowercase when converted to a data table, which means that the thinkphp table naming specification is in all lowercase format.

2. Instantiation of other public model classes

The first method is instantiated because there is no definition of the model class, so it is difficult to encapsulate some additional logical methods, but in most cases, perhaps just need to extend some common logic, then you can try one of the following methods.

$User = M (' Commonmodel:user ');

The change usage is actually equivalent to:

$User = new Commonmodel (' User ');

because the model classes of the system can be loaded automatically, we do not need to manually perform class library import operations prior to instantiation. Model class Commonmodel must inherit models. We can define some common logic methods in the Commonmodel class, and we can omit to define a specific model class for each data table, if your project already has more than 100 data tables, and most of the cases are some basic curd operations, Just the individual models have some complex business logic that needs to be encapsulated, so the combination of the first and second approaches is a good choice.

3. Incoming table prefixes, databases, and other information

The M method has three parameters, the first parameter is the model name (which can include the underlying model class and database), the second parameter sets the data table prefix (leaving the table prefix for the current project configuration blank), and the third parameter sets the database connection information currently in use (leaving the database connection information for the current project configuration blank). For example:

$User = M (' DB2. User ', ' think_ ');

represents the instantiation of Model class, and manipulate the Think_user table in the DB2 database.

If the second argument is left blank or does not pass, the data table prefix in the current project configuration is used, and if the data table for the operation does not have a table prefix, then you can use:

$User = M (' db1. User ', null);

represents the instantiation of Model class, and manipulate the user table in the DB1 database.

If you are working on a database that requires a different user account, you can pass in the connection information of the database, for example:

$User = M (' User ', ' think_ ', ' mysql://user_a:[email protected]:3306/thinkphp ');

represents the underlying model class with the Model, and then to the Think_user table operation, with user_a account for database connection, operation database is thinkphp.

A third connection information parameter can be used The DSN configuration or array configuration can even support configuration parameters.

For example, in the project configuration file, configure the following:

' Db_config ' = ' mysql://user_a:[email protected]:3306/thinkphp ';

You can use:

$User = M (' User ', ' think_ ', ' db_config ');

The underlying model classes and databases can be used together, for example:

$User = M (' Commonmodel:db2. User ', ' think_ ');

If you are instantiating a hierarchical model, using the public model class, we can use:

M (' Userlogic:user ');

to instantiate Userlogic, although this does not make much sense, because it can be used

D (' User ', ' Logic ');

Achieve the same functionality.

The method of operation of the R method for invoking a controller is further enhanced and supplemented by the a approach.
The call format for the R method:
R (' [Project://][Group/] Module/operation ', ' parameters ', ' Controller layer name ')
For example, we define an action method that:

    1. Class Useraction extends Action {
    2. Public Function Detail ($id) {
    3. Return M (' User ')->find ($id);
    4. }
    5. }

then you can go through The R method calls this action method in the other controller (the general R method is used for cross-module invocation)

    1. $data = R (' User/detail ', Array (' 5 '));

represents the call The detail method of the user controller (the detail method must be of the public type), and the return value is a user data with a query ID of 5. If the action method you want to invoke is not a parameter, the second argument can be left blank and used directly:

    1. $data = R (' User/detail ');

You can also support cross-grouping and project invocation, for example:

    1. R (' Admin/user/detail ', Array (' 5 '));

represents the call The Admin Group detail method under the user controller.

    1. R (' Admin://user/detail ', Array (' 5 '));

represents the call The detail method for the user controller under the Admin project.
The official recommendation is not to make too many calls on the same layer, which can cause logical confusion, and the part that is called by the public should be encapsulated as a separate interface, with a new 3.1-feature multilayer controller that adds a single controller layer to the interface call, for example, We add an API controller layer,

    1. Class Userapi extends Action {
    2. Public Function Detail ($id) {
    3. Return M (' User ')->find ($id);
    4. }
    5. }

then, using R method call

    1. $data = R (' User/detail ', Array (' 5 '), ' Api ');

in other words, The third parameter of the R method supports specifying the controller layer of the call.
Meanwhile, The R method can support the operation suffix setting C (' Action_suffix ') when invoking the action method, and if you set the action method suffix, you still do not need to change the way the R method is called.

The L method is used to enable multiple languages, setting and getting the current language definition.

Call Format: L (' language variable ' [, ' language value '])

Setting language variables

in addition to using language packs to define language variables, we can use the The L method dynamically sets the language variables, for example:

L (' Lang_var ', ' language definition ');

Language definitions are not case-sensitive, so the following are equivalent:

L (' Lang_var ', ' language definition ');

But for the sake of specification, we recommend using uppercase to define language variables uniformly.

The L method supports batch setting of language variables, such as:

$lang [' lang_var1 '] = ' language definition 1 ';

$lang [' lang_var2 '] = ' language definition 2 ';

$lang [' lang_var3 '] = ' language definition 3 ';

L ($lang);

represents the simultaneous setting of 3 language variables Lang_var1 lang_var2 and LANG_VAR3.

[-more-]

Get language variables

$langVar = L (' Lang_var ');

Or:

$langVar = L (' Lang_var ');

If the argument is empty, it means getting all of the currently defined language variables (including the language definition file):

$lang = L ();

Or we can use it in a template.

{$Think. Lang.lang_var}

To output the language definition.

The N method belongs to the counter method and is used for the core query, the count of cache statistics and statistics. But in fact it can be used for other counting purposes of the application, the usage is relatively simple, the calling format:
N (' Count position ' [, ' step value '])
For example, we want to count the number of queries in the page, which can be used

    1. N (' read ', 1);

indicates that each execution to this position will cause the counter to add 1, until the end of the page, we can use

    1. $count = N (' read ');

To count the number of queries performed on the current page.
If you want the counter to increase by 5 each time , you can change the step value, for example:

    1. N (' Score ', 5);

It is important to note that The statistical results of the N method page are not brought into the next statistic.

The function of the G method includes two functions for marker position and interval statistics, and the following is a detailed usage:

Mark Position

The first use of the G method is to mark a location, for example:

    1. G (' begin ');

indicates that the current position is marked as a Begin label, and the execution time of the current position is recorded, and if the environment supports it, it can also record memory usage. You can call the G method tag at any location.

Run Time statistics

Once you've tagged the location, we can call The G method is used for interval statistics, for example:

    1. G (' begin ');
    2. // ... other Code Snippets
    3. G (' End ');
    4. // ... Maybe there's another code here .
    5. to make a statistical interval
    6. echo G (' Begin ', ' End '). ' S ';

G (' Begin ', ' end ') represents the execution time (in seconds) of the statistics begin position to the end position, where the begin must be a marked position, and if the end position has not been marked at this time, the current position is automatically marked as the end label, and the output is similar to the following:

    1. 0.0056s

The default statistical precision is 4 digits after the decimal point , if you think this statistic accuracy is not enough, you can also set up for example:

    1. G (' Begin ', ' End ', 6). ' S ';

The possible output will become:

    1. 0.005587s

Memory Overhead Statistics

If your environment supports memory consumption statistics, you can also use the The G method carries out interval memory overhead statistics in kilobytes (KB), for example:

    1. echo G (' Begin ', ' End ', ' m '). ' KB ';

The third parameter uses m for memory cost statistics, and the result of the output may be:

    1. 625kb

Similarly, if the end tag is not marked, the current position is automatically labeled with the end tag.
If the environment does not support memory statistics, the parameter is invalid and interval run time statistics are still performed.
Forget debug_start, Debug_end, Boulevard to Jane, you know ~

The F method is actually a subset of the S method, only for simple data caching, and can only support file form, does not support the cache validity period, because the use of Php return method, so its efficiency is higher than the S method, so we also called the fast caching method.
The F method is characterized by:
simple data caching;
File Form Preservation;
Adopt PHP returns data mode to load the cache;
Supports subdirectory caching and automatic creation;
Support to delete cache and bulk delete;
Write and read cache

    1. F (' Data ', ' test data ');

The default save starting path is Data_path (the constant is in the default configuration at Runtime_path. ' data/' below), that is, the generated file name is Data_path. ' data.php ' cache file.
Note: Make sure that your cache identity is unique and avoid data overwrite and conflicts.
The next time the cache data is read, use:

    1. $Data = F (' Data ');

We can use subdirectories to save, for example:

    1. F (' User/data ', $data); Cache Write
    2. F (' User/data '); Read Cache

will generate Data_path. ' user/data.php ' cache file, if the user subdirectory does not exist, it will be created automatically, or can support a multilevel subdirectory, for example:

    1. F (' Level1/level2/data ', $data);

If you need to specify the starting directory for the cache, you can do this in the following way:

    1. F (' Data ', $data, Temp_path);

When you get it, you need to use:

    1. F (' Data ', ', temp_path);

Delete Cache

Deleting the cache is also simple, using:

    1. F (' data ', NULL);

The second parameter is passed in NULL indicates the deletion of the data cache that is identified as data.
support for bulk deletion, especially for subdirectory caches, assuming we want to delete all cached data under the user subdirectory, you can use:

    1. F (' user/* ', NULL);

or delete using filter conditions, for example:

    1. F (' user/[^a]* ', NULL);

The C method is the thinkphp used to set up, get, and save configuration parameters, with a high frequency.
Understand The C method needs to first understand the configuration of the next thinkphp because all the operations of the C method are related to configuration. The thinkphp configuration file is defined in PHP array format.
Due to the use of the function overload design, so the use of more, we come to one by one instructions.
Setting parameters

    1. C (' db_name ', ' thinkphp ');

represents settings The value of the db_name configuration parameter is thinkphp, because the configuration parameters are not case sensitive, so the following are the same: [-more-]

    1. C (' db_name ', ' thinkphp ');

However, it is recommended that you maintain a uniform capitalization configuration definition specification.
All parameters of the project can be dynamically changed by this method before it takes effect, and the last set value overrides the definition in the previous setting or custom configuration, or you can add a new configuration using the parameter configuration method.
Supports settings for level two configuration parameters, such as:

    1. C (' USER. USER_ID ', 8);

Configuration parameters are not recommended for more than two levels.
If you want to set multiple parameters, you can use bulk settings, such as:

    1. $config [' user_id '] = 1;
    2. $config [' user_type '] = 1;
    3. C ($config);

if The first parameter of the C method passes in the array, which represents the bulk assignment, and the above assignment is equivalent to:

    1. C (' user_id ', 1);
    2. C (' User_type ', 1);

Get parameters
To get the parameters of the setting, you can use:

    1. $userId = C (' user_id ');
    2. $userType = C (' User_type ');

if Null is returned if the USER_ID parameter has not been defined.
You can also support obtaining level two configuration parameters, such as:

    1. $userId = C (' USER. User_ID ');

If the incoming configuration parameter is empty, it means getting all the parameters:

    1. $config = C ();

Save Settings
Version 3.1 adds a feature that permanently saves settings parameters only for batch assignments, such as:

    1. $config [' user_id '] = 1;
    2. $config [' user_type '] = 1;
    3. C ($config, ' name ');

After the config parameter is set in bulk , it is saved to the cache file (or other configured cache mode), along with all current configuration parameters.
After saving, if you want to retrieve the saved parameters, you can use the

    1. $config = C ("', ' name ');

where name is the identity of the cache that was used to save the parameter earlier, it must be consistent to retrieve the saved parameters correctly. The retrieved parameters are merged with the current configuration parameters and do not need to be merged manually.

The meaning of large letters in thinkphp

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.