Easier PHP development in less than 10 simple steps

I’m sure we all have our little tool kit we install while we build our applications, mine included the Symphony VarDumper Component another one I like to install in development is PHP-Console along with a few other things that I wrote years ago and haven’t released.

Anyway, it’s annoying having to install my development toolkit every time I want to build a plugin or debug a controller, or whatever I’m working on at the time.

So I found a tip in the Symphony docs that shows you how to prepend your globally loaded composer packages into every PHP script on your localhost or your development server.

It’s really simple honestly, basically install the packages you use to develop with globally, then modify the php.ini file to prepend the autoloader.php to all your PHP scripts.

So just to install the VarDumper Component you would install it globally like this

# install globally all your development packages like this.
composer global require symfony/var-dumper
composer global require php-console/php-console

Now by installing these globally they will be located in your home directory unless you sudo installed them then they would be in /root/ I think, I don’t know since I haven't tried it, I think running sudo composer is just asking for troubles.

Next to load these packages and use them in every PHP project on the server we installed these on, I normally only install these on my localhost and my development server, do not install these on production servers.

But the final step to all this is modifying your php.ini file, if you don’t know where your php.ini file is located at you can do the following in your console.

php -i | grep "php.ini"

which should give you some output like the following

➜ php -i | grep "php.ini"
Configuration File (php.ini) Path => /usr/local/etc/php/7.2
Loaded Configuration File => /usr/local/etc/php/7.2/php.ini

So the final task to complete this is using the editor of your choice, I am just going to use nano because it’s quick and easy to load the Loaded Configuration File, go to the bottom of the file, and add the following

sudo nano /usr/local/etc/php/7.2/php.ini

Go to the bottom of the file add the following line, replace my username shawnc with your Linux username.

## Don't forget to replace shawnc with your username here.
auto_prepend_file = /home/shawnc/.composer/vendor/autoload.php

And that's it, restart your PHP server with either

sudo service php7.2-fpm restart
sudo systemctl restart php7.2-fpm

Or whatever command and version PHP you have installed, a good trick there is just type PHP and hit tab it should autocomplete the running service when you are trying to restart it. But you should also know what freaking version of PHP you are running!

Once you have restarted your service you can test it by just add this simple PHP file and test if it works

<?php

  $array = [1,2,3,4];
  dd($array);

Just copy that small file and paste it as test.php in your current directory then run

php test.php

It should give a pretty var dump of the array, other packages you add will require more configuration. That's on you to read the package instructions and follow them.

Anyway thanks for listening to me ramble on about PHP and development shortcuts.

Did you find this article valuable?

Support Shawn Crigger by becoming a sponsor. Any amount is appreciated!