简体   繁体   English

试图从全局命名空间调用函数“yaml_parse_file”

[英]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.

相关问题 尝试从Silex的全局名称空间调用函数 - Attempted to call function from the global namespace on Silex 为什么yaml_parse_file返回true? - Why is yaml_parse_file returning true? 无法打开流没有这样的文件或目录:带有 yaml_parse_file 的 Wordpress 自定义函数 - Failed to open stream No such file or directory : Wordpress custom function with yaml_parse_file Symfony2 + Twig:尝试从全局名称空间调用函数“ replace” - Symfony2 + Twig: Attempted to call function “replace” from the global namespace UndefinedFunctionException:尝试从命名空间Controller调用函数 - UndefinedFunctionException: Attempted to call function from namespace Controller 尝试从名称空间symfony Controller调用函数 - Attempted to call function from namespace symfony Controller 尝试从命名空间“App\Controller”调用 function - Attempted to call function from namespace “App\Controller” 尝试从全局名称空间Symfony2 + Ekino Wordpress调用函数“ wp” - Attempted to call function “wp” from the global namespace Symfony2 + Ekino Wordpress 尝试在 Prestashop 中使用 Mail 方法时尝试从全局命名空间调用函数“idn_to_ascii” - Attempted to call function "idn_to_ascii" from the global namespace when trying to use Mail methods in Prestashop 尝试从名称空间“ Controller”调用函数“ split” - Attempted to call function “split” from namespace “Controller”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM