Laravel artisan serve in PhpStorm
I recently started looking into the Laravel framework and I have to admit that it is leaps and bounds ahead from my current favorite framework, Codeigniter. It is so much more elegant in every aspect and takes full advantage of several new PHP features.
One very cool feature as of PHP 5.4, is the built-in web server. By using the built-in web server you no longer need to install servers like Apache in your PC.
Why is this is such a great thing? Personally at any given time, I am developing and maintaining multiple projects. This means that I am forced to either keep all of the projects in the web server’s public html and manage the whole situtation through virtual hosts, or copy-paste the current project that I need in the root of my public html folder and then move it to another location when I am done. With the built-in web server I no longer have to face this problem.
Laravel leverages this functionality through the artisan console tool. In order to serve a Laravel application you just open the command prompt, navigate to the project folder and type artisan serve. As I have already mentioned I prefer using an IDE for php development, so wouldn’t it be cool if we could invoke this functionality directly from our IDE!
Integrate Artisan in PhpStorm
I am using PhpStrom and I am loving it. I really hope that the boys and girl at Jetbrains publish Laravel specific plugins in the near future but in the meantime we can integrate the artisan command tool using this tutorial
Additionaly we can install this syntax helper to make our lives easier
Finally it would be even cooler if we could press the play button at the top right of the IDE and have the command run automatically. I am pleased to say that I after a couple of hours I managed to set this up through the use of an external tool.
Creating an External Tool
- Create a .bat file that runs the following commands and save it anywhere you want
tasklist /FI "IMAGENAME eq php.exe" 2>NUL | find /I /N "php.exe">NUL if %ERRORLEVEL%==0 taskkill /IM "php.exe" start php artisan serve
- Open PhpStorm and create a new Debug/Deploy Configuration.
- Go to Edit Configuration->PHP Web Application->Before Launch->External Tool and select the bat file that you created
- Make sure that at the field Working directory you insert $ProjectFileDir$
So what actually happens is that we are instructing the IDE to navigate to the project directory and run a few commands. These commands check whether any php scripts are currently running and if they do, the bat file kills them and begins and new instance of the buitl-in web server. The key here is that since we have navigated to the project’s root directory we are invoking the project’s artisan application.