|
| 1 | +How to optimize your development Environment for debugging |
| 2 | +========================================================== |
| 3 | + |
| 4 | +When you work on a Symfony project on your local machine, you should use the |
| 5 | +``dev`` environment (``app_dev.php`` front controller). This environment |
| 6 | +configuration is optimized for two main purposes: |
| 7 | + |
| 8 | + * Give the developer accurate feedback whenever something goes wrong (web |
| 9 | + debug toolbar, nice exception pages, profiler, ...); |
| 10 | + |
| 11 | + * Be as similar as possible as the production environment to avoid problems |
| 12 | + when deploying the project. |
| 13 | + |
| 14 | +And to make the production environment as fast as possible, Symfony creates |
| 15 | +big PHP files in your cache containing the aggregation of PHP classes your |
| 16 | +project needs for every request. However, this behavior can confuse your IDE |
| 17 | +or your debugger. This recipe shows you how you can tweak this caching |
| 18 | +mechanism to make it friendlier when you need to debug code that involves |
| 19 | +Symfony classes. |
| 20 | + |
| 21 | +The ``app_dev.php`` front controller reads as follows by default:: |
| 22 | + |
| 23 | + // ... |
| 24 | + |
| 25 | + require_once __DIR__.'/../app/bootstrap.php.cache'; |
| 26 | + require_once __DIR__.'/../app/AppKernel.php'; |
| 27 | + |
| 28 | + use Symfony\Component\HttpFoundation\Request; |
| 29 | + |
| 30 | + $kernel = new AppKernel('dev', true); |
| 31 | + $kernel->loadClassCache(); |
| 32 | + $kernel->handle(Request::createFromGlobals())->send(); |
| 33 | + |
| 34 | +To make you debugger happier, disable all PHP class caches by removing the |
| 35 | +call to ``loadClassCache()`` and by replacing the require statements like |
| 36 | +below:: |
| 37 | + |
| 38 | + // ... |
| 39 | + |
| 40 | + // require_once __DIR__.'/../app/bootstrap.php.cache'; |
| 41 | + require_once __DIR__.'/../vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php'; |
| 42 | + require_once __DIR__.'/../app/autoload.php'; |
| 43 | + require_once __DIR__.'/../app/AppKernel.php'; |
| 44 | + |
| 45 | + use Symfony\Component\HttpFoundation\Request; |
| 46 | + |
| 47 | + $kernel = new AppKernel('dev', true); |
| 48 | + // $kernel->loadClassCache(); |
| 49 | + $kernel->handle(Request::createFromGlobals())->send(); |
| 50 | + |
| 51 | +.. tip:: |
| 52 | + |
| 53 | + If you disable the PHP caches, don't forget to revert after your debugging |
| 54 | + session. |
| 55 | + |
| 56 | +Some IDEs do not like the fact that some classes are stored in different |
| 57 | +locations. To avoid problems, you can either tell your IDE to ignore the PHP |
| 58 | +cache files, or you can change the extension used by Symfony for these files:: |
| 59 | + |
| 60 | + $kernel->loadClassCache('classes', '.php.cache'); |
0 commit comments