2013-03-27 49 views
2

我正在创建自己的框架作为学习过程。有一个配置文件,人们可以设置框架是否处于开发模式。.htaccess display_startup_errors设置取决于php脚本配置文件

<?PHP 
$project[security][dev_mode] = true; 
?> 

Display_startup_errors在.htaccess中定义,用于指示是否应显示语法错误。我宁愿如果用户不需要混淆.htaccess文件,以便'调整'到配置文件中的设置。任何人有一个想法如果如何有可能以某种方式让.htaccess检查php文件的内容并采取相应的行动?

一个解决方案,设置display_startup_errors比.htaccess其他方式也是受欢迎的;-)。

非常感谢提前!

+1

处理错误的配置'display_startup_errors'将是什么人将要为自己。我不想要一个PHP框架来尝试控制http服务器。话虽如此,我不知道这是一个简单的方法来完成你在说什么。 htaccess只是一个iirc指令文件。 – castis 2013-03-27 00:52:44

+0

你可能是对的,但这个框架不会大规模分发;-)。谢谢。 – dirk 2013-03-27 00:54:13

+0

为什么使用.htaccess来显示错误而不是php的ini_se()配置? – 2013-03-27 00:55:49

回答

1

尝试

<?php 
$iDevMode = ($project['security']['dev_mode']) ? 1 : 0; 

ini_set('display_errors', $iDevMode); 
?> 

要切换基础上的定义。这是一个丑陋的三元操作(你可以转换为如果声明练习),并且需要在程序中尽早处理。

另请注意,PHP会抛出一个通知,不会将您的关联数组引用封装在引号中,如上所述。

+0

当脚本在处理任何代码时发现语法错误时立即终止,我明白无法使用ini_set设置display_startup_errors ... – dirk 2013-03-27 01:03:12

+0

试试** display_errors **代替。 – Zmart 2013-03-27 01:12:56

1

在处理错误时使用.htaccess的替代方法是创建一个可配置的 php文件,该文件可以像其他框架一样在开发,生产和测试阶段进行设置。

* You can load different configurations depending on your 
* current environment. Setting the environment also influences 
* things like logging and error reporting. 
* 
* This can be set to anything, but default usage is: 
* 
*  development 
*  testing 
*  production 
* 
* NOTE: If you change these, also change the error_reporting() code below 
* 
*/ 
    define('ENVIRONMENT', 'development'); 
/* 
*--------------------------------------------------------------- 
* ERROR REPORTING 
*--------------------------------------------------------------- 
* 
* Different environments will require different levels of error reporting. 
* By default development will show errors but testing and live will hide them. 
*/ 

if (defined('ENVIRONMENT')) 
{ 
    switch (ENVIRONMENT) 
    { 
     case 'development': 
      error_reporting(E_ALL); 
     break; 

     case 'testing': 
     case 'production': 
      error_reporting(0); 
     break; 

     default: 
      exit('The application environment is not set correctly.'); 
    } 
} 

或手动尝试使用的ini_set()来正确设置上

// change settings for error handler to show errors 
// $this setup is used for checking errors for development to be shown.... 
ini_set('display_errors', 1); 
ini_set('display_startup_errors',1); 
error_reporting(E_ALL);