Buy

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 http://symfony.com/doc/current/book/installation.html#configuration-and-setup for more information
//umask(0000);
set_time_limit(0);
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) {
Debug::enable();
}
$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->run($input);

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
umask(0000);
... 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__);
$dotenv->load();
$env = $_SERVER['SYMFONY_ENV'];
$debug = $_SERVER['SYMFONY_DEBUG'];
... 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
./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:

./console

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!