From app_dev.php to Production: The Symfony 2 Environment Switch
Moving a Symfony 2 application from development to production often feels like flipping a switch, but the reality is slightly more nuanced. Developers frequently encounter a scenario where the application runs perfectly under app_dev.php, yet switching to app.php results in a stubborn 404 error. The confusion usually stems from the belief that simply renaming the entry point or changing an environment variable is sufficient without understanding how Symfony manages its routing cache and kernel instances.
When you run your app via app_dev.php, Symfony loads the development environment configuration, which includes specific debug routes and a different kernel instance. However, switching to app.php implies moving to the production environment. If this switch fails with a 404 error immediately after the $kernel->handle()->send() line, it typically means the router is unable to find the requested path because the routing definitions for that specific environment haven't been loaded or cached correctly for the current kernel instance.
In many cases, developers assume that adding a route definition in app/config/routing.yml is enough. While you must indeed define your main routes thereâsuch as mapping _welcome to your bundle's resource like so:
_welcome:
resource: "@AcmeDemoBundle/Resources/config/routing.yml"
prefix: /
and ensuring your src/Acme/DemoBundle/Resources/config/routing.yml contains the actual controller patterns, simply editing these files does not immediately update the router's internal state if the application has already been booted with a different configuration. Symfony relies heavily on caching to improve performance in production. The routing information is compiled into a cache file upon the first request. If you are running on app.php (production), but the cache was generated while running on app_dev.php, or if the cache directory is stale, the router will fail to resolve your routes correctly, leading to the observed 404.
The definitive fix, as verified by community experience with this specific version of Symfony, involves clearing the cache explicitly for the target environment. You cannot rely on the default cache:clear command if it defaults to the current environment variable or a generic setting that doesn't match your production setup. Instead, you must invoke the console command with the environment parameter set to prod:
./app/console --env=prod cache:clear
This command ensures that Symfony rebuilds the routing table, compiled classes, and other assets specifically for the production environment defined in app/config/parameters.yml or .dist. The --env=prod flag is critical here because it forces the kernel to operate under the production bootstrap logic, ensuring that the correct entry point configuration is active.
Without this specific command, you might be running a kernel instance that still holds references to development routes or missing cache entries, effectively rendering your production routing definitions invisible to the web server. Once the cache is cleared with the correct environment flag, the $kernel->handle() call in app.php will successfully resolve the request to your controller action, such as AcmeDemoBundle:Demo:index.
It is worth noting that this behavior highlights a common pitfall when transitioning from the development bootstrap (app_dev.php) to the production bootstrap (app.php). In development, Symfony often provides a fallback mechanism or loads all routes regardless of the environment, but in production, strict adherence to the configured environment and cache state is required. If you encounter similar issues with other Symfony versions or frameworks, always verify that your cache strategy aligns with your deployment workflow.