-1

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();
    }
a_fr159
  • 17
  • 2
  • I think you can't do that, you should inject a parameter into the command – Manzolo Jan 14 '22 at 11:13
  • And it's described [in the manual](https://symfony.com/doc/3.4/console/request_context.html). – msg Jan 14 '22 at 14:51
  • You can check the link below. [https://stackoverflow.com/questions/8811251/how-to-get-the-full-url-for-an-asset-in-controller](https://stackoverflow.com/questions/8811251/how-to-get-the-full-url-for-an-asset-in-controller) – Fuzzer Apr 14 '22 at 07:32

1 Answers1

0

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