錯誤報告配置(可選)
找到在設定虛擬主機時添加的代碼:
01
<VirtualHost *:80>
02
DocumentRoot
"D:/Website/htdocs/zf2-tutorial/public"
03
ServerName zf2-tutorial.com
04
# This should be omitted in the production environment
05
SetEnv APPLICATION_ENV development
06
<Directory /
>
07
DirectoryIndex index.php
08
AllowOverride All
09
Order allow,deny
10
Allow from all
11
</Directory>
12
</VirtualHost>
在代碼中有一行
1
SetEnv APPLICATION_ENV development
這行說明目前我們將整個項目的模式設定為”開發模式(developement)“,在這個模式下 PHP 會向浏覽器輸出所有的錯誤信息,方便我們的開發和調試。
注意:項目正式上線之前要將這個模式刪除,以免造成信息洩露和安全漏洞。
除了在虛擬主機的配置文件中設定開發模式之外,還需要在項目入口文件中做一點額外的設定。
打開 D:\Website\htdocs\zf2-tutorial\public\index.php ,默認的代碼如下
01
<?php
02
/**
03
* This makes our life easier when dealing with paths. Everything is relative
04
* to the application root now.
05
*/
06
chdir
(dirname(__DIR__));
07
08
// Decline static file requests back to the PHP built-in webserver
09
if
(php_sapi_name() ===
'cli-server'
&&
is_file
(__DIR__ .
parse_url
(
$_SERVER
[
'REQUEST_URI'
], PHP_URL_PATH))) {
10
return
false;
11
}
12
13
// Setup autoloading
14
require
'init_autoloader.php'
;
15
16
// Run the application!
17
Zend\Mvc\Application::init(
require
'config/application.config.php'
)->run();
在文件頂部添加以下代碼
1
/**
2
* Display all errors when APPLICATION_ENV is development.
3
*/
4
if
(
$_SERVER
[
'APPLICATION_ENV'
] ==
'development'
) {
5
error_reporting
(E_ALL);
6
ini_set
(
"display_errors"
, 1);
7
}
最終的代碼如下:
01
<?php
02
/**
03
* Display all errors when APPLICATION_ENV is development.
04
*/
05
if
(
$_SERVER
[
'APPLICATION_ENV'
] ==
'development'
) {
06
error_reporting
(E_ALL);
07
ini_set
(
"display_errors"
, 1);
08
}
09
10
/**
11
* This makes our life easier when dealing with paths. Everything is relative
12
* to the application root now.
13
*/
14
chdir
(dirname(__DIR__));
15
16
// Decline static file requests back to the PHP built-in webserver
17
if
(php_sapi_name() ===
'cli-server'
&&
is_file
(__DIR__ .
parse_url
(
$_SERVER
[
'REQUEST_URI'
], PHP_URL_PATH))) {
18
return
false;
19
}
20
21
// Setup autoloading
22
require
'init_autoloader.php'
;
23
24
// Run the application!
25
Zend\Mvc\Application::init(
require
'config/application.config.php'
)->run();
*