我在 Controller 中有一个用于在数据库中批量插入的操作... 因此,这会使用大量资源,并且探查器会缓存所有内容,然后服务器就会宕机。
如何在 Controller 上通过一个操作禁用探查器(以及所有调试服务)?
Controller 看起来像:
<?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 ]);
}
}
如果我尝试在构造函数方法中 Autowiring 探查器,则会收到错误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.
如果我尝试在索引方法中 Autowiring 探查器,则会收到错误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();
// ...
}
}
如果 Autowiring 出现错误
# config/services.yaml
services:
Symfony\Component\HttpKernel\Profiler\Profiler: '@profiler'
旧:
如果您想从 Controller 禁用分析器,您可以这样:
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
模式并禁用 Debug模式即可。
为此,请在您喜欢的编辑器中打开服务器上的 .env
文件(注意:您永远不应该将此文件提交到 Git,因为您在其中存储 secret !)。
在该文件中,您应该看到以以下内容开头的部分:###> symfony/framework-bundle ###
正下方有一个 APP_ENV=dev
和 APP_DEBUG=1
,将这两行更改为 APP_ENV=prod
和 APP_DEBUG=0
。然后保存文件。
接下来,您应该清除生产模式的缓存并安装 Assets 。为此,请运行以下命令:
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 通常没有时间限制。如果用户需要能够自己上传,我建议他们上传文件,在数据库中输入有关该文件的“注释”,并让 cronjob 读取该数据库中未导入的文件并导入它们。
关于Symfony 4 在 Controller 操作中禁用分析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52020323/