问题
文档假设您使用的是 Apache:
可选地,使用 Apache 时,您可以使用 VirtualHost 中的 APPLICATION_ENV 设置让 PHP 将其所有错误输出到浏览器。这在您的应用程序开发过程中很有用。
来源:the manual。
第一个解决方案:定义APPLICATION_ENV
正如another question 中所讨论的,您应该能够使用以下命令运行定义了APPLICATION_ENV 的开发服务器:
APPLICATION_ENV=development php -S 0.0.0.0:8080 public public/index
第二种解决方案:使用 Apache
您可以简单地设置一个 Apache VirtualHost,如 the manual 中所述。将以下内容放入您的 VirtualHost 配置中:
<VirtualHost *:80>
ServerName zf2-tutorial.localhost
DocumentRoot /path/to/zf2-tutorial/public
SetEnv APPLICATION_ENV "development"
<Directory /path/to/zf2-tutorial/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
并更新您的hosts:
127.0.0.1 zf2-tutorial.localhost localhost
如有问题请联系the manual。
我不会指导您完成 this package (here are some instructions) 的安装,但我将提供两种解决方案来检查(在您的代码中)是否启用了开发模式。
第一种方法:检查config/development.config.php是否存在
开启开发模式后,config/development.config.php.dist的内容应该复制到config/development.config.php。在您的代码中,使用以下内容检查您是否处于开发模式:
if (file_exists('config/development.config.php')) {
// in default skeleton application. You may have to play with
// __DIR__ if you’ve modified your public/index.php
}
第二种方法:注入应用配置
假设您要检查是否在控制器/服务或其他可以由实现zend-servicemanager 的FactoryInterface 的工厂创建的东西中启用开发模式。下面我介绍示例应用程序配置、工厂和服务:
config/development.config.php.dist
<?php
return [
'development' => true
];
Application\Service\Factory\ExampleServiceFactory
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new ExampleService($container->get('Config'));
}
Application\Service\ExampleService
<?php
// imports, namespace…
class ExampleService
{
protected $dev;
public function __construct(array $config)
{
$this->dev = isset($config['development']) && $config['development'] === true;
}
public function doSomething()
{
if ($this->dev) {
// …
}
}
}