Symfony 4 在控制器操作中禁用分析器

3pe*_*pe3 2 profiler symfony

我在控制器中有一个用于在数据库中进行批量插入的操作...因此这使用了大量资源,并且探查器正在缓存所有内容,并且服务器出现故障。

如何在控制器上通过一项操作禁用探查器(以及所有调试服务)?

控制器看起来像:

<?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 ]); 

    }   
}
Run Code Online (Sandbox Code Playgroud)

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

无法自动装配服务“App\Controller\SyncController”:方法“__construct()”的参数“$profiler”引用类“Symfony\Component\HttpKernel\Profiler\Profiler”,但不存在此类服务。您也许应该将此类别名为现有的“分析器”服务。

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

控制器“App\Controller\SyncController::index()”要求您为“$profiler”参数提供一个值。参数可以为 null 并且未提供 null 值、未提供默认值或者因为此参数后面有一个非可选参数。

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

$em->getConnection()->getConfiguration()->setSQLLogger(null);
Run Code Online (Sandbox Code Playgroud)

waw*_*awa 5

交响乐 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();

        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您在自动装配时遇到错误

# config/services.yaml
services:
    Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'
Run Code Online (Sandbox Code Playgroud)

老的:

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

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

class DefaultController
{
    // ...

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

        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

来源: 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
Run Code Online (Sandbox Code Playgroud)

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


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