How to disable dump symfony function on production

后端 未结 5 954
青春惊慌失措
青春惊慌失措 2021-01-02 20:03

How to disable dump() function, when its in production environment? If i forget the dump function anywhere It crashes with an 500 error

相关标签:
5条回答
  • 2021-01-02 20:40

    For Symfony 4.1+, you can modify index.php this way:

      if ($debug) {
         umask(0000);
    
         Debug::enable();
    + } else {
    +     \Symfony\Component\VarDumper\VarDumper::setHandler(function($var) {});
    + }
    

    Then dump() will do nothing when called. If you want you can throw an Exception or write some log.

    The problem is that the content of dump() will go to the web debug toolbar in dev mode, which can be easily overlooked. When deploying such code to production, user will then see an inline dump()!

    0 讨论(0)
  • 2021-01-02 20:42

    It would be nice if the dump showed up in a log or as HTML comments so that it would be useful for those situations, where something in your production environment is not working quite right but you need to keep the production environment working as normal. Granted you can't output the pretty HTML you normally see in dump. I have used this technique before and it can be very useful for debugging production issues or some basic performance checks.

    0 讨论(0)
  • 2021-01-02 20:54

    NEW ANSWER AS OF 3.3:

    Now in Symfony 3.3+, you can dump to prod environment. Rather than turning up in the debug toolbar, it shows up in the content area of your page (so if you do this in the controller, it will be at the top).

    This can be handy to debug things that don't do the same between dev and prod, and then you can leave your debug code in as you develop, but just make sure before you deploy to a production environment that you comment out any dumps.

    0 讨论(0)
  • 2021-01-02 21:03

    You should remove the dump()s from your production code, it doesn't have to be there.

    But, as noted by Cerad, since it can be annoying when you forget to remove them before checking in, you can define an empty dump() function at the begining of web/app.php:

    src\web\app.php

    <?php
    
    use Symfony\Component\ClassLoader\ApcClassLoader;
    use Symfony\Component\HttpFoundation\Request;
    
    function dump($object){
        return;
    }
    
    
    $loader = require_once __DIR__.'/../app/bootstrap.php.cache';
    //rest of app.php
    
    0 讨论(0)
  • 2021-01-02 21:03

    Veve's answer was good but I suggest to you to try the fllowing solution.

    NB: this is just to unlock you. You should remove the dump() from your prod code!

    In app/AppKernel.php add this:

    class AppKernel extends Kernel
    {
    
    public function registerBundles()
    {
        $bundles = [
             new Symfony\Bundle\DebugBundle\DebugBundle(),
             new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(),
             # code ...
        ];
    
        # ...
    }
    
    0 讨论(0)
提交回复
热议问题