Home  >  Article  >  Backend Development  >  TP: When APP_DEBUG=false, CLI and WEBSITE will share the same ~runtimephp, causing an error.

TP: When APP_DEBUG=false, CLI and WEBSITE will share the same ~runtimephp, causing an error.

WBOY
WBOYOriginal
2016-07-29 09:10:261321browse

TP若使用了

define('APP_DEBUG',false);

會生成./Runtime/~runtime.php 緩存文件,以後每次調用都會用這個文件來處理
但是若一個項目又使用了CLI,CLI生成的./Runtime/~runtime.php 文件跟website是同一個文件,而且cli生成的./Runtime/~runtime.php文件少了一些定義,例如__ROOT__
if(!IS_CLI) {
    // 当前文件名
    if(!defined('_PHP_FILE_')) {
        if(IS_CGI) {
            //CGI/FASTCGI模式下
            $_temp  = explode('.php',$_SERVER['PHP_SELF']);
            define('_PHP_FILE_',    rtrim(str_replace($_SERVER['HTTP_HOST'],'',$_temp[0].'.php'),'/'));
        }else {
            define('_PHP_FILE_',    rtrim($_SERVER['SCRIPT_NAME'],'/'));
        }
    }
    if(!defined('__ROOT__')) {
        // 网站URL根目录
        if( strtoupper(APP_NAME) == strtoupper(basename(dirname(_PHP_FILE_))) ) {
            $_root = dirname(dirname(_PHP_FILE_));
        }else {
            $_root = dirname(_PHP_FILE_);
        }
        define('__ROOT__',   (($_root=='/' || $_root=='\\')?'':$_root));
    }

    //支持的URL模式
    define('URL_COMMON',      0);   //普通模式
    define('URL_PATHINFO',    1);   //PATHINFO模式
    define('URL_REWRITE',     2);   //REWRITE模式
    define('URL_COMPAT',      3);   // 兼容模式
}

第一個解決辦法,將IS_CLI改成0
define('IS_CLI',PHP_SAPI=='cli'? 1   :   0);

改成
define('IS_CLI',0);

但是問題並沒有解決,雖然生成了defined('__ROOT__'),但是在CLI下生成的
__ROOT__ = .;
而website模式下生成的__ROOT__ = "";
兩者的_PHP_FILE_也不同。雖然code相同。
因此這個方案是不行的。
第二個解決辦法,cli和website使用2個不同的runtime cache文件
在入口文件index.php 添加判斷
$is_cli = PHP_SAPI=='cli' ? 1 : 0;
if (!APP_DEBUG && $is_cli) {
	define('RUNTIME_FILE','./Runtime/~runtime_cli.php');
}

這樣在不同模式下使用各自的runtime文件,不會導致衝突。完美解決!

以上就介绍了TP在APP_DEBUG=false的情況下,CLI和WEBSITE會共用同一個~runtimephp導致出錯的問題解決辦法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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