Packagist & Github – How-To Guide

Test it

If you want to make sure everything has worked, now is the time to add your new package to your project. This can be any project that you’d like to use with Composer i.e. it doesn’t have to be Laravel, Code Igniter etc.

In the root directory of your project, create another *composer.json* file. In its most basic form, all you need to do is tell your project that it needs to install your package. For our package, *composer.json* looks like this. At this point I’m hoping you aren’t using ‘digitalformula’ as your vendor name – it just won’t work. 🙂

"require": {
"digitalformula/hello-world": "v1.0"

That will force the project to only install releases of our package that are tagged with as version v1.0. No other releases will be accepted.

If you like, please feel free to test this by installing this package from digitalformula/hello-world. The latest release version at the time of writing this document is “v1.0.1”.

From a terminal session and while in the root directory of your project (the same directory that contains the *composer.json* file above), run the command to update Composer.

composer install
composer dump-autoload -o

Technically, the second command isn’t required but I like to run it anyway, just to make sure the autoload files are generated properly.

It’s also worth noting that if you have a project that already uses Composer, you need to change the above commands to the following:

composer update
composer dump-autoload -o

If everything ran successfully, you should now see a “vendor” directory in the root of your project. Inside that will be a “composer” directory, containing a bunch of PHP files that tell PHP to autoload the packages. There will also be a directory for your vendor name – in my example it is “digitalformula”. Inside that there will be a directory called “hello-world” that contains all our Github/Packagist package files. Nice!

To make sure the package works, add the following line to your project. Where this line goes will depend on the project you’re creating but if you have a simple one-page website with an index.php in the root directory, put it in there for testing purposes. Note that this isn’t required for Laravel as it handles the autoloading for you. If you do add the line, remember to change the path to match your system.

require_once __DIR__ . '/../vendor/autoload.php';

Finally, test that the package definitely works.

$hw = new \DigitalFormula\HelloWorld\PrintHelloWorld();
echo( $hw->SayIt() );

If you see ‘Hello, World!’ on the screen, everything has worked. 🙂