1

I am using PHP and Yii Framework, so I need to call php yii with each command. To make this just a little bit more handy, following this example, I have created run.bat file, with this content:

@echo off
php yii %*

I have placed it in %SystemRoot% (c:\WINDOWS\), so it is accessible system wide, from any path.

To my extreme surprise I have found the following:

  1. Command-line (Terminal in Windows 11): Both run and run.bat works just fine.
  2. Windows PowerShell: Both run and run.bat works just fine.
  3. Terminal inside PhpStorm: Must use run.bat because run doesn't work:

enter image description here

In the same time both php and yii works in PhpStorm like I charm and I am not forced to execute php.exe or yii.bat.

What am I missing? What changes should I introduce to file content or file location (or maybe PATH system variable) to be able to use just run in PhpStorm?

6
  • 1
    Does the same command works if you launch the same terminal outside of PhpStorm? Just in case: in PhpStorm you seems to be using BASH and not CMD or PS. And each shell will have own rules deciding what file is executable when file extension is omitted. (echo %PATHEXT% in CMD will show what file extensions are used and in what order when you try to execute your run without .bat) Commented Nov 19, 2023 at 18:24
  • Regardless of the location of php.exe and yii.bat you should still use their full paths, and extensions: @"%~dp0php.exe" "%~dp0..\htdocs\yii\framework\yii.bat" %*. It is almost always prudent to explicitly define the working directory at the outset of your scripts, (this allows for you to use relative locations if you prefer). You should never rely upon %PATH% or %PATHEXT%, being preset with your required content, (doing so also adds a time overhead). I'd have to ask why you aren't using yiic.bat, which is the console interface script. Commented Nov 19, 2023 at 21:32
  • @Compo The below works, so I still don't see the reason for putting full paths. I am not relying on %PATH% in anywhere. If there will be no path to php.exe executable defined then nothing will work, both my scripts and php yii call. The yiic is a PHP script from versio 1.1 of Yii Framework. It was replaced with yii in version 2.0. I am using it directly. As in question -- my script is just an shorthand to call y instead of php yii as so far. Nothing else. The yii script is still responsible for all the magic here. I am not changing this in any way. Commented Nov 21, 2023 at 12:24
  • 1
    If you use what's in the current answer, you are still relying on %PATHEXT%, and you explained in the body text, item 1. that %PATH% is relied upon! Commented Nov 21, 2023 at 12:57
  • Sorry, but I am too newbie to this topic, so I still don't understand your point of view. In my system %PATHEXT% includes extensions only (no paths). While it truly has .cmd listed, in my approach I am not using this extension. I am executing y command which calls y file, not y.cmd. Commented Nov 21, 2023 at 13:10

1 Answer 1

1

Based on comments and this article the correct resolution is to:

  1. Pick a folder listed in %PATH% system variable (C:\XAMPP\php in my case).

  2. Put a file named i.e. y.cmd in this folder with the following content:

@php.exe yii %*
  1. Put a file named i.e. y in the same folder with the following content:
#!/bin/bash
php.exe yii $@

That should be all. Starting from this point it should be possible to call both y and y.cmd from any location in given system and with using cmd command-line, Windows Terminal, Git Bash (including PhpStorm) etc.

BTW: There is no path for php.exe in above scripts, because both y and y.cmd scripts as good as php.exe file are placed in the same folder. If these files are put in separate folders then using full paths is necessary.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.