How to set the environment in a Symfony2 console command

后端 未结 6 1183
清歌不尽
清歌不尽 2021-01-31 14:46

Hopefully a simple question - how does one specify which environment to use when running a console command in Symfony2. I\'ve created a few commands, however I would like to run

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 15:24

    To answer the question @croca had, to expand on what @Francesc Rosàs posted, and as @Problematic suggested.

    If you look in app/console you should see $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');

    All this does is checks the input arguments passed to the console for --env or -e, checks the default value from getenv('SYMFONY_ENV'), or sets it to dev if neither are supplied.

    It is then passed to $kernel = new AppKernel($env, $debug);

    You could essentially either make changes directly to app/console to achieve your application's specific functionality or copy app/console to a separate file such as app/exec, then process the $env variable how you prefer to determine the desired environment.

    Simple Example: app/exec

    #!/usr/bin/env php
    run($input);
    

    Then call php app/exec namespace:command arguments --flags

    Additionally you could process your own application instead of using the app/console AppKernel - instructions from Symfony can be found here: http://symfony.com/doc/current/components/console/introduction.html

提交回复
热议问题