The problem
The documentation assumes that you’re using Apache:
Optionally, when using Apache, you can use the APPLICATION_ENV
setting in your VirtualHost
to let PHP output all its errors to the browser. This can be useful during the development of your application.
Source: the manual.
First solution: define APPLICATION_ENV
As discussed in another question, you should be able to run your development server with defined APPLICATION_ENV
using the following command:
APPLICATION_ENV=development php -S 0.0.0.0:8080 public public/index
Second solution: use Apache
You may simply setup an Apache VirtualHost
as said in the manual. Put the following in your VirtualHost
config:
<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>
And update your hosts
:
127.0.0.1 zf2-tutorial.localhost localhost
Refer to the manual in case of problems.
I won’t guide you through installation of this package (here are some instructions), but I’ll present two solutions for checking (in your code) whether development mode is enabled.
First method: check if config/development.config.php
exists
After enabling development mode, contents of config/development.config.php.dist
should be copied to config/development.config.php
. In your code, use the following to check if you’re in development mode:
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
}
Second method: inject application config
Let’s assume that you want to check if development mode is enabled in a controller/service or other thing that can be created by a factory implementing zend-servicemanager
’s FactoryInterface
. Below I present example application config, factory and service:
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) {
// …
}
}
}