Shorthand notation for Yii commands in Git Bash and Windows Terminal

Developing PHP applications in Yii 2 makes you use Yii command-line commands quite often (for example for migrations). Since I am a Windows maniac that uses XAMPP, I have to type:

php yii

before each command each and every time.

A bit tiring so I wanted to make this a little bit easy. For example, to be able to type:

y migrate

instead of:

php yii migrate

Getting there was quite easy for Windows scripts and using Windows Terminal. Things has got a little bit complicated when trying to achieve the same in PhpStorm. Which uses GitBash instead of Windows Terminal.

To be able to execute my “shorthand” scripts anywhere within the system, I had to put them to any folder that is listed on my %PATH% system variable (hold Left Alt + double click This PC then go to Advanced system settingsEnvironment Variables… → System variables to learn yours).

The C:\xampp\php sounded like a good choice. For quite obvious reasons I didn’t want to use %SystemRoot% (Windows folder).

In the above mentioned path I have created y.cmd file with the following content:

@php.exe yii %*

That was enough to be able to execute just y command instead of php yii in Windows Terminal, Windows PowerShell or cmd command-line. But PhpStorm and Git Bash in general was still objecting — allowing to execute y.cmd only and throwing bash: y: command not found when trying to use y there.

With some extra advise I have managed to figure out that creating y file (no extension) in the same location as y.cmd file (that is: C:\xampp\php) and with the following content:

#!/bin/bash
php.exe yii $@

Is all that I need.

Right now I can use just:

y migrate

or anything else that Yii provides (or any custom command of mine own) in both Windows Terminal, Git Bash and PhpStorm and within any path in my system (that is for any Yii project).

Leave a Reply