Symfony 4 在控制器操作中禁用探查器

Symfony 4 disable profiler in controller action

我在控制器中有一个动作,用于在数据库中批量插入... 所以这会使用大量资源,分析器正在缓存所有内容,服务器出现故障。

如何在控制器上通过一次操作禁用分析器(和所有调试服务)?

控制器看起来像:

<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Sync\Incomming\Syncronize;

/**
* @Route("/sync")
*/
class SyncController extends AbstractController
{
    private $syncronize;
    public function __construct(Syncronize $syncronize)
    {
        $this->syncronize = $syncronize;
    }
    /**
     * @Route("/",name="sync_index")
     */
    public function index(Request $request, Profiler $profiler)
    {  
        $message = "Hello";

        return $this->render( 'sync/output.html.twig', ['message' => $message ]); 

    }   
}

如果我尝试在构造函数方法中自动装配探查器,则会收到错误 public function __construct(Syncronize $syncronize, Profiler $profiler):

Cannot autowire service "App\Controller\SyncController": argument "$profiler" of method "__construct()" references class "Symfony\Component\HttpKernel\Profiler\Profiler" but no such service exists. You should maybe alias this class to the existing "profiler" service.

如果我尝试在索引方法中自动装配探查器,则会收到错误 public function index(Request $request, Profiler $profiler):

Controller "App\Controller\SyncController::index()" requires that you provide a value for the "$profiler" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

编辑 对于大查询禁用探查器不是解决方案......实际上你需要禁用 setSQLLogger:

$em->getConnection()->getConfiguration()->setSQLLogger(null);

Symfony 3.4 / 4

https://symfony.com/doc/4.0/profiler/matchers.html

use Symfony\Component\HttpKernel\Profiler\Profiler;

class DefaultController
{
    // ...

    public function someMethod(Profiler $profiler)
    {
        // for this particular controller action, the profiler is disabled
        $profiler->disable();

        // ...
    }
}

如果自动装配有错误

# config/services.yaml
services:
    Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'

旧:

如果你想从控制器禁用分析器,你可以这样:

use Symfony\Component\HttpKernel\Profiler\Profiler;
// ...

class DefaultController
{
    // ...

    public function someMethod(Profiler $profiler)
    {
        // for this particular controller action, the profiler is disabled
        $profiler->disable();

        // ...
    }
}

来源:https://symfony.com/doc/current/profiler/matchers.html

另一种方法是使用:$this->get('profiler')->disable();


年长者:
只需将应用程序切换到 prod 模式并禁用调试模式。

为此,请在您喜欢的编辑器中打开服务器上的 .env 文件(注意:您永远不应将此文件提交给 Git,因为您在其中存储机密!)。

在该文件中,您应该看到以以下内容开头的部分:###> symfony/framework-bundle ### 在其正下方有一个 APP_ENV=devAPP_DEBUG=1,将这两行更改为 APP_ENV=prodAPP_DEBUG=0。然后保存文件。

接下来您应该清除生产模式的缓存并安装资产。为此,运行 以下命令:

php bin/console cache:clear --env=prod --no-debug
php bin/console cache:warmup --env=prod --no-debug
php bin/console assets:install --env=prod --no-debug --symlink

如果您现在加载应用程序,它将处于生产模式,其中包含更多缓存并且在禁用调试时速度更快。


注:
PHP 仍有时间限制。如果您仍然达到该限制,您可以更改 PHP 设置,或者您可以 运行 从 CLI 导入,因为 CLI 通常没有时间限制。如果用户需要能够自己上传,我建议让他们上传文件,输入关于文件的 "note" 到数据库,并让 cronjob 读取该数据库以获取未导入的文件并导入它们。