thinkphp Single letter function pack up

Source: Internet
Author: User
thinkphp Single letter function collation

This is a tdweb note, and I put it up after obtaining his consent. He was originally written for the Lite version, but now these single-letter functions are also present in the Standard Edition. So this data is equally applicable in two versions of thinkphp. Because Tdweb stole a little lazy, part of the code is directly copied over, so, I did a bit of finishing (mainly page style of a little update, while some of the statements have been updated, Tdweb mo angry)
Original URL: http://bitctw.cn/hl/docs/
The original text is as follows: (I only introduce the single letter function)

TP all the separate functions, except for the two buildappdir used to build the project, the rest are in the framework directory/common/functions.php this file.

?

Some people do not like TP this single-letter function, in fact, this is also a feature of TP, if you understand the function of these functions, whether it is back, or write, are very convenient, next we start in alphabetical order.

?

A function (basically a shorthand for action)

A function is used to instantiate our action class, for example, our program has 2 actions indexaction and testaction respectively, in Indexaction there is a Myhello method to output Hello World, What if I also want to output the same text in testaction? The most primitive method first we import IndexAction.class.php this file, then new indexaction, and finally call the Myhello method.

The code is generally:

?

PHP code

  1. ? ("@. Action.index ");? ?
  2. //Import the Index.class.php file under the action directory in this project directory. ??
  3. class ? Testaction? extends ? action{??
  4. ???? Public ? function ? Index ()??
  5. ???? {??
  6. ???????? $index = New ? Indexaction (); //instantiation of Indexaction ??
  7. ???????? Echo ? $index ->myhello (); //Call Myhello () method ??
  8. ????}??
  9. }??
  10. ???

So, if we use a function, how to write it?

?

PHP code

  1. class ? Testaction? extends ? action{??
  2. ???? Public ? function ? Index ()??
  3. ???? {??
  4. ???????? $index =a ("Index");??
  5. ???????? Echo ? $index ->myhello ();??
  6. ????}??
  7. }??
  8. ???

?

Oh, write a lot less code. Of course, this is not the most lazy way, please look at the R () function below, you know what is the ultimate lazy.

?

b function

?

This is the new function that comes with behavior, which can perform a behavior such as B (' App_begin '), which is to execute all functions defined by this behavior before the start of the project, supports 2 parameters, and the second parameter supports the need to accept an array, such as B (' App_begin ', array ("name" =& gt; " Tdweb "," Time "=>time ());

?

C function

Get the configuration value, this should be a lot of people use. Although easy to use, the C function is undoubtedly a very powerful function.

Get Value:
Get all settings: C (); Returns an array containing all the settings, without passing any arguments.
Get the specified configuration: C (' Url_model ') so that you can get Url_model configuration information
Gets the specified two-dimensional array configuration: C ("Array.name"), which returns the value corresponding to the name of the key under array

Set the value:
Assign a value of C ("Array.name", "value") to a two-dimensional array, the same principle (get the value of Array.name), and the value behind

?

Batch assignment:?
$test =array ("Url_model" =>1, "Thin_model" =>true ");
C ($test);
This will directly assign the values in the array.

?

To determine whether to assign a value:
C ("? Url_model ") to add a"? "to the front so that if the value is already assigned, it returns true

?

It should be noted that although the value of the configuration is changed here, but only that the page has changed, to the next page will not work, if you want to change the permanent changes, then you need to cooperate with the F function, the configuration file written to config.php line.

?

D function

The DAO function should be the most common function we use to write programs. Similar to a function, if you do not use the D function, you need to import the model, and then the new model, the rest is the same.

?

But the D function has 2 a bit, one is if the previous instance of the model, then no longer instantiated, the remaining resources, the second is convenient debugging, if there is no such model, will throw TP anomaly, very humane.

If you visit the Model Direct D ("model name") of this project, you can use D ("model name", "project name") if you intend to access across projects; the rest is nothing to say.

?

f function

Quickly read and save file data

?

Quickly save the data: F ("MyData", "Here is the data to be saved"), so that a file named mydata.php is stored in the project data directory, the contents of which is the second parameter of the function.

?

Specify save time: F ("MyData", "Here is the data to be saved", "60"), so that if the next time you visit, the interval is greater than 60 seconds to delete the secondary cache file.

?

Specify Save directory: F ("MyData", "Here is the data to be saved", "" "," Data_path "), so it is specified to be saved in the data directory

Delete the cache now: F ("MyData", null), the second parameter passes a null value, so the mydate cache is deleted

?

Read the cached data: F ("MyData") so that the cache is read

?

L function

?

The language definition function, L ("Intro") gets the language defined as intro, L ("Intro", "Introduction") is assigned to intro, and the long-term nature of the assignment is the same as the C function.

?

R function

?

Remember our a function, if just want to execute a method, in fact, with R function more convenient, just can replace

PHP code

  1. class ? Testaction? extends ? action{??
  2. ???? Public ? function ? Index ()??
  3. ???? {??
  4. ???????? $index =r ("Index","MyHello");??
  5. ???????? Echo ? $index ;??
  6. ????}??
  7. }??
  8. ???

? hehe, isn't it easier?

?

s function

Global cache Read and write functions, and C similar, but directly written to the file, write in the temp directory, but in the cache there is a point to note, if the cache name is AAA, then the cache generated file name is MD5 ("AAA") value, it is worth noting.

u function

?

The U function is a very powerful function, it is mainly for URL assembly, while supporting different modes and routing

?

For example:
Get the action address of the current module: U ("/nowmethod");
Get the action address of the current module and pass the parameter: U ("/nowmethod?params=test");

?

(If you are not accustomed to the way above, you can use the U ("/nowmethod", Array ("params" = "test"), such an array way to pass parameters, the effect is the same)

Ways to access other modules: U ("Other/othermethod"), which is to access the Othermethod method under the other module
Cross-Project Access: U ("Appname://other/othermethod");
Use Routing access: U ("Appname://routename@modulename/actionname?params");

?

In addition, if you want to jump directly, then in the second parameter write 1, such as U ("/nowmethod", 1), so that the direct switch to the specified URL

  • 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.