Where's my app/console

We have environments and everything is going great, but we don't have an app/console script. So we can't use any of the handy commands like debug:router or debug:container. And, if you surf to a 404 page, it's still missing its CSS. That's because we need to run app/console assets:install so that it can symlink or copy a few core CSS files that the exception page uses. That's a bit difficult right now, since there is no console script in our project.

Let's go steal it from the standard edition. On GitHub, head into the app directory, open console and copy its contents. Now, put our new console file right at the root of the project and make sure PhpStorm formats it as a PHP file. Paste the contents:

28 lines console
#!/usr/bin/env php
// if you don't want to setup permissions the proper way, just uncomment the following PHP line
// read for more information
require_once __DIR__.'/bootstrap.php.cache';
require_once __DIR__.'/AppKernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
if ($debug) {
$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);

Let's go to work: uncomment the umask. For the first require_once, we know that this should be config/autoload.php. And AppKernel lives right in this directory, so that's fine:

30 lines console
... lines 1 - 3
... lines 5 - 7
require_once __DIR__.'/config/autoload.php';
require_once __DIR__.'/AppKernel.php';
... lines 10 - 30

The biggest thing we need to change is the $env and $debug variables. Let's grab that code from index.php. PhpStorm is being weird and hiding my web/ directory... now it's back. Copy the 4 dotenv lines from index.php. Now in console, delete the $env and $debug lines and paste our stuff:

30 lines console
... lines 1 - 14
// load the environmental variables
$dotenv = new Dotenv\Dotenv(__DIR__);
... lines 20 - 30

Instead of passing a --env=prod flag to change the environment, this reads the .env file like normal. Let's try it!

If you're on UNIX, make this executable with a chmod +x. Then run ./console or in Windows php console:

chmod +x console

That blows up with "Environment file .env not found". I forgot to update my DotEnv path since this file lives in the root. Make sure you pass it just __DIR__:

30 lines console
... lines 1 - 15
$dotenv = new Dotenv\Dotenv(__DIR__);
... lines 17 - 30

Try it now:


Our mini-app has a fully-fledged console. Instead of ./app/console, the command is just ./console, but everything else is the same. Use debug:router to get all the routes:

./console debug:router

And debug:container to see the services:

./console debug:container

And the one we want is ./console assets:install with a --symlink if your system supports that.

./console assets:install --symlink

Refresh the 404 page: there's the beautiful exception page we expect.

Leave a comment!

  • 2017-03-13 Victor Bocharsky

    Hey Slawek,

    You're right, it's due to a bit different location of ./console file, if you put it inside the bin directory, tweak paths and run with ./bin/console it will work. But the easiest way is to set the right document root for this command: "./console s:r --docroot=./web". The "./console s:r -h" command will show you available options for it.


  • 2017-03-12 SÅ‚awek Grochowski

    Hey thx for this tutorial. I was wondering why you don't use use bin/console server:run after installing console component? I tried and I got this: "[ERROR] The given document root directory "/home/slk500/Workspace/standard-api/../web" does not exist" maybe it due some differences in web/index.php & console file?