search

关于U 方法

本帖最后由 lmq_2016 于 2015-05-04 22:57:34 编辑 先说说提这个问题的由头:看到别人在thinkphp中用U方法时,这样用 U('verify')
其中参数verify是自定义的一个控制器里的方法
那么问题来了这种形式的调用为啥能有效呢,下面是U方法的写法,从中没有看出哪里可以这样使用呢?

function U($url,$vars='',$suffix=true,$redirect=false,$domain=false) {<br />    // 解析URL<br />    $info =  parse_url($url);<br />    $url   =  !empty($info['path'])?$info['path']:ACTION_NAME;<br />    // 解析子域名<br />    if($domain===true){<br />        $domain = $_SERVER['HTTP_HOST'];<br />        if(C('APP_SUB_DOMAIN_DEPLOY') ) { // 开启子域名部署<br />            $domain = $domain=='localhost'?'localhost':'www'.strstr($_SERVER['HTTP_HOST'],'.');<br />            // '子域名'=>array('项目[/分组]');<br />            foreach (C('APP_SUB_DOMAIN_RULES') as $key => $rule) {<br />                if(false === strpos($key,'*') && 0=== strpos($url,$rule[0])) {<br />                    $domain = $key.strstr($domain,'.'); // 生成对应子域名<br />                    $url   =  substr_replace($url,'',0,strlen($rule[0]));<br />                    break;<br />                }<br />            }<br />        }<br />    }<br /><br />    // 解析参数<br />    if(is_string($vars)) { // aaa=1&bbb=2 转换成数组<br />        parse_str($vars,$vars);<br />    }elseif(!is_array($vars)){<br />        $vars = array();<br />    }<br />    if(isset($info['query'])) { // 解析地址里面参数 合并到vars<br />        parse_str($info['query'],$params);<br />        $vars = array_merge($params,$vars);<br />    }<br /><br />    // URL组装<br />    $depr = C('URL_PATHINFO_DEPR');<br />    if($url) {<br />        if(0=== strpos($url,'/')) {// 定义路由<br />            $route   =  true;<br />            $url   =  substr($url,1);<br />            if('/' != $depr) {<br />                $url   =  str_replace('/',$depr,$url);<br />            }<br />        }else{<br />            if('/' != $depr) { // 安全替换<br />                $url   =  str_replace('/',$depr,$url);<br />            }<br />            // 解析分组、模块和操作<br />            $url   =  trim($url,$depr);<br />            $path = explode($depr,$url);<br />            $var  =  array();<br />            $var[C('VAR_ACTION')] = !empty($path)?array_pop($path):ACTION_NAME;<br />            $var[C('VAR_MODULE')] = !empty($path)?array_pop($path):MODULE_NAME;<br />            if(C('URL_CASE_INSENSITIVE')) {<br />                $var[C('VAR_MODULE')] =  parse_name($var[C('VAR_MODULE')]);<br />            }<br />            if(C('APP_GROUP_LIST')) {<br />                if(!empty($path)) {<br />                    $group   =  array_pop($path);<br />                    $var[C('VAR_GROUP')]  =   $group;<br />                }else{<br />                    if(GROUP_NAME != C('DEFAULT_GROUP')) {<br />                        $var[C('VAR_GROUP')]  =   GROUP_NAME;<br />                    }<br />                }<br />            }<br />        }<br />    }<br /><br />    if(C('URL_MODEL') == 0) { // 普通模式URL转换<br />        $url   =  __APP__.'?'.http_build_query($var);<br />        if(!empty($vars)) {<br />            $vars = http_build_query($vars);<br />            $url   .= '&'.$vars;<br />        }<br />    }else{ // PATHINFO模式或者兼容URL模式<br />        if(isset($route)) {<br />            $url   =  __APP__.'/'.$url;<br />        }else{<br />            $url   =  __APP__.'/'.implode($depr,array_reverse($var));<br />        }<br />        if(!empty($vars)) { // 添加参数<br />            $vars = http_build_query($vars);<br />            $url .= $depr.str_replace(array('=','&'),$depr,$vars);<br />        }<br />        if($suffix) {<br />            $suffix   =  $suffix===true?C('URL_HTML_SUFFIX'):$suffix;<br />            if($suffix) {<br />                $url  .=  '.'.ltrim($suffix,'.');<br />            }<br />        }<br />    }<br />    if($domain) {<br />        $url   =  'http://'.$domain.$url;<br />    }<br />    if($redirect) // 直接跳转URL<br />        redirect($url);<br />    else<br />        return $url;<br />}<br /><br />// URL重定向

------解决思路----------------------
$url = 'verify';<br />$info = parse_url($url);
Array
(
    [path] => verify
)
你跟踪一下就知道了
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)