Laravel 5: Creating Package

Laravel 5: Creating Package

I would not be surprised, if when I get back to it some time later it is not going to work. It took me awhile, lots of teeth gritting and almost a dozen tutorials to make it work and I am not entirely sure, why the heck attempts before successful one did not work.

Try reading also this.
Also this for some testing ideas.
Here is also not a bad read.

Want some laugh? Read this, how Laravel folks screwed up.


Step one

Create this folder structure inside of folder ‘packages’, which should be sitting in root path of your Laravel installation.
You can change names later.
But for now, stick “religiously” to naming and even case – to the letter and last dot.

Step two

Open your console and get into folder \imagecache :

Once inside of mentioned dir, run this command:

… and answer some questions. They are simple enough.

You should get something like this:

This is your package JSON file.
It should reside in topmost folder of your package – in this case: \imagecache.

Step three

Create files as shown on image:

  1. this is your service provider
  2. this is your work class, where you actually do something
  3. and this is your Facade, that allows to easy access toy your package output anywhere

And here is code.

service provider

class

facade

Step four

Now we have to register stuff with \config\app.

Open this file: config\app.php and add to :

and to:

Step five

Update your main composer file (composer.json), one that sits in root folder of your Laravel install.

Just find ‘autoload section and ADD THIS LINE marked content.

You have to run composer command – since you adjusted composer.json:

Step six

This is how you use it:

 

If you did all as outlined and LARAVEL DEITY is in good mood, you should see this:

BTW:

I tried to use this new Laravel auto-discovery of ServiceProviders, where you could spice up a bit composer.json:

… and do without adding:

… to app.providers, but I could not make it work.