<code><span><?php</span><span>// +--------------------------------------------- ----------------------------------</span><span>// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]</span><span> // +-------------------------------------------------- -----------------------</span><span>// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.</span><span>// +------------------------------------------------- ---------------------</span><span>// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )</span><span>// +- -------------------------------------------------- ------------------</span><span>// | Author: liu21st <liu21st@gmail.com></span><span>// +---------- -------------------------------------------------- ----------</span><span>/*** Think system function library*/</span><span>// Students, in the last class, we have completed the definition of various predefined variables in thinkphp</span><span>// The key points can be divided into The following points: </span><span>// First: For the project document that needs to be loaded by the web, thinkphp uses the dirname method to combine and load it </span><span>// It is characterized by ////// Such slashes </span><span> // For the files that need to be imported, which is the core framework part of thinkphp, the loading method adopts __DIR__ method. </span><span>// It is characterized by backslashes like \\ </span><span>// ps. Of course, these are based on window In the following direction, it is divided into two paths, the ancestor app_PATH and think_PATH</span><span>// Second: What we can record is </span><span>// As a framework program, it needs to be able to record the execution time and memory of the script consumption, so I turned on mirctime and memory_get_usage without hesitation</span><span>// This moment serves as the starting point of time and memory. </span><span>// Third: What deserves our attention is: </span><span>// Use const and define to define system constants, but the feeling is that they must be immutable. Using const</span><span>// means that this is completely solidified Damn, define allows users to modify it when creating their own app. Before the system defines it, it will check whether it is defined. </span><span>// There is no way to redefine const </span><span>// Summary There is basically no difference. The only difference between these two is usage, which is a difference in compilation! </span><span>// Fourth: System predefined variables [GPC] and text data stream </span><span>// Basically it can be said to be non-escaping processing, is there something like addsalshe? </span><span>// Fifth: Interpret php and The communication method of the web server is cgi</span><span>// Determined the operating system</span><span>// Determined whether the command line tool runs away from the server</span><span>// Sixth: Regarding the inconsistency in the definitions of ROOT and _FILE_ files, reworked Multi-platform definition enhances platform portability. </span><span>// In short: it standardizes the definition and its cross-platform features! </span><span>// Next we will explain these public functions with functions.php 20151205</span><span>/*** Instantiate multi-layer controller format: [resource://][module/]controller
*<span> @param</span> string $name resource address
*<span> @param</span> string $layer control layer name
*<span> @param</span> integer $level controller level
*<span> @return</span> ThinkController|false*/</span><span>// This function performs multi-layer controller instantiation function to facilitate its internal calling. It is less used when writing app applications.</span><span><span>function</span><span>A</span><span>(<span>$name</span>,<span>$layer</span>=<span>''</span>,<span>$level</span>=<span>0</span>)</span> {</span><span>static</span><span>$_action</span> = <span>array </span>() ;<span>// The static storage array is defined here to implement the single-column instantiation mode </span><span>$layer</span> = <span>$layer</span>? : C(<span>'DEFAULT_C_LAYER'</span>); <span>//'DEFAULT_C_LAYER' => 'Controller ', // Default controller layer name </span><span>$level</span> = <span>$level</span>? : (<span>$layer</span> == C(<span>'DEFAULT_C_LAYER'</span>)?C(<span>'CONTROLLER_LEVEL'</span>):<span>1</span> ); <span>// 'CONTROLLER_LEVEL' => 1,</span><span>if</span>(<span>isset</span>(<span>$_action</span>[<span>$name</span>.<span>$layer</span>]))<span>// According to the incoming controller and its The corresponding level defaults: Controller 1 level return</span><span>return</span><span>$_action</span>[<span>$name</span>.<span>$layer</span>];
<span>$class</span> = parse_res_name(<span>$name</span>,<span>$layer</span>,<span>$level</span>); <span>// Get the corresponding class name based on the controller name level class name passed in </span><span>if</span>(class_exists(<span> $class</span>)) { <span>// If the class name generated based on the above is instantiated if it exists</span><span>$action</span> = <span>new</span><span>$class</span>(); <span>// Instantiation</span><span>$_action</span>[ <span>$name</span>.<span>$layer</span>] = <span>$action</span>;<span>// Store the instantiated object in a static array </span><span>return</span><span>$action</span>;<span>// Return the instantiation status </span>
}<span>else</span> {
<span>return</span><span>false</span>;
}
<span>// For example: $name = 'admin' The result is $class = AdiminController class under the AdminController.class.php file. </span>
}
<span>// Summary: In fact, this is to return different instantiated objects based on the $name you pass in. The options that can exist for $name are: </span><span>// A('[Project://][Group/]Module','Controller layer name') At present, I feel that this level is basically unused. </span><span>// Waiting for rescue</span><span>// Okay, students, let’s continue today. Yesterday we learned about the A function, which is actually a functional function that instantiates different controller classes based on different parameters </span><span>// Note that the A function is added The name and location of a corresponding class that converts different input parameters are given</span><span>// Next let’s take a look at the function of the B function</span><span>/***Perform an action
*<span> @param</span> string $name behavior name
*<span> @param</span> string $tag tag name (behavior class does not need to be passed in)
*<span> @param</span> Mixed $params passed in parameters
*<span> @return</span> void*/</span><span><span>function</span><span>B</span><span>(<span>$ name</span>, <span>$tag</span>=<span>''</span>,&<span>$params</span>=NULL)</span> {</span><span>if</span>(<span>''</span>==<span>$tag</span>){
<span>$name</span> .= <span>'Behavior'</span>;
}
<span>return</span> ThinkHook::exec(<span>$name</span>,<span>$tag</span>,<span>$params</span>);
}
<span>// Literally speaking, this is a function that performs a certain behavior, </span><span>// If there is no corresponding label, the default behavior is to find the hook function and execute it </span><span>// Another thing to note is its $params In fact, it is an introduction by value, not an ordinary copy by value. In this way, the incoming parameters can be changed without returning. </span><span>// According to the special situation of its hook function, it is generally configured under Addons </span><span>// The default is $name Behavior combination </span><span>// The default execution function is run</span><span>// File location "Addons\{$name }\{$name}Addon";</span><span>// $class = $name.'Behavior';</span><span>// $tag = 'run';</span><span>// return $addon->$tag($params) ;</span><span>// In summary, in fact, the B function is the two types of executing the plug-in [internal/external], introducing the starting position of the plug-in. Execute the start function. </span><span>// return $class->run(parameter);</span><span>// Let’s continue our study. This C function is a very commonly used function. If we say AB, we can generally skip it. This requires us Study it carefully</span><span>//</span><span>/*** Get and set configuration parameters and support batch definition
*<span> @param</span> string|array $name configuration variable
*<span> @param</span> mixed $value configuration value
*<span> @param</span> mixed $default default value
*<span> @return</span> mixed*/</span><span><span>function</span><span>C</span><span>(<span>$name</span>=null, <span>$value</span>=null,<span>$default</span>=null)</span> {</span><span>// Define initialization container, which can only be initialized once </span><span>static</span><span>$_config</span> = <span>array</span>();<span>// Classic static global variable registration, effective when executing a single process, in fact, for multiple pages If loaded differently, the effect will not be obvious. This is a place that can be optimized.</span><span>// Get all cases without parameters 1</span><span>if</span> (<span>empty</span>(<span>$name</span>)) { <span>// This is a big trick, that is, when calling C(), pay attention to the internal When it is empty, all your family members will be returned. </span><span>return</span><span>$_config</span>;
}
<span>// Prioritize execution of setting acquisition or assignment 2</span><span>if</span> (is_string(<span>$name</span>)) { <span>// If it is a string, it will not be in the form of an array </span><span>if</span> (!strpos(<span>$name </span>, <span>'.'</span>)) { <span>// This can be recorded as 2.1. If there is no connection symbol, I think this is a bit too many times, but it is for compatibility with the storage form of the array. Old Liu, it’s really not easy for you. </span><span>$name</span> = strtoupper(<span>$name</span>); <span>// It doesn’t matter what letters, all capital letters, this is actually a good way to deal with compatibility, students can learn from it! </span><span>if</span> (is_null(<span>$value</span>)) <span>// This can actually be divided and is recorded as 2.1.1</span><span>return</span><span>isset</span>(<span>$_config</span>[<span>$name</span>]) ? <span> $_config</span>[<span>$name</span>] : <span>$default</span>; <span>// The ternary here is really clever and can be divided into 2.1.1.1 and 2.1.1.2</span><span>$_config</span>[<span>$name</span>] = ... 'zhangsan'); is to assign name to Zhang San<span></span>// If $name = C('name') is to read the assignment of name, if the above statement has just been executed<span></span>// then $name is Zhang San <span>
}
</span>// Two-dimensional array setting and obtaining support <span></span>$name<span> = explode(</span>'.'<span>, </span>$name<span>); </span>// This only adds support for two-dimensional arrays. There is a problem here, that is, two The child elements of the dimensional array are not uppercase <span></span>$name<span>[</span>0<span>] = strtoupper(</span>$name<span>[</span>0<span>]);
</span>if<span> (is_null(</span>$value<span>))
</span>return<span></span>isset<span>(</span>$_config<span>[</span>$name<span>[</span>0<span>]][</span>$name<span>[</span>1<span>]]) ? </span>$_config<span>[</span>$name<span>[</span>0<span>]] [</span>$name<span>[</span>1<span>]] : </span>$default<span>;
</span>$_config<span>[</span>$name<span>[</span>0<span>]][</span>$name<span>[</span>1<span>]] = </span>$value<span>;
</span>return<span></span>null<span>;
}
</span>// Batch setting Scenario 3: Directly merge data. In fact, it is not very commonly used because it is easy to get confused. For people with low IQ like me, forget it, but I will use it occasionally. <span></span>if<span> (is_array(</span>$name<span>)){
</span>$_config<span> = array_merge(</span>$_config<span>, array_change_key_case(</span>$name<span>,CASE_UPPER));
</span>return<span></span>null<span>;
}
</span>// Other situations <span></span>return<span></span>null<span>; </span>// Avoid illegal parameters<span>
}
</span>// Okay, thank you students, we will continue in the next class! <span></span>// In fact, we talked about the C function in the last course. Here is an idea, try not to be restricted by the configuration file as far as the function is concerned. <span></span>// Today we continue with the D function. This function is used throughout the use of thinkphp. Always.<span>//This is a function that instantiates the Model class</span><span>/*** Instantiate model class format [resource://][module/]model
*<span> @param</span> string $name resource address
*<span> @param</span> string $layer model layer name
*<span> @return</span> ThinkModel*/</span><span><span>function</span><span>D</span><span>(<span>$name</span>=<span>''</span>,<span>$layer</span>=<span>' '</span>)</span> {</span><span>if</span>(<span>empty</span>(<span>$name</span>)) <span>return</span><span>new</span> ThinkModel; <span>//If the input parameter is empty, return the default Model directly</span><span>static</span><span>$_model</span> = <span>array</span>(); <span>// Otherwise, you can create a static instantiation warehouse </span><span>$layer</span> = <span>$layer</span>? : C(<span>'DEFAULT_M_LAYER'</span>); <span>// Confirm the default layer here, that is </span><span>if</span>(<span>isset</span>(<span>$_model</span>[<span>$name</span>.<span>$layer</span>])) <span>// The same principle is returned when it exists. In fact, it is a single-column application idea</span><span>return</span><span>$_model</span> [<span>$name</span>.<span>$layer</span>];
<span>$class</span> = parse_res_name(<span>$name</span>,<span>$layer</span>); <span>//Get the corresponding class name through parsing. This function includes the function of importing files. It’s awesome</span><span>if</span>(class_exists(<span>$ class</span>)) { <span>// If it exists, load it directly and instantiate it</span><span>$model</span> = <span>new</span><span>$class</span>(basename(<span>$name</span>));
}<span>elseif</span>(<span>false</span> === strpos(<span>$name</span>,<span>'/'</span>)){ <span>// If the class file is not found, it means that the class is not found</span><span>// Automatically load the following public modules Model </span><span>if</span>(!C(<span>'APP_USE_NAMESPACE'</span>)){ <span>// Just go to the public model to find it. If the public model is not specified</span>
import(<span>'Common/'</span>.<span>$layer</span>.<span>'/'</span>.<span>$class</span>); <span>//Default public model storage location</span>
}<span>else</span>{
<span>$class</span> = <span>'\Common\'</span>.<span>$layer</span>.<span>'\'</span>.<span>$name</span>.<span>$layer</span>;<span>// If it doesn't work, instantiate the default class</span>
}
<span>$model</span> = class_exists(<span>$class</span>)? <span>new</span><span>$class</span>(<span>$name</span>) : <span>new</span> ThinkModel(<span>$name</span>);
}<span>else</span> { <span>// Otherwise logging error instantiates a basic class and returns it</span>
ThinkLog::record(<span>'D method instantiation did not find model class'</span>.<span>$class</span>,ThinkLog::NOTICE);
<span>$model</span> = <span>new</span> ThinkModel(basename(<span>$name</span>));
}
<span>$_model</span>[<span>$name</span>.<span>$layer</span>] = <span>$model</span>; <span>//Save in history </span><span>return</span><span>$model</span>;<span>//Return the current instantiated class in 3 ways Instantiation of </span>
}
<span>// Throwing an exception is basically an encapsulation and is transferred directly, but there is nothing in its core code. </span><span>// Just inherits PHP’s default exception class </span><span>/*** Throw exception handling
*<span> @param</span> string $msg exception message
*<span> @param</span> integer $code exception code default is 0
*<span> @throws</span> ThinkException
*<span> @return</span> void*/</span><span><span>function</span><span>E</span><span>(<span>$msg</span>, <span>$code</span>=<span>0</span>)</span> {</span> <span>throw</span><span>new</span> Think<span>Exception</span>(<span>$msg</span>, <span>$code</span>);
}
<span>// This is a matter of fast data saving and reading operations through files. </span><span>/*** Fast file data reading and saving for simple data types such as strings and arrays
*<span> @param</span> string $name cache name
*<span> @param</span> mixed $value cache value
*<span> @param</span> string $path cache path
*<span> @return</span> mixed*/</span><span><span>function</span><span>F</span><span>(<span>$name</span>, <span>$value</span>=<span>''</span>, <span>$path</span>=DATA_PATH)</span> {</span><span>static</span><span>$ _cache</span> = <span>array</span>(); <span>// It’s the same old trick, it seems easy to use, </span><span>$filename</span> = <span>$path</span> . <span>$name</span> . <span>'.php'</span>; <span>// File The directory is also very simple.Directly used php file </span><span>if</span> (<span>''</span> !== <span>$value</span>) { <span>// If there is a value </span><span>if</span> (is_null(<span>$value</span>)) { <span>// If there is a value If it is empty</span><span>//Delete cache</span><span>if</span>(<span>false</span> !== strpos(<span>$name</span>,<span>'*'</span>)){ <span>// If there is an * number in the saved object, error</span> <span>return</span><span>false</span>; <span>// TODO</span>
}<span>else</span>{
<span>unset</span>(<span>$_cache</span>[<span>$name</span>]);<span>//Delete data cache</span><span>return</span> ThinkStorage::unlink(<span>$filename</span>,<span>'F'</span>); <span>//Delete data file</span>
}
} <span>else</span> {
ThinkStorage::put(<span>$filename</span>,serialize(<span>$value</span>),<span>'F'</span>); <span>//Write files using serialization</span><span>//Cache data</span><span>$_cache</span>[<span>$ name</span>] = <span>$value</span>; <span>// and write to cache </span><span>return</span><span>null</span>;
}
}
<span>// Get cache data </span><span>if</span> (<span>isset</span>(<span>$_cache</span>[<span>$name</span>])) <span>// It’s very similar to its general C, </span><span>return</span><span>$_cache</span>[<span>$name </span>];
<span>if</span> (ThinkStorage::has(<span>$filename</span>,<span>'F'</span>)){ <span>// Read existing file</span><span>$value</span> = unserialize(ThinkStorage::read(<span>$filename</span>,<span>' F'</span>));
<span>$_cache</span>[<span>$name</span>] = <span>$value</span>; <span>// Return data</span>
} <span>else</span> {
<span>$value</span> = <span>false</span>;
}
<span>return</span><span>$value</span>; <span>//Return data</span>
}
<span>// It is just a cached data reading, which is much worse than file </span><span>// Okay, dear students, continue </span><span>// Here is a reminder, the one in the framework is called functions.php in the application It's called function.php</span><span>// Do you understand the location in the application I'm talking about now?</span><span>// As a function comment, this letter is concise and clear </span><span>/*** Logging and statistics of time (microseconds) and memory usage
* Instructions:
* <code>
* G('begin'); // Record start mark bit
* // ...interval running code
* G('end'); // Record end tag bit
* echo G('begin','end',6); // Statistical interval running time, accurate to 6 decimal places, time in numbers
* echo G('begin','end','m'); //Statistical interval memory usage, memory is represented by m
* If the end mark bit is not defined, the current mark bit will be automatically used.
* The statistical memory usage requires the MEMORY_LIMIT_ON constant to be true to be effective.
* </code>
*<span> @param</span> string $start start tag
*<span> @param</span> string $end end tag
*<span> @param</span> integer|string $dec decimal place or m
*<span> @return</span> mixed*/</span><span>// I have to say here that the founder of thinkphp particularly likes to do something, that is, according to Different input parameters have different meanings </span><span>// For example, C function and F function, if only a single parameter is entered, it means reading a number, 2 parameters means setting a value, and 3 parameters generally add a default value </span><span>/ /number_format — Format a number with thousands separators</span><span>// $nombre_format_francais = number_format($number, 2, ',', ' ');</span><span><span>function</span><span>G</span><span>(<span>$start</span>, <span>$end</span>=<span>''</span>,<span>$dec</span>=<span>4</span>)</span> {</span><span>static</span><span>$_info</span> = <span>array</span>(); <span>// This is the time warehouse </span><span>static</span><span>$_mem </span> = <span>array</span>(); <span>// This is the memory warehouse </span><span>if</span>(is_float(<span>$end</span>)) { <span>// If the record time is passed as G('start',2342353234.453); it is a record Keep consistent with the above style</span><span>// When a decimal is passed in, it ends</span><span>$_info</span>[<span>$start</span>] = <span>$end</span>; <span>// Or if the passed value is like this G('start',microtime(TRUE ));</span>
}<span>elseif</span>(!<span>empty</span>(<span>$end</span>)){ <span>// Statistics of time and memory usage, that is, its default priority is time</span><span>// If there is a non-digit ending, the difference is returned</span><span>if</span> (!<span>isset</span>(<span>$_info</span>[<span>$end</span>])) <span>$_info</span>[<span>$end</span>] = microtime(<span>TRUE</span>);
<span>if</span>(MEMORY_LIMIT_ON && <span>$dec</span>==<span>'m'</span>){ <span>// If memory logging is turned on and it is clearly a memory record </span><span>if</span>(!<span>isset</span>(<span>$_mem</span>[<span>$ end</span>])) <span>$_mem</span>[<span>$end</span>] = memory_get_usage(); <span>// Get the memory record </span><span>return</span> number_format((<span>$_mem</span>[<span>$end</span>]-<span>$_mem</span>[<span> $start</span>])/<span>1024</span>); <span>// Get the formatted value returned</span>
}<span>else</span>{
<span>return</span> number_format((<span>$_info</span>[<span>$end</span>]-<span>$_info</span>[<span>$start</span>]),<span>$dec</span>); <span>// Return the number of formatted digits. The default is 4 decimal places</span>
}
}<span>else</span>{ <span>//Record time and memory usage</span><span>// Separately, it records the memory and time flags synchronously.</span><span>$_info</span>[<span>$start</span>] = microtime(<span>TRUE</span>);
<span>if</span>(MEMORY_LIMIT_ON) <span>$_mem</span>[<span>$start</span>] = memory_get_usage();
}
<span>return</span><span>null</span>;
}
<span>// No H function </span><span>// That’s it for the interview this morning, thank you! </span><span>// Well, I was a little rushed yesterday. In fact, this G is a function that records time and memory. It has the attributes of the second and third parameters. </span><span>// It distinguishes whether it is the recorded time or something else, but no matter No matter what, the time is recorded to the memory at the same time</span><span>// The recording of time and memory can reflect the performance of the PHP program from one angle</span><span>// Next is our powerful I input filter function, which supports Default value</span><span>/*** Get input parameters and support filtering and default values
* Instructions:
* <code>
* I('id',0); Get the id parameter and automatically determine get or post // Well, these examples you gave are indeed very commonly used.
* I('post.name','','htmlspecialchars'); Get $_POST['name']
* I('get.'); Get $_GET
* </code>
*<span> @param</span> string $name The name of the variable supports specified types
*<span> @param</span> mixed $default Default value when it does not exist
*<span> @param</span> mixed $filter parameter filtering method
*<span> @param</span> mixed $datas Additional data sources to obtain
*<span> @return</span> mixed*/</span><span><span>function</span><span>I</span><span>(<span>$name</span>,<span>$default</span>=<span>''</span>,<span>$filter</span>=null,<span>$datas</span>=null )</span> {</span><span>// The first step: Specify the warehouse </span><span>static</span><span>$_PUT</span> = <span>null</span>; <span>// Default single data warehouse </span><span>// The second step: Determine the input type </span><span>if</span>(strpos (<span>$name</span>,<span>'/'</span>)){ <span>//Specify modifiers</span><span>list</span>(<span>$name</span>,<span>$type</span>) = explode(<span>'/'</span>,<span>$name</span>,<span> 2</span>);
}<span>elseif</span>(C(<span>'VAR_AUTO_STRING'</span>)){ <span>// Default forced conversion to string</span><span>// // Whether input variables are automatically forced to be converted to strings. If it is enabled, array variables need to be manually passed in variable modifications The symbol gets the variable </span><span>// In fact, the default value of the above is false</span><span>$type</span> = <span>'s'</span>;
}
<span>// The third step: Obtain the data source </span><span>// The third step: The first small step: decompose the data source </span><span>// In general programs, the above two are not used, that is, specify The data type is not specified by default.</span><span>if</span>(strpos(<span>$name</span>,<span>'.'</span>)) { <span>//Specify parameter source</span><span>list</span>(<span>$method</span>,<span>$name</span>) = explode(<span>'.'</span>, <span>$name</span>,<span>2</span>);
}<span>else</span>{ <span>//The default is automatic judgment</span><span>$method</span> = <span>'param'</span>;
}
<span>// The third step: the second small step: associate the data source </span><span>// Specify the data source, the commonly used one is get post </span><span>switch</span>(strtolower(<span>$method</span>)) { <span>// Actually this is used Very classic, lowercase before comparison</span><span>case</span><span>'get'</span>:
<span>$input</span> =& <span>$_GET</span>; <span>// It’s also good to use to get the address, very creative</span><span>break</span>;
<span>case</span><span>'post'</span> :
<span>$input</span> =& <span>$_POST</span>;
<span>break</span>;
<span>case</span><span>'put'</span> :
<span>if</span>(is_null(<span>$_PUT</span>)){
parse_str(file_get_contents(<span>'php://input'</span>), <span>$_PUT</span>);
}
<span>$input</span> = <span>$_PUT</span>;
<span>/*
Read POST data
Cannot be used with multipart/form-data types
php://input VS $HTTP_RAW_POST_DATA
Read POST data */</span><span>break</span>;
<span>case</span><span>'param'</span> :<span>// In fact, this is the most unscientific. In order to be compatible with lazy programming, </span><span>switch</span>(<span>$_SERVER</span>[<span>'REQUEST_METHOD'</span>]) {
<span>case</span><span>'POST'</span>:
<span>$input</span> = <span>$_POST</span>;
<span>break</span>;
<span>case</span><span>'PUT'</span>:
<span>if</span>(is_null(<span>$_PUT</span>)){
parse_str(file_get_contents(<span>'php://input'</span>), <span>$_PUT</span>);
}
<span>$input</span> = <span>$_PUT</span>;
<span>break</span>;
<span>default</span>:
<span>$input</span> = <span>$_GET</span>;
}
<span>break</span>;
<span>// Three commonly used input acquisition methods GET POST PUT</span><span>case</span><span>'path'</span> : <span>// There is actually path acquisition, which I have never used in my calls</span><span>$input</span> = <span>array</span>() ;
<span>if</span>(!<span>empty</span>(<span>$_SERVER</span>[<span>'PATH_INFO'</span>])){
<span>$depr</span> = C(<span>'URL_PATHINFO_DEPR'</span>);<span>// Path separator </span><span>//'URL_PATHINFO_DEPR' => '/', // In PATHINFO mode, the separation symbol between each parameter </span><span>$ input</span> = explode(<span>$depr</span>,trim(<span>$_SERVER</span>[<span>'PATH_INFO'</span>],<span>$depr</span>));
}
<span>break</span>;
<span>case</span><span>'request'</span> :
<span>$input</span> =& <span>$_REQUEST</span>;
<span>break</span>;
<span>case</span><span>'session'</span> :
<span>$input</span> =& <span>$_SESSION</span>;
<span>break</span>;
<span>case</span><span>'cookie'</span> :
<span>$input</span> =& <span>$_COOKIE</span>;
<span>break</span>;
<span>case</span><span>'server'</span> :
<span>$input</span> =& <span>$_SERVER</span>;
<span>break</span>;
<span>case</span><span>'globals'</span> :
<span>$input</span> =& <span>$GLOBALS</span>;
<span>break</span>;
<span>case</span><span>'data'</span> :
<span>$input</span> =& <span>$datas</span>;
<span>break</span>;
<span>default</span>:
<span>return</span><span>null</span>;
}
<span>// Step 4: Get the variables clearly </span><span>// 4.1 Get all the values </span><span>if</span>(<span>''</span>==<span>$name</span>) { <span>// Get all the variables </span><span>$data</span> = <span>$input </span>;
<span>// Continue filtering with the filter function </span><span>$filters</span> = <span>isset</span>(<span>$filter</span>)?<span>$filter</span>:C(<span>'DEFAULT_FILTER'</span>);
<span>if</span>(<span>$filters</span>) {
<span>if</span>(is_string(<span>$filters</span>)){
<span>$filters</span> = explode(<span>','</span>,<span>$filters</span>);
}
<span>foreach</span>(<span>$filters</span><span>as</span><span>$filter</span>){
<span>$data</span> = array_map_recursive(<span>$filter</span>,<span>$data</span>); <span>// Parameter filtering</span>
}
}
<span>// 4.2 Get the specified value</span>
}<span>elseif</span>(<span>isset</span>(<span>$input</span>[<span>$name</span>])) { <span>// Value operation if a value is specified </span><span>$data</span> = <span>$input</span>[<span>$name</span>]; <span> //Data acquisition completed</span><span>//Start filtering</span><span>$filters</span> = <span>isset</span>(<span>$filter</span>)?<span>$filter</span>:C(<span>'DEFAULT_FILTER'</span>);
<span>// Filter exists. Start filtering</span><span>if</span>(<span>$filters</span>) {
<span>if</span>(is_string(<span>$filters</span>)){
<span>if</span>(<span>0</span> === strpos(<span>$filters</span>,<span>'/'</span>)){
<span>if</span>(<span>1</span> !== preg_match(<span>$filters</span>,(string)<span>$data</span>)){ <span>// Filter supports regular expressions</span><span>// Supports regular expression verification</span><span>return</span><span>isset</span>(<span> $default</span>) ? <span>$default</span> : <span>null</span>;
}
}<span>else</span>{
<span>$filters</span> = explode(<span>','</span>,<span>$filters</span>);
}
}<span>elseif</span>(is_int(<span>$filters</span>)){
<span>$filters</span> = <span>array</span>(<span>$filters</span>);
}
<span>// Perform array filtering</span><span>if</span>(is_array(<span>$filters</span>)){
<span>foreach</span>(<span>$filters</span><span>as</span><span>$filter</span>){
<span>if</span>(function_exists(<span>$filter</span>)) {
<span>$data</span> = is_array(<span>$data</span>) ? array_map_recursive(<span>$filter</span>,<span>$data</span>) : <span>$filter</span>(<span>$data</span>); <span>//Parameter filtering</span>
}<span>else</span>{
<span>$data</span> = filter_var(<span>$data</span>,is_int(<span>$filter</span>) ? <span>$filter</span> : filter_id(<span>$filter</span>));
<span>if</span>(<span>false</span> === <span>$data</span>) {
<span>return</span><span>isset</span>(<span>$default</span>) ? <span>$default</span> : <span>null</span>;
}
}
}
}
}
<span>//Specify the output data type. Default string</span><span>if</span>(!<span>empty</span>(<span>$type</span>)){
<span>switch</span>(strtolower(<span>$type</span>)){
<span>case</span><span>'a'</span>: <span>// array</span><span>$data</span> = (<span>array</span>)<span>$data</span>;
<span>break</span>;
<span>case</span><span>'d'</span>: <span>// Number </span><span>$data</span> = (int)<span>$data</span>;
<span>break</span>;
<span>case</span><span>'f'</span>: <span>// Floating point</span><span>$data</span> = (float)<span>$data</span>;
<span>break</span>;
<span>case</span><span>'b'</span>: <span>// Boolean</span><span>$data</span> = (boolean)<span>$data</span>;
<span>break</span>;
<span>case</span><span>'s'</span>: <span>// string </span><span>default</span>:
<span>$data</span> = (string)<span>$data</span>;
}
}
<span>//4.3 Get the default value</span>
}<span>else</span>{ <span>// Variable default value</span><span>$data</span> = <span>isset</span>(<span>$default</span>)?<span>$default</span>:<span>null</span>;
}
<span>// Finally, before returning the data, it is processed, that is, if it is an array, the default filter function is executed</span>
is_array(<span>$data</span>) && array_walk_recursive(<span>$data</span>,<span>'think_filter'</span>);
<span>return</span><span>$data</span>;
}
<span>// In summary, in fact, after the analysis of the above functions, you can learn roughly like this: </span><span>// First: Write branch code in steps similar to the first step: 1.1 1.2 The second step: 2.1 2.2 Like this </span><span>// Second: It still runs through its tradition. The feature of adjusting its output through parameters is that the styles of various parameters are compatible with each other</span><span>// Third: It is the convenient combination of various filter functions. Really good! </span><span>// I like you, haha! </span><span>// No J function </span><span>// No K function </span><span>// When encountering this L function, it is usually used for language configuration.</span><span>/*** Get and set language definition (case-insensitive)
*<span> @param</span> string|array $name language variable
*<span> @param</span> mixed $value language value or variable
*<span> @return</span> mixed*/</span><span><span>function</span><span>L</span><span>(<span>$name</span>=null, <span>$value</span>=null)</span> {</span><span>static</span><span>$_lang</span> = <span>array</span>() ;<span>//Old pace, define warehouse</span><span>// Empty parameters return all definitions</span><span>// Three ways</span><span>// The first way: empty</span><span>if</span> (<span>empty</span>(<span>$name</span>) ) <span>//Old pace: no input returns all</span><span>return</span><span>$_lang</span>;
<span>// Determine the language acquisition (or setting) </span><span>// If it does not exist, directly return the all-capital $name</span><span>// If the string </span><span>// The second way: the string is then subdivided into the empty array and is recorded by default The return here is actually an artifact </span><span>if</span> (is_string(<span>$name</span>)) { <span>// If it is a string </span><span>$name</span> = strtoupper(<span>$name</span>); <span>// Step 1: Everything Convert to uppercase </span><span>if</span> (is_null(<span>$value</span>)){ <span>// Determine whether to set or read </span><span>return</span><span>isset</span>(<span>$_lang</span>[<span>$name</span>]) ? <span>$_lang</span> [<span>$name</span>] : <span>$name</span>; <span>// If there is a definition, return the definition. If there is no definition, return directly </span>
}<span>elseif</span>(is_array(<span>$value</span>)){ <span>// If it is an array </span><span>// Support variables </span><span>$replace</span> = array_keys(<span>$value</span>); <span>// Returns all key names in the array A new array of: </span><span>foreach</span>(<span>$replace</span><span>as</span> &<span>$v</span>){ <span>// It’s so complicated. I didn’t understand this section. Hey, if you can understand it, please reply downstairs! Thanks </span><span>$v</span> = <span>'{$'</span>.<span>$v</span>.<span>'}'</span>;
}
<span>return</span> str_replace(<span>$replace</span>,<span>$value</span>,<span>isset</span>(<span>$_lang</span>[<span>$name</span>]) ? <span>$_lang</span>[<span>$name</span>] : <span>$name</span>);
}
<span>$_lang</span>[<span>$name</span>] = <span>$value</span>; <span>// Language definition Otherwise, define </span><span>return</span><span>null</span>;
}
<span>// Batch definition </span><span>// The third way: array </span><span>if</span> (is_array(<span>$name</span>)) <span>// Batch definition array_change_key_case() function converts all keys of the array to uppercase letters or lowercase letter. Default uppercase </span><span>$_lang</span> = array_merge(<span>$_lang</span>, array_change_key_case(<span>$name</span>, CASE_UPPER));
<span>return</span><span>null</span>;
}
<span>// A particularly commonly used function, but I will recommend the D function later, but any function has its own characteristics</span><span>/*** Instantiate a Model without a model file
*<span> @param</span> string $name Model name supports specifying basic models, such as MongoModel:User
*<span> @param</span> string $tablePrefix table prefix
*<span> @param</span> mixed $connection database connection information
*<span> @return</span> ThinkModel*/</span><span>//</span><span><span>function</span><span>M</span><span>( <span>$name</span>=<span>''</span>, <span>$tablePrefix</span>=<span>''</span>,<span>$connection</span>=<span>''</span>)</span> {</span><span>static</span><span>$_model</span> = <span>array</span>();<span>// An immutable warehouse </span><span>if</span>(strpos(<span>$name</span>,<span>':'</span>)) { <span>// You can combine its code and then splice it into a class, followed by the class name </span><span>list</span>(<span>$class</span>,<span>$ name</span>) = explode(<span>':'</span>,<span>$name</span>);
}<span>else</span>{
<span>$class</span> = <span>'Think\Model'</span>; <span>// Otherwise, execute the default Model class instantiation</span>
}
<span>// This is equivalent to making a unique value</span><span>$guid</span> = (is_array(<span>$connection</span>)?implode(<span>''</span>,<span>$connection</span>):<span>$connection</span>).<span>$tablePrefix</span> . <span>$name</span> . <span>'_'</span> . <span>$class</span>;
<span>if</span> (!<span>isset</span>(<span>$_model</span>[<span>$guid</span>])) <span>// Single column single column</span><span>$_model</span>[<span>$guid</span>] = <span>new</span><span>$class</span>(<span>$name</span> ,<span>$tablePrefix</span>,<span>$connection</span>); <span>// Instantiate the saved single column </span><span>return</span><span>$_model</span>[<span>$guid</span>]; <span>// I won’t say much more about this, that’s it.</span>
}
<span>/*** Set and get statistics
* Instructions:
* <code>
* N('db',1); // Record the number of database operations
* N('read',1); // Record the number of reads
* echo N('db'); // Get the number of operations on the current page database
* echo N('read'); // Get the number of times the current page has been read
* </code>
*<span> @param</span> string $key identifies the location
*<span> @param</span> integer $step step value
*<span> @param</span> boolean $save whether to save the result
*<span> @return</span> mixed*/</span><span><span>function</span><span>N</span><span>(<span>$key</span>, <span>$step</span>=<span>0</span>,<span>$save</span>=false)</span> {</span><span>static</span><span>$_num</span> = <span>array</span>(); <span>// Warehouse</span><span>if</span> (!<span>isset</span>(<span>$_num</span>[<span>$key</span>])) { <span>// If the current value </span><span>$_num</span>[<span> $key</span>] = (<span>false</span> !== <span>$save</span>)? S(<span>'N_'</span>.<span>$key</span>) : <span>0</span>; <span>//If storage is set, it is in the S function, read Take processing, otherwise it will be 0</span>
}
<span>if</span> (<span>empty</span>(<span>$step</span>)){ <span>//If there is no step setting</span><span>return</span><span>$_num</span>[<span>$key</span>];
}<span>else</span>{ <span>// Otherwise, proceed in step way</span><span>$_num</span>[<span>$key</span>] = <span>$_num</span>[<span>$key</span>] + (int)<span>$step</span>;
}
<span>if</span>(<span>false</span> !== <span>$save</span>){ <span>// Save the result. In fact, this is through the cache reading function. </span>
S(<span>'N_'</span>.<span>$key</span>,<span>$_num</span>[<span>$key</span>],<span>$save</span>);
}
<span>return</span><span>null</span>;
}
<span>// No O function </span><span>// No P function </span><span>// No Q function </span><span>// This is the end of today. It tells about the L M N function language package M. Instantiation can specify the instantiation class and the connected database N. Recording steps </span><span>// Sorry, I’m confused. There was no update yesterday and it was only updated today</span><span>/***Remotely call the operation method of the controller URL parameter format [resource://][module/]controller/operation
*<span> @param</span> string $url calling address
*<span> @param</span> string|array $vars calling parameters support strings and arrays
*<span> @param</span> string $layer The name of the control layer to be called
*<span> @return</span> mixed*/</span><span>// Parse the query string into a variable</span><span>// The parse_str() function parses the query Strings are parsed into variables. </span><span> // Note: The magic_quotes_gpc setting in the php.ini file affects the output of this function. If enabled, variables are converted by addslashes() before being parsed by parse_str() . </span><span>/**parse_str("name=Bill&age=60");
echo $name."<br>";
echo $age;
*
parse_str("name=Bill&age=60",$myArray);
print_r($myArray);*/</span><span>// print_r(pathinfo("/testweb/test.txt"));</span><span>// pathinfo() returns an associative array containing path information. </span><span>/**Array
(
[dirname] => /testweb
[basename] => test.txt
[extension] => txt
)
* [dirname]
[basename]
[extension]*/</span><span>/**Class ClassA
{
function bc($b, $c) {
$bc = $b + $c;
echo $bc;
}
}
call_user_func_array(array('ClassA','bc'), array("111", "222"));
//显示 333*/</span><span><span>function</span><span>R</span><span>(<span>$url</span>,<span>$vars</span>=array<span>()</span>,<span>$layer</span> =<span>''</span>)</span> {</span><span>$info</span> = pathinfo(<span>$url</span>); <span>// Parse path </span><span>$action</span> = <span>$info</span>[<span>'basename'</span>]; <span>// Get file Name</span><span>$module</span> = <span>$info</span>[<span>'dirname'</span>]; <span>// Get the file path </span><span>$class</span> = A(<span>$module</span>,<span>$layer</span>); <span>// Get the actual class Instantiate an additional level of relationship such as Widget</span><span>if</span>(<span>$class</span>){ <span>// If there is a class</span><span>if</span>(is_string(<span>$vars</span>)) { <span>// If there are variables passed in</span>
parse_str(<span>$vars</span>,<span>$vars</span>); <span>// Parse the incoming parameters into the array</span>
}
<span>return</span> call_user_func_array(<span>array</span>(&<span>$class</span>,<span>$action</span>.C(<span>'ACTION_SUFFIX'</span>)),<span>$vars</span>); <span>//</span>
}<span>else</span>{
<span>return</span><span>false</span>;
}
}
<span>// In summary, in fact, this R is an upgraded version of call_user_func_array, which is processed directly through the url. </span><span>// One and a half hours left. Today ends. Continue today's study</span><span>// This function is actually a very awesome function. It seems that you can use the F function. Let's compare it and see what the difference is between these two ghosts.</span><span>/*** Cache management
*<span> @param</span> mixed $name cache name, if cache settings are array representation
*<span> @param</span> mixed $value cache value
*<span> @param</span> mixed $options cache parameters
*<span> @return</span> mixed*/</span><span><span>function</span><span>S</span><span>(<span>$name</span>,<span>$value</span>=<span>''</span>,<span>$options</span>=null)</span> {</span><span>static</span><span>$ cache</span> = <span>''</span>; <span>// Warehouse warehouse warehouse is a warehouse </span><span>//The first step: initialization </span><span>if</span>(is_array(<span>$options</span>)){ <span>// If the cache parameters are actually a bit messy type It can be placed in any location</span><span>// The simultaneous initialization of cache operations is actually an initialization process</span><span>$type</span> = <span>isset</span>(<span>$options</span>[<span>'type'</span>])?<span>$options</span>[ <span>'type'</span>]:<span>''</span>;
<span>$cache</span> = ThinkCache::getInstance(<span>$type</span>,<span>$options</span>);
}<span>elseif</span>(is_array(<span>$name</span>)) { <span>// Cache initialization // If the cache parameters are actually a bit messy, they can be placed in any position </span><span>$type</span> = <span>isset</span>(<span>$name</span>[<span> 'type'</span>])?<span>$name</span>[<span>'type'</span>]:<span>''</span>;
<span>$cache</span> = ThinkCache::getInstance(<span>$type</span>,<span>$name</span>);
<span>return</span><span>$cache</span>;
}<span>elseif</span>(<span>empty</span>(<span>$cache</span>)) { <span>//Automatic initialization If not done yet</span><span>$cache</span> = ThinkCache::getInstance(); <span>//Initialization</span>
}
<span>// Design based on the data </span><span>if</span>(<span>''</span>=== <span>$value</span>){ <span>// Get cache</span><span>return</span><span>$cache</span>->get(<span>$name</span>); <span>//Get data</span>
}<span>elseif</span>(is_null(<span>$value</span>)) { <span>//Delete cache</span><span>return</span><span>$cache</span>->rm(<span>$name</span>); <span>//Delete data</span>
}<span>else</span> { <span>// Cache data </span><span>if</span>(is_array(<span>$options</span>)) {
<span>$expire</span> = <span>isset</span>(<span>$options</span>[<span>'expire'</span>])?<span>$options</span>[<span>'expire'</span>]:<span>NULL</span>;
}<span>else</span>{
<span>$expire</span> = is_numeric(<span>$options</span>)?<span>$options</span>:<span>NULL</span>;
}
<span>return</span><span>$cache</span>->set(<span>$name</span>, <span>$value</span>, <span>$expire</span>); <span>// Save data</span>
}
}
<span>// In summary, what is this actually for? The key point is the class function </span><span>// In fact, that function is nothing anymore </span><span>// Today I learned a new thing, which is to write a template engine </span><span>/*** Get template file format resource://module@theme/controller/operation
*<span> @param</span> string $template template resource address
*<span> @param</span> string $layer view layer (directory) name
*<span> @return</span> string*/</span><span><span>function</span><span>T</span><span>(<span>$template</span>=<span>''</span>,<span>$layer</span>=<span>''</span>)</span>{</span><span>//The first step to parse the template resource address: </span><span> if</span>(<span>false</span> === strpos(<span>$template</span>,<span>'://'</span>)){
<span>$template</span> = <span>'http://'</span>.str_replace(<span>':'</span>, <span>'/'</span>,<span>$template</span>);
}
<span>$info</span> = parse_url(<span>$template</span>); <span>// Step 2: Parse into your own array </span><span>$file</span> = <span>$info</span>[<span>'host'</span>].(<span>isset</span>(<span> $info</span>[<span>'path'</span>])?<span>$info</span>[<span>'path'</span>]:<span>''</span>);
<span>$module</span> = <span>isset</span>(<span>$info</span>[<span>'user'</span>])?<span>$info</span>[<span>'user'</span>].<span>'/'</span>:MODULE_NAME.<span>'/'</span>; <span>/ / Extend username</span><span>$extend</span> = <span>$info</span>[<span>'scheme'</span>]; <span>// Extend file extension</span><span>$layer</span> = <span>$layer</span>?<span>$layer</span>:C(<span>'DEFAULT_V_LAYER '</span>); <span>// Level </span><span>// Get the template path of the current theme </span><span>$auto</span> = C(<span>'AUTOLOAD_NAMESPACE'</span>);
<span>if</span>(<span>$auto</span> && <span>isset</span>(<span>$auto</span>[<span>$extend</span>])){ <span>// Extended resource </span><span>$baseUrl</span> = <span>$auto</span>[<span>$extend</span>].<span>$ module</span>.<span>$layer</span>.<span>'/'</span>;
}<span>elseif</span>(C(<span>'VIEW_PATH'</span>)){
<span>//Change module view directory</span><span>$baseUrl</span> = C(<span>'VIEW_PATH'</span>);
}<span>elseif</span>(defined(<span>'TMPL_PATH'</span>)){
<span>//Specify the global view directory</span><span>$baseUrl</span> = TMPL_PATH.<span>$module</span>;
}<span>else</span>{
<span>$baseUrl</span> = APP_PATH.<span>$module</span>.<span>$layer</span>.<span>'/'</span>;
}
<span>// Get theme</span><span>$theme</span> = substr_count(<span>$file</span>,<span>'/'</span>)<<span>2</span> ? C(<span>'DEFAULT_THEME'</span>) : <span>''</span>;
<span>//Analysis template file rules</span><span>$depr</span> = C(<span>'TMPL_FILE_DEPR'</span>);
<span>if</span>(<span>''</span> == <span>$file</span>) {
<span>// If the template file name is empty, locate according to the default rules </span><span>$file</span> = CONTROLLER_NAME . <span>$depr</span> . ACTION_NAME;
}<span>elseif</span>(<span>false</span> === strpos(<span>$file</span>, <span>'/'</span>)){
<span>$file</span> = CONTROLLER_NAME . <span>$depr</span> . <span>$file</span>;
}<span>elseif</span>(<span>'/'</span> != <span>$depr</span>){
<span>$file</span> = substr_count(<span>$file</span>,<span>'/'</span>)><span>1</span> ? substr_replace(<span>$file</span>,<span>$depr</span>,strrpos(<span>$file</span>,<span>'/'</span>), <span>1</span>) : str_replace(<span>'/'</span>, <span>$depr</span>, <span>$file</span>);
}
<span>return</span><span>$baseUrl</span>.(<span>$theme</span>?<span>$theme</span>.<span>'/'</span>:<span>''</span>).<span>$file</span>.C(<span>'TMPL_TEMPLATE_SUFFIX'</span>);
}
<span>// In summary, in fact, this product just returns a real URL path</span><span>// Today is this new thing, the assembled product</span><span>/**
* URL assembly supports different URL patterns
*<span> @param</span> string $url URL expression, format: '[module/controller/operation#anchor@domain name]?parameter1=value1¶meter2=value2...'
*<span> @param</span> string|array $vars passed in parameters, supports arrays and strings
*<span> @param</span> string|boolean $suffix pseudo-static suffix, the default is true to obtain the configuration value
*<span> @param</span> boolean $domain whether to display the domain name
*<span> @return</span> string
* This function is not used to verify the validity of the given URL, only to break it into the parts listed below. Incomplete URLs are also accepted and parse_url() will try to parse them as correctly as possible.* Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value after question mark ?
[fragment] => anchor after the hash symbol #
)
* $url = 'http://username:password@hostname/path?arg=value#anchor';
*/</span><span><span>function</span><span>U</span><span>(<span>$url</span>=<span>''</span>,<span>$vars</span>=<span>''</span>,<span>$suffix</span>=true,<span>$domain</span>=false)</span> {</span><span> // Parse the URL. In fact, the url passed in here is not the URL of the person at the normal address. He has re-assembled it. I personally think it is not very scientific. </span><span>$info</span> = parse_url(<span>$url</span>); <span>// Parsing of the parsing parameters here The method is not the same as the normal one</span><span>// Situation 1</span><span>// $url = 'Home/Index/index#zhangsan@www.maizi.net?name=lisi&age=32';</span><span>// var_dump(parse_url ($url));</span><span>//array (size=2)</span><span>//'path' => string 'Home/Index/index' (length=16)</span><span>// 'fragment' => string 'zhangsan@www.maizi.net?name=lisi&age=32' (length=39)</span><span>// Case 2</span><span>// $url = 'Home/Index/index@www.maizi.net?name=lisi&age= 32';</span><span>// var_dump(parse_url($url));</span><span>// array (size=2)</span><span>// 'path' => string 'Home/Index/index@www.maizi.net' (length=30)</span><span>// 'query' => string 'name=lisi&age=32' (length=16)</span><span>$url</span> = !<span>empty</span>(<span>$info</span>[<span>'path'</span>] )?<span>$info</span>[<span>'path'</span>]:ACTION_NAME; <span>// If the path is parsed, use the parsed path, otherwise use action_name</span><span>if</span>(<span>isset</span>(<span>$info</span>[<span>' fragment'</span>])) { <span>// The parsing anchor point is a mark in the web page that jumps to a fixed location on the website </span><span>$anchor</span> = <span>$info</span>[<span>'fragment'</span>]; <span>// In fact, this is all '[module/controller/action#anchor@domain name]?parameter1=value1¶meter2=value2...'</span><span>if</span>(<span>false</span> !== strpos(<span>$anchor</span>,<span>' ?'</span>)) { <span>// Parse parameters if there are following parameters after the anchor </span><span>list</span>(<span>$anchor</span>,<span>$info</span>[<span>'query'</span>]) = explode(<span>'?'</span> ,<span>$anchor</span>,<span>2</span>);
}
<span>if</span>(<span>false</span> !== strpos(<span>$anchor</span>,<span>'@'</span>)) { <span>// After parsing the domain name, if there is @ domain name after anchoring, </span><span>list</span>(<span>$anchor</span>,<span> $host</span>) = explode(<span>'@'</span>,<span>$anchor</span>, <span>2</span>);
}
}<span>elseif</span>(<span>false</span> !== strpos(<span>$url</span>,<span>'@'</span>)) { <span>//Resolve the domain name and separate the username and password from the domain name</span><span>list</span>(<span>$url</span>,<span> $host</span>) = explode(<span>'@'</span>,<span>$info</span>[<span>'path'</span>], <span>2</span>);
<span>// '[Module/Controller/Operation@Domain Name]? Parameter 1=Value 1&Parameter 2=Value 2...' This is incomplete</span>
}
<span>// Parsing the subdomain name host is the domain name </span><span>if</span>(<span>isset</span>(<span>$host</span>)) { <span>// Isn’t it a second-level domain name? In fact, under normal circumstances, there is no such thing </span><span>// In fact, this usage It's strange. Usually, it's $domain = $host. Here it may follow the parameters </span><span>$domain</span> = <span>$host</span>.(strpos(<span>$host</span>,<span>'.'</span>)?<span>''</span>: strstr(<span>$_SERVER</span>[<span>'HTTP_HOST'</span>],<span>'.'</span>));
}<span>elseif</span>(<span>$domain</span>===<span>true</span>){ <span>//If the displayed domain name is added, it is not a true boolen type. </span><span>$domain</span> = <span>$_SERVER</span>[<span>'HTTP_HOST' </span>]; <span>//Display the host name and domain name </span><span>if</span>(C(<span>'APP_SUB_DOMAIN_DEPLOY'</span>) ) { <span>// Enable subdomain deployment is not enabled by default </span><span>$domain</span> = <span>$domain</span>==<span>' localhost'</span>?<span>'localhost'</span>:<span>'www'</span>.strstr(<span>$_SERVER</span>[<span>'HTTP_HOST'</span>],<span>'.'</span>); <span>// By default, www.your domain name is cached for him Ignore the local ones</span><span>// 'Subdomain name'=>array('Module[/controller]'); Find the matching rules for the subdomain name. In fact, it may not be used</span><span>foreach</span> (C( <span>'APP_SUB_DOMAIN_RULES'</span>) <span>as</span><span>$key</span> => <span>$rule</span>) {<span>// Processing subdomain rules</span><span>$rule</span> = is_array(<span>$rule</span>)?<span>$rule</span>[<span>0 </span>]:<span>$rule</span>;
<span>if</span>(<span>false</span> === strpos(<span>$key</span>,<span>'*'</span>) && <span>0</span>=== strpos(<span>$url</span>,<span>$rule</span>)) {
<span>$domain</span> = <span>$key</span>.strstr(<span>$domain</span>,<span>'.'</span>); <span>// Generate the corresponding subdomain </span><span>$url</span> = substr_replace(<span>$url</span>,<span>''</span>,<span> 0</span>,strlen(<span>$rule</span>));
<span>break</span>;
}
}
}
}
<span>// Parse parameters Parse parameters, this is the parameter passed in later</span><span>if</span>(is_string(<span>$vars</span>)) { <span>// aaa=1&bbb=2 converted into an array</span>
parse_str(<span>$vars</span>,<span>$vars</span>);
}<span>elseif</span>(!is_array(<span>$vars</span>)){
<span>$vars</span> = <span>array</span>();
}
<span>// Merge parameters </span><span>if</span>(<span>isset</span>(<span>$info</span>[<span>'query'</span>])) { <span>// Parse the parameters in the address and merge them into vars</span>
parse_str(<span>$info</span>[<span>'query'</span>],<span>$params</span>);
<span>$vars</span> = array_merge(<span>$params</span>,<span>$vars</span>);
}
<span>// To summarize here, it is actually a large-scale step. The first step is splitting </span><span>// The second step: assembly </span><span>// URL assembly </span><span>$depr</span> = C(<span>'URL_PATHINFO_DEPR'</span>); <span>/ /'/', // In PATHINFO mode, the separation symbol between each parameter </span><span>$urlCase</span> = C(<span>'URL_CASE_INSENSITIVE'</span>); <span>//// Default false means URL is case-sensitive, true means insensitive Case </span><span>// If there is a url address </span><span>if</span>(<span>$url</span>) {
<span>if</span>(<span>0</span>=== strpos(<span>$url</span>,<span>'/'</span>)) {<span>// Define route If it is with the directory </span><span>$route</span> = <span>true</span>;
<span>$url</span> = substr(<span>$url</span>,<span>1</span>); <span>// Remove the first slash </span><span>if</span>(<span>'/'</span> != <span>$depr</span>) { <span>// Change to system The specified interval symbol </span><span>$url</span> = str_replace(<span>'/'</span>,<span>$depr</span>,<span>$url</span>);
}
}<span>else</span>{ <span>// That is, when it is not the root directory </span><span>if</span>(<span>'/'</span> != <span>$depr</span>) { <span>// Safe replacement</span><span>$url</span> = str_replace(<span>' /'</span>,<span>$depr</span>,<span>$url</span>);
}<span>// Parse modules, controllers and operations </span><span>$url</span> = trim(<span>$url</span>,<span>$depr</span>); <span>// Remove the spacer symbols at both ends </span><span>$path</span> = explode(<span>$depr</span>, <span>$url</span>); <span>// Parse path </span><span>$var</span> = <span>array</span>();
<span>$varModule</span> = C(<span>'VAR_MODULE'</span>); <span>// 'VAR_MODULE' => 'm', // Default module gets variables </span><span>$varController</span> = C(<span>'VAR_CONTROLLER'</span>); <span>/ /'VAR_CONTROLLER' => 'c', // The default controller gets the variable </span><span>$varAction</span> = C(<span>'VAR_ACTION'</span>); <span>// 'VAR_ACTION' => 'a', // The default action Get the variable </span><span>$var</span>[<span>$varAction</span>] = !<span>empty</span>(<span>$path</span>)?array_pop(<span>$path</span>):ACTION_NAME; <span>// Parse out the action</span><span>$var</span> in this way [<span>$varController</span>] = !<span>empty</span>(<span>$path</span>)?array_pop(<span>$path</span>):CONTROLLER_NAME;<span>// Same as above to parse </span><span>if</span>(<span>$maps</span> = C(<span>'URL_ACTION_MAP' </span>)) { <span>// There is no defined routing rule by default, so it is not executed here</span><span>if</span>(<span>isset</span>(<span>$maps</span>[strtolower(<span>$var</span>[<span>$varController</span>]) ])) {
<span>$maps</span> = <span>$maps</span>[strtolower(<span>$var</span>[<span>$varController</span>])];
<span>if</span>(<span>$action</span> = array_search(strtolower(<span>$var</span>[<span>$varAction</span>]),<span>$maps</span>)){
<span>$var</span>[<span>$varAction</span>] = <span>$action</span>;
}
}
}
<span>if</span>(<span>$maps</span> = C(<span>'URL_CONTROLLER_MAP'</span>)) { <span>// Same as above</span><span>// $a=array("a"=>"red","b"=>"green ","c"=>"blue");</span><span>// echo array_search("red",$a);</span><span>if</span>(<span>$controller</span> = array_search(strtolower(<span>$var</span>[<span>$varController </span>]),<span>$maps</span>)){
<span>$var</span>[<span>$varController</span>] = <span>$controller</span>;
}
}
<span>if</span>(<span>$urlCase</span>) { <span>//Whether it is case sensitive or not, the default is true, which means no distinction is made </span><span>$var</span>[<span>$varController</span>] = parse_name(<span>$var</span>[<span>$varController</span>]); <span> //Convert them all into a unified format</span>
}
<span>$module</span> = <span>''</span>; <span>//Initialized to empty</span><span>if</span>(!<span>empty</span>(<span>$path</span>)) { <span>//If the path is not empty</span><span>$var</span>[<span>$varModule </span>] = implode(<span>$depr</span>,<span>$path</span>);
}<span>else</span>{
<span>if</span>(C(<span>'MULTI_MODULE'</span>)) { <span>// If multiple modules are enabled // Whether to allow multiple modules. If it is false, you must set DEFAULT_MODULE</span><span>if</span>(MODULE_NAME != C(<span>'DEFAULT_MODULE'</span>) || !C(<span>'MODULE_ALLOW_LIST'</span>)){
<span>$var</span>[<span>$varModule</span>]= MODULE_NAME;
}
}
}
<span>it </span>]),<span>$maps</span>)){
<span>$var</span>[<span>$varModule</span>] = <span>$_module</span>;
}
}
<span>if</span>(<span>isset</span>(<span>$var</span>[<span>$varModule</span>])){ <span>// Same as above</span><span>$module</span> = <span>$var</span>[<span>$varModule</span>];
<span>unset</span>(<span>$var</span>[<span>$varModule</span>]);
}
}
}
<span>// In fact, the real combination begins here. There are two ways </span><span>// Domain name </span><span>//</span><span>if</span>(C(<span>'URL_MODEL'</span>) == <span>0</span>) { <span>// Normal mode URL conversion </span><span>$url</span> = __APP__.<span>'?'</span>.C(<span>'VAR_MODULE'</span>).<span>"={$module}&"</span>.http_build_query(array_reverse(<span>$var</span>));
<span>if</span>(<span>$urlCase</span>){ <span>//Convert all to lowercase</span><span>$url</span> = strtolower(<span>$url</span>);
}
<span>if</span>(!<span>empty</span>(<span>$vars</span>)) { <span>// If the parameter is not empty, add the parameter </span><span>$vars</span> = http_build_query(<span>$vars</span>);
<span>$url</span> .= <span>'&'</span>.<span>$vars</span>;
}
}<span>else</span>{ <span>// PATHINFO mode or compatible URL mode </span><span>if</span>(<span>isset</span>(<span>$route</span>)) {<span>// If routing is enabled </span><span>$url</span> = __APP__.<span>'/'</span>. rtrim(<span>$url</span>,<span>$depr</span>);
}<span>else</span>{
<span>$module</span> = (defined(<span>'BIND_MODULE'</span>) && BIND_MODULE==<span>$module</span> )? <span>''</span> : <span>$module</span>;
<span>$url</span> = __APP__.<span>'/'</span>.(<span>$module</span>?<span>$module</span>.MODULE_PATHINFO_DEPR:<span>''</span>).implode(<span>$depr</span>,array_reverse(<span>$var</span>));
}
<span>if</span>(<span>$urlCase</span>){ <span>// Convert </span><span>$url</span> = strtolower(<span>$url</span>);
}
<span>if</span>(!<span>empty</span>(<span>$vars</span>)) { <span>// Just another way to parse parameters</span><span>foreach</span> (<span>$vars</span><span>as</span><span>$var</span> => <span>$val </span>){
<span>if</span>(<span>''</span> !== trim(<span>$val</span>)) <span>$url</span> .= <span>$depr</span> . <span>$var</span> . <span>$depr</span> . urlencode(<span>$val</span>);
}
}
<span>if</span>(<span>$suffix</span>) {<span>// If the file suffix is defined </span><span>$suffix</span> = <span>$suffix</span>===<span>true</span>?C(<span>'URL_HTML_SUFFIX'</span>):<span>$suffix</span>;
<span>if</span>(<span>$pos</span> = strpos(<span>$suffix</span>, <span>'|'</span>)){
<span>$suffix</span> = substr(<span>$suffix</span>, <span>0</span>, <span>$pos</span>);
}
<span>if</span>(<span>$suffix</span> && <span>'/'</span> != substr(<span>$url</span>,-<span>1</span>)){
<span>$url</span> .= <span>'.'</span>.ltrim(<span>$suffix</span>,<span>'.'</span>);
}
}
}
<span>if</span>(<span>isset</span>(<span>$anchor</span>)){ <span>// If there is an anchor point, combine </span><span>$url</span> .= <span>'#'</span>.<span>$anchor</span>;
}
<span>if</span>(<span>$domain</span>) { <span>// Combine the domain name </span><span>$url</span> = (is_ssl()?<span>'https://'</span>:<span>'http://'</span>).<span>$domain</span> .<span>$url</span>;
}
<span>return</span><span>$url</span>;
}
... Method<span></span>// The commonly used method is U('Home/Index/index',array('name'=>'lijingshan','age'=>'12'));<span></span>// By default I don’t know how to use domain names and anchors, but these two are still good, haha, it’s not complicated to parse, but when combined, the routing rules are a bit laborious. <span></span>// Okay, today we continue <span></span>// This function is actually a package <span></span>/*** Render output Widget
*<span> @param</span> string $name Widget name
*<span> @param</span> array $data passed in parameters
*<span> @return</span> void*/<span><span></span>function<span></span>W<span></span>(</span>$name<span>, <span>$data</span> =array<span>()</span>)<span> {<span></span>return<span> R(</span>$name<span>,</span>$data</span>,</span>'Widget'<span>);
}
</span>// No
* Parse resource addresses and import class library files
* For example module/controller addon://module/behavior
*<span></span></code>
Copy after login