简体   繁体   中英

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:

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

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.

Then get it by (it depends in witch version of sf you have)

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

Don't forget to extends your command from ContainerAwareCommand instead of Command to have access to the container

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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