0

Here is my problem (which is more a workflow concern than a real problem):

I'm currently learning how to use Symfony 4 and as you may know, this framework uses a lot of commands with PHP, using the $ php bin/console command command line.

Here's my question: is there any possible way to configure a CMD window so I dont have to always type the $ php bin/console before the command name?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Welcome as a new user to [SO]. You should take the [tour] and read [ask]. You communicated will all the answerers but didn't [vote up](https://stackoverflow.com/help/privileges/vote-up) or [check mark an answer](https://stackoverflow.com/help/accepted-answer) –  Jul 06 '18 at 16:21

3 Answers3

2

You will need a batch file to accomplish your task Save the following code in your project folder where the bin folder and other Symfony files resides and name it SymfonyConsole.bat

@echo off

:SymfonyConsolePrompt
set "SymfonyConsoleCommand="
echo,
set /p "SymfonyConsoleCommand=Symfony Console Command>"
for /F "tokens=*" %%C in ("%SymfonyConsoleCommand%") do set "SymfonyConsoleCommand=%%C"

if defined SymfonyConsoleCommand (
    php bin/console %SymfonyConsoleCommand%
) else (
    choice /C NY /N /M "Do you want to quit Symfony Console Prompt?[YN]"
    if errorlevel 2 exit /b
)
goto :SymfonyConsolePrompt

Then at the command prompt type > SymfonyConsole You will get this prompt Symfony Console Command> ready to get commands for the console component of Sympony.

ex: Symfony Console Command>server:start 0.0.0.0:8000 will automatically run php console/bin server:start 0.0.0.0:8000 and then waits for the next Sympony console command until you hit enter without entering anything in which case you can quit the batch file and return to the cmd prompt.

sst
  • 1,443
  • 1
  • 12
  • 15
  • Thanks a lot ! It works perflectly ! Could you maybe detail the lines of the code, in the case I would like to reuse this for another command (I'm thinking about the Composer packages) Thanks again ! :) – Tok the digger Jul 05 '18 at 16:40
  • The code is pretty simple there is not much to explain. It seems you are not familiar enough with batch files. You can refer to [ss64.com](https://ss64.com/nt/commands.html) and [Windows XP Batch Reference](https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490869(v%3dtechnet.10)) to get information about each command and batch scripts syntax. Quick hint: For using `composer` package you just need to replace `php bin/console` with `composer` and save it in a separate batch file. – sst Jul 06 '18 at 09:41
  • Also read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – sst Jul 06 '18 at 09:44
0

The easiest (and recommended) way would be to load the php bin/console into a variable then just reuse the variable

I'm not familiar with the $ php bin/console syntax but I think in windows script the equivalent is just php bin/console

So you can try this:

SET phpconsole=php bin/console/

"%phpconsole%Command1"
"%phpconsole%Command2"

would result in this:

"php bin/console/Command1"
"php bin/console/Command2"

What does your existing windows batch file look like?

Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
  • Thanks for your answer but I think i have not been clear enough. Im running my commands dirrectly from the command prompt, I'm not using any batch file. To be more precise, I'm working in a directory, which contains all my Symfony files. One of this folder names "bin" contains one file named "console" (without any extension). So I'm running the command prompt in my project folder, and call php for running this "bin/console" file with some arguments (such as "doctrine:database:create"). My purpose is to make my Cmd promp to alwas run my commands with "php bin/console". – Tok the digger Jul 05 '18 at 03:04
  • A symfony or php person could probably answer this much quicker than me but I'll keep trying :) I've done some googling and I think the 'command line' you're talking about is the special bundled symfony one. If you see a `$` in your command line, you're using the symfony command line. If you see something like `C:>` in your command line then you're using the windows command line. I can help you to make this simpler in the windows command line but I don't know anything about the symocny command line. – Nick.Mc Jul 05 '18 at 03:17
  • I think this is exactly what you're after: https://stackoverflow.com/questions/15597067/how-to-run-php-from-windows-command-line-in-wampserver – Nick.Mc Jul 05 '18 at 03:19
  • Well I put the $ in my post to point out the fact I'm using a command prompt, but I actually use the Windows' one and the exact linecode I use is `D:\Documents\Projects\Web\portfolio\www\tok-portfolio>php bin/console some:symfony:command`, which is gonna run _some symfony command_ inside my tok-portfolio folder. What I want is doing this instead : `D:\Documents\Projects\Web\portfolio\www\tok-portfolio>some:symfony:command`, like a sort of shortcut. I already saw the link you just post, but I don't get how it solves my problem :/ Thank you again for your help anyway ! – Tok the digger Jul 05 '18 at 04:28
  • Looks like sst hsa a good answre. In future it helps a lot to describe the entire problem – Nick.Mc Jul 05 '18 at 05:35
  • Yes, sorry if I wasn't clear enough :( Next time I will know ! Anyway, thank you very much for your time ! – Tok the digger Jul 05 '18 at 16:44
0
@echo off
setlocal

:: Check 1st argument for help.
if "%~1" == "-h" goto :help
if "%~1" == "--help" goto :help
if "%~1" == "/?" goto :help

:: Check script arguments for program.
set _program=%*

:: If not program defined, prompt for program.
if not defined _program set /p "_program=Enter the program: "

:: If the required program is not defined then exit.
if not defined _program exit /b 1

:: Set a prompt string.
set "_prompt=%_program:"='%>"

:: Show information before start of the loop.
echo Pass arguments to '%_program%' in a loop.
echo.


:begin_loop

:: Prompt for arguments.
set "_args="
set /p "_args=%_prompt% "

:: Restart loop if no args or exit if requested.
if not defined _args goto :begin_loop
if /i "%_args:"='%" == "exit" exit /b 0

:: Clear local variables and then run the program with arguments.
setlocal
set "_prompt=" & set "_program=" & set "_args=" & %_program% %_args%
endlocal

echo.
goto :begin_loop


:help

:: Help information.
echo Pass arguments to a select program in a loop.
echo If script arguments are passed, then that will be the program.
echo If a program is not defined, then a prompt will ask for a program.
echo Variables _prompt, _program and _args are cleared with each execution.

This is same as I have posted at my website, except a line removed that sets a ANSI escaped prompt for conemu. Doubt this site would keep the control character intact if posted.

I call it loop.cmd and place it in PATH (see note). It is made for any command so is universal.

To use:

loop command

then you will get a prompt with the command in it i.e.

command>

What you type at the prompt now will be prefixed with command and executed.

You can just type exit to end the loop.

Based on your code example of use:

loop php bin/console

then add your command at the prompt:

php bin/console> command

A Python version exists too though I use the batch file as it keeps auto completion etc.


Note:

The PATH I mention is where the script can find files on your system. A path in the PATH environmental variable or the current working directory.

Recommend creating a folder path i.e. %SystemDrive%\bin and add that to the system variable PATH where your PATH suitable scripts can be stored. Or you could just add the script to an existing directory in the variable PATH if you are uncomfortable to add your own directory to PATH.

How to set the path and environment variables in Windows

michael_heath
  • 5,262
  • 2
  • 12
  • 22
  • Looks very efficient, but when you say "Place it in PATH", what do you mean? For what I know, there is not a PATH folder but only a list of path inside the PATH environment variable. So where do I put the file itself? Anywhere? Is there a recommended folder for this kind of cmd files ? Thank you ! :) – Tok the digger Jul 05 '18 at 16:43
  • Updated answer with more details and a *note*. I do imply the `PATH` environmental variable so you can access from anywhere. I do not recommend an existing system folder as you can lose track of files though if you do not want to follow the *note* in the answer, then perhaps add the script to the `Windows` directory in root of the system drive. – michael_heath Jul 06 '18 at 02:08