交响申请中的全局功能
我认为这很容易。对于故障排除,我希望从应用程序中的任何地方调用我的_logger()
函数。
function _logger(?string $msg=null, int $offset=0, bool $time=false):void
{
$d1 = debug_backtrace()[$offset+1];
$d2 = debug_backtrace()[$offset+2];
syslog(LOG_INFO, sprintf('_logger%s: %s::%s(%s) called by %s::%s on line %s%s', $time?'('.date("Y-m-d H:i:s").')':'', $d1['class'], $d1['function'], json_encode($d1['args']), $d2['class'], $d2['function'], $d2['line'], $msg?' | '.$msg:''));
}
首先,我尝试将其添加到index.php中。直接在主体中添加时,我会遇到服务器错误,并且要么有错误报告,因此无法获得任何信息(只是好奇,但有人知道为什么吗?)。然后,我将其放入闭合功能中,仅适用于HTTP请求。
<?php
use App\Kernel;
// Added my logger() function here resulted in unexplained server error.
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
return function (array $context) {
// Added my logger() function here works
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};
然后,我尝试将其添加到kernal.php中,并且似乎可以工作,但必须称为\ app \ _logger()
。
<?php
declare(strict_types=1);
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
}
if(!function_exists('_logger')) {
// Added my logger() function here works but must be called as \App\_logger()
}
因此,然后我创建了一个名为_logger.php的新文件,该文件有时可以工作,但有时会收到以下错误,并且需要清除缓存以使其再次工作。
<?php
declare(strict_types=1);
if(!function_exists('_logger')) {
// Added my logger() function sometimes works but other times doesn't
}
期望在文件中找到类“ app_logger” 从资源导入服务时,“/var/www/src/_logger.php” “ ../ src/”,但找不到!检查使用的名称空间前缀 /var/www/config/services.yaml中的资源(正在 从“/var/www/src/kernel.php”中导入)。 (500个内部服务器错误)
因此,我又回到了kernel.php中找到它,所有这些都很好,但确实知道添加全局功能的正确方法,当我尝试我的同时,交响乐的缓存有什么含义各种方法。
I thought this would be easy. For troubleshooting, I wish to call my _logger()
function from anywhere in my application.
function _logger(?string $msg=null, int $offset=0, bool $time=false):void
{
$d1 = debug_backtrace()[$offset+1];
$d2 = debug_backtrace()[$offset+2];
syslog(LOG_INFO, sprintf('_logger%s: %s::%s(%s) called by %s::%s on line %s%s', $time?'('.date("Y-m-d H:i:s").')':'', $d1['class'], $d1['function'], json_encode($d1['args']), $d2['class'], $d2['function'], $d2['line'], $msg?' | '.$msg:''));
}
First, I tried adding it to index.php. When added directly in the main body, I got a server error and either with error reporting, couldn't get any info about it (just curious, but anyone know why?). I then placed it into the closure function and it works for HTTP requests only.
<?php
use App\Kernel;
// Added my logger() function here resulted in unexplained server error.
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
return function (array $context) {
// Added my logger() function here works
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};
Then I tried adding it to to Kernal.php, and it seems to work, but must be called as \App\_logger()
.
<?php
declare(strict_types=1);
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
}
if(!function_exists('_logger')) {
// Added my logger() function here works but must be called as \App\_logger()
}
So, then I created a new file called _logger.php which I located in src, and it works sometimes but other times the get the below error and need to clear cache to get it working again.
<?php
declare(strict_types=1);
if(!function_exists('_logger')) {
// Added my logger() function sometimes works but other times doesn't
}
Expected to find class "App_logger" in file
"/var/www/src/_logger.php" while importing services from resource
"../src/", but it was not found! Check the namespace prefix used with
the resource in /var/www/config/services.yaml (which is being
imported from "/var/www/src/Kernel.php"). (500 Internal Server Error)
So, I went back to locating it in Kernel.php, and all is kind of good, but would really know the correct way to add a global function and what are the implications of Symphony's caching when I tried my various approaches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了设计决策,您可以通过在
kernal.php
文件中覆盖名称空间来实现这一目标:注意:此代码未经测试。
来源:
Design decisions aside, you can achieve this by overriding the namespace in the
Kernal.php
file:Note: this code is untested.
Sources: