[英]Attempted to call function "yaml_parse_file" from the global namespace
I am new to Symfony, Facing problem while trying to run the cron job.我是 Symfony 的新手,在尝试运行 cron 作业时遇到问题。 I am really clueless, whats wrong here.
我真的一无所知,这里有什么问题。 It seems that I am trying to access some functions present in
app/config/functions.php
from the global namespace, But I can't figure out which namespace is it.似乎我试图从全局命名空间访问
app/config/functions.php
functions.php 中存在的一些函数,但我无法弄清楚它是哪个命名空间。 Following is my code.以下是我的代码。
<?php
namespace App\Command;
use App\Services\Upcontent\Upcontent;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class UpcontentRefreshCommand extends Command
{
protected static $defaultName = 'app:upcontent-refresh';
private $upcontent;
public function __construct(Upcontent $upcontent)
{
$this->upcontent = $upcontent;
parent::__construct();
}
protected function configure()
{
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln([
'',
'=================',
'Upcontent Refresh',
'=================',
'',
]);
$output->writeln('Clearing Cache...');
clear_cache();
$output->writeln('Cache Cleared');
$output->writeln('Refreshing Sports Topic...');
$output->writeln('Loading, be patient...');
$sports = $this->upcontent->getTopic('########');
$output->writeln([
'',
'=====================',
'End Upcontent Refresh',
'=====================',
'',
]);
}
}
?>
The error occur when I run, php72 bin/console app:upcontent-refresh
Please help.运行时出现错误,
php72 bin/console app:upcontent-refresh
请帮忙。 Thanks in advance.提前致谢。
You will probably have to update the "autoload"-section to make sure that your custom functions.php is loaded.您可能需要更新“自动加载”部分以确保加载了您的自定义 functions.php。
{
"autoload": {
"psr-4" {
"App\\": "src/"
},
"files": ["app/config/functions.php"]
}
}
You might also want to refactor that file to instead move the functions into some kind of service-class, eg like this:您可能还想重构该文件以将函数移动到某种服务类中,例如:
# src/Yaml/Parser.php
<?php
namespace App\Yaml;
class Parser
{
public function parseFile(string $fileName)
{
// The logic from your yaml_parse_file() inside your functions.php
}
}
Then in your command (or wherever you need your custom yaml parsing) inject the service:然后在您的命令中(或您需要自定义 yaml 解析的任何地方)注入服务:
use App\Yaml\Parser;
class MyService
{
private $yamlParser;
public function __construct(Parser $yamlParser)
{
$this->yamlParser = $yamlParser;
}
// ...
public function something()
{
$this->yamlParser->parseFile($filename);
}
}
Since Symfony provides a Yaml-component, you might even want to use that instead.由于 Symfony 提供了一个 Yaml 组件,您甚至可能想要使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.