简体   繁体   English

在 symfony 3.4 命令上获取基础 url

[英]Get base url on symfony 3.4 command

I'm trying to get the base url on a command to get localhost on local and the production url on producction but all the exmaples I found are using the reuqest object but I don't have it on a command:我正在尝试在命令上获取基础 url 以获取本地主机,在生产上获取生产 url 但我发现的所有示例都在使用请求的 ZA8CFDE6331CBD59EB2AC96F8911 上的命令但没有:

class PasswordExpirationCommand extends Command
{
    private $ms;
    private $translator;
    private $em;

    public function __construct(MailService $ms, TranslatorInterface $translator, EntityManagerInterface $em)
    {
        parent::__construct();
        $this->ms = $ms;
        $this->translator = $translator;
        $this->em = $em;
    }

    protected function configure()
    {
        $this
            ->setName('app:password:expiration')
            ->setDescription('Expire 3 months old password');
    }

    /**
     *
     *  
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int|void|null
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $base_url =....//need http://localhost/ or https://production_url 
        //reminders
        $this->passwordExpirationReminder();
    }

If you have already installed dotenv component so you can set a new variable in your.env file如果您已经安装了dotenv组件,那么您可以在您的 .env 文件中设置一个新变量

BASE_URL = 'your_url' //need http://localhost/ or https://production_url

If not you can put it in parameters.yml it's a good practice also.如果没有,你可以把它放在parameters.yml中,这也是一个好习惯。

Then get it by (it depends in witch version of sf you have)然后得到它(这取决于你拥有的 sf 女巫版本)

 $base_url = $this->getContainer()->getParameter('translator')

Don't forget to extends your command from ContainerAwareCommand instead of Command to have access to the container不要忘记从ContainerAwareCommand而不是Command扩展您的命令以访问容器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM