U function, multi-entry jump Method

Source: Internet
Author: User
Tags subdomain subdomain name
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. For example, index. php uses the U method to jump to user. php, which is not supported by default.
However, this method is used to completely solve this problem.
You can join QQ Group 161607701 to learn PHP Technology!


Usage
The key lies in the first parameter of the U function, which can be processed in arrays or strings.
For example
I want to go to index. php? M = Home & c = Index & a = index page
Jump to admin. php? M = Site & c = Public & a = login page

The first method string format:
U ('admin. php? Site/Public/login '); => admin. php? M = Site & c = Public & a = login;
As long as one? Is added before the first parameter? Perform the interval. But remember that this entry must end with. php? You can set it like TP later.
The second method string format:
U (array ('admin. php', 'site/Public/login'); => admin. php? M = Site & c = Public & a = login;

** URL_MODEL = 0 and 1 are valid. 2, 3 is similar to changing the PATHINFO style ***



\ ThinkPHP \ Common \ functions. php open the system function library file
Search function U (where the function is located)
Replace the U function with the function that I have edited below. Function U ($ url = '', $ vars ='', $ suffix = true, $ domain = false ){
$/Index. php = '';
If (is_array ($ url )){
$/Index. php = C ('url _ PATHINFO_DEPR '). $ URL [0];
$ Url = $ url [1];
} Elseif (strpos ($ url ,'? ') & Strpos ($ url,'. php ')){
$ Url_var = explode ('? ', $ Url, 2 );
$/Index. php = C ('url _ PATHINFO_DEPR '). $ url_var ['0'];
$ Url = $ url_var ['1'];
}
// Parse the URL
$ Info = parse_url ($ url );
$ Url =! Empty ($ info ['path'])? $ Info ['path']: ACTION_NAME;
If (isset ($ info ['fragment ']) {// parse the anchor
$ Anchor = $ info ['fragment '];
If (false! = Strpos ($ anchor ,'? ') {// Parse parameters
List ($ anchor, $ info ['query']) = explode ('? ', $ Anchor, 2 );
}
If (false! = Strpos ($ anchor, '@') {// resolve Domain Name
List ($ anchor, $ host) = explode ('@', $ anchor, 2 );
}
} Elseif (false! = Strpos ($ url, '@') {// resolve Domain Name
List ($ url, $ host) = explode ('@', $ info ['path'], 2 );
}
// Resolve the subdomain name
If (isset ($ host )){
$ Domain = $ host. (strpos ($ host ,'.')? '': Strstr ($ _ SERVER ['HTTP _ host'], '.');
} Elseif ($ domain === true ){
$ Domain = $ _ SERVER ['HTTP _ host'];
If (C ('app _ SUB_DOMAIN_DEPLOY ') {// enable sub-domain name deployment
$ Domain = 'localhost '? 'Localhost': 'www '. strstr ($ _ SERVER ['HTTP _ host'],'. ');
// 'Subdomain name' => array ('module [/controller] ');
Foreach (C ('app _ SUB_DOMAIN_RULES ') as $ key => $ rule ){
$ Rule = is_array ($ rule )? $ Rule [0]: $ rule;
If (false === strpos ($ key, '*') & 0 === strpos ($ url, $ rule )){
$ Domain = $ key. strstr ($ domain, '.'); // generate the corresponding subdomain name
$ Url = substr_replace ($ url, '', 0, strlen ($ rule ));
Break;
}
}
}
}

// Parse parameters
If (is_string ($ vars) {// aaa = 1 & bbb = 2 convert to an array
Parse_str ($ vars, $ vars );
} Elseif (! Is_array ($ vars )){
$ Vars = array ();
}
If (isset ($ info ['query']) {// merge parameters in the resolution address to vars
Parse_str ($ info ['query'], $ params );
$ Vars = array_merge ($ params, $ vars );
}

// URL assembly
$ Depr = C ('url _ PATHINFO_DEPR ');
$ UrlCase = C ('url _ CASE_INSENSITIVE ');
If ($ url ){
If (0 === strpos ($ url, '/') {// define the route
$ Route = true;
$ Url = substr ($ url, 1 );
If ('/'! = $ Depr ){
$ Url = str_replace ('/', $ depr, $ url );
}
} Else {
If ('/'! = $ Depr) {// Security replacement
$ Url = str_replace ('/', $ depr, $ url );
}
// Resolution module, Controller, and Operation
$ Url = trim ($ url, $ depr );
$ Path = explode ($ depr, $ url );
$ Var = array ();
$ VarModule = C ('var _ module ');
$ VarController = C ('var _ controller ');
$ VarAction = C ('var _ Action ');
$ Var [$ varAction] =! Empty ($ path )? Array_pop ($ path): ACTION_NAME;
$ Var [$ varController] =! Empty ($ path )? Array_pop ($ path): CONTROLLER_NAME;
If ($ maps = C ('url _ ACTION_MAP ')){
If (isset ($ maps [strtolower ($ var [$ varController]) {
$ Maps = $ maps [strtolower ($ var [$ varController])];
If ($ action = array_search (strtolower ($ var [$ varAction]), $ maps )){
$ Var [$ varAction] = $ action;
}
}
}
If ($ maps = C ('url _ CONTROLLER_MAP ')){
If ($ controller = array_search (strtolower ($ var [$ varController]), $ maps )){
$ Var [$ varController] = $ controller;
}
}
If ($ urlCase ){
$ Var [$ varController] = parse_name ($ var [$ varController]);
}
$ Module = '';
If (! Empty ($ path )){
$ Var [$ varModule] = implode ($ depr, $ path );
} Else {
If (C ('multi _ module ')){
If (MODULE_NAME! = C ('default _ module') |! C ('module _ ALLOW_LIST ')){
$ Var [$ varModule] = MODULE_NAME;
}
}
}
If ($ maps = C ('url _ MODULE_MAP ')){
If ($ _ module = array_search (strtolower ($ var [$ varModule]), $ maps )){
$ Var [$ varModule] =$ _ module;
}
}
If (isset ($ var [$ varModule]) {
$ Module = $ var [$ varModule];
Unset ($ var [$ varModule]);
}
}
}
If (C ('url _ model') = 0) {// normal mode URL Conversion
$ Url = ($/index. php? $/Index. php:/index. php ).'? '. C ('var _ module'). "= {$ MODULE} &". http_build_query (array_reverse ($ VAR ));
If ($ urlCase ){
$ Url = strtolower ($ url );
}
If (! Empty ($ vars )){
$ Vars = http_build_query ($ vars );
$ Url. = '&'. $ vars;
}
} Else {// PATHINFO mode or URL-compatible mode
If (isset ($ route )){
$ Url = ($/index. php? $/Index. php:/index. php). '/'. rtrim ($ url, $ depr );
} Else {
$ Module = (defined ('Bind _ module') & BIND_MODULE = $ MODULE )? '': $ Module;
$ Url = ($/index. php? $/Index. php:/index. php). '/'. ($ module? $ Module. MODULE_PATHINFO_DEPR: ''). implode ($ depr, array_reverse ($ var ));
}
If ($ urlCase ){
$ Url = strtolower ($ url );
}
If (! Empty ($ vars) {// Add Parameters
Foreach ($ vars as $ var => $ val ){
If (''! = Trim ($ val) $ url. = $ depr. $ var. $ depr. urlencode ($ val );
}
}
If ($ suffix ){
$ Suffix = true? C ('url _ HTML_SUFFIX '): $ suffix;
If ($ pos = strpos ($ suffix, '| ')){
$ Suffix = substr ($ suffix, 0, $ pos );
}
If ($ suffix &&'/'! = Substr ($ url,-1 )){
$ Url. = '.'. ltrim ($ suffix ,'.');
}
}
}
If (isset ($ anchor )){
$ Url. = '#'. $ anchor;
}
If ($ domain ){
$ Url = (is_ssl ()? 'Https: // ': 'http: //'). $ domain. $ url;
}
Return $ url;
}

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.