本文实例讲述了ThinkPHP中url隐藏入口文件后接收alipay传值的方法。分享给大家供大家参考。具体方法如下:
现在公司项目的需求变化多端,项目使用的是Thinkphp2.0,而conf.php中设置的URL_MODEL=2,为了兼容.htaccess和隐藏index.php,但在使用一些第三方接口时(例如支付宝或MSN开放平台),他们返回的参数往往含有一个?号,而URL_MODEL=2时,TP会自动将含有?的URL进行转换.
转换前:http://www.xxx.com/index.php/alipay/callback/?is_success/T/sign_type/MD5/notify_id/RqPnCoPT3K9/vwbh3I
转换后:http://www.xxx.com/alipay/callback/is_success/T/sign_type/MD5/notify_id/RqPnCoPT3K9/vwbh3I
转换后的网址将无法访问,而转换前的网址是可以访问的,这时需要临时针对alipay这个控制器,URL_MODEL=1,但在该控制器里C('URL_MODEL',1)是没有用的,因为TP会在入口文件App::run()时直接404了.
想了很多方法,先是修改它的配置文件Conf.php,结果发现会生成缓存文件~app.php,所以必须根据情况删除这个缓存文件,于是我对index.php入口文件进行了修改(事实上,放在任何App::run()之前都可以,你可以自行封装).
PHP实例代码:
代码如下:
// 定义ThinkPHP框架路径
define('THINK_PATH', '不告诉你我的TP文件夹名/');
//如果网址中存在这样的形式就删除缓存配置文件,并创建一个标识
if(strstr($_SERVER['REQUEST_URI'],'alipay/callback/')||strstr($_SERVER['REQUEST_URI'],'order.return_pay')){
unlink('./home/Runtime/~app.php');
//创建一个缓存文件做为标识
$fn= fopen('./home/Runtime/re_url_model.php','wb');
fclose($fn);
}elseif(file_exists('./home/Runtime/re_url_model.php')){
unlink('./home/Runtime/re_url_model.php');
unlink('./home/Runtime/~app.php');
}
//定义项目名称和路径
define('APP_NAME', 'home');
define('APP_PATH', 'home');
// 加载框架入口文件
require(THINK_PATH."ThinkPHP.php");
//实例化一个网站应用实例
App::run();
?>
项目的配置文件Conf.php根据你自己的情况做一个判断即可.
PHP实例源码:
代码如下:
//兼容支付宝快捷登录的?请求
if(strstr($_SERVER['REQUEST_URI'],'alipay/callback/')||strstr($_SERVER['REQUEST_URI'],'order.return_pay')){
$my_array['URL_MODEL'] = 1;
}
return $my_array;
这样,访问www.xxx.com或www.xxx.com/shipin/等任何非alipay控制器的页面时,网站依然是使用url_model = 2来隐藏index.php,使得URL友好.
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
