2013-03-27 95 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);