Over a million developers have joined DZone.

The Do’s and Don’ts of Creating a Package in Laravel

DZone's Guide to

The Do’s and Don’ts of Creating a Package in Laravel

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

PHP is already full of competition when it comes to choosing a framework, however the chase does not ends here. Now, let us suppose that if we choose Laravel for development and we are all set with our root app folder comprising the model, view and controller but in order to add additional functionality we need add-ons. This is why we need Package or Bundle in order to improve the technology. 

We know the fact Laravel developers have described packages as the major and primary unit to add on new features.

“Packages are the primary way of adding functionality to Laravel.
Packages such as  frozennode  have ample lot of downloads but I have analysed that despite of very good functionalities we still do have some features which still needs a lot of improvement.
In this article I am have tried to cover basic things which we should keep in mind while crafting packages in Laravel.

1) Define Your Dependencies

So you need to add your dependencies to your Package’s composer.json file.The developers should develop their own dependencies as a Pacakage in Laravel are similar to sandbox module. 

"php": ">=5.4.0",

"illuminate/support": "4.0.x",

You can also write dependencies in the composer.json file of our package and you can use “php composer.phar update” for root.

2) Do use Sentry in Your Package 

In order to avoid confusion Sentry is a package created by Cartalyst.  Reason behind using sentry is because it is a very easy to use and fortified authentication and authorization package. So, if you want to make your package authentication proof then you can certainly use this package.

However, make sure of its placement as after getting downloaded in your package this can produce errors in your package
This gives a FatalErrorException message:
Class ‘Vendor\Package\Sentry’ not found
This is because Sentry is a third party package and has been recognised under our Package namespace. 
In the basic documentation of Laravel, we can define alias name such as Sentry in the inside the config and app.php file. But this do not comprises any way to register in my package.
So, in order to use this you can do this 
<?php namespace OurVendor\OurPackage;
Directory :
class CustomController extends BaseController {
    public function logOut()

3)  How to use the Controller in the Package 

This is the piece of code which developers used to use earlier:
  {{ Form::open(array('action' => 'ourVendor\ourPackage\ABCController@method')) }}
However, now the developers need to add your Seller\Package namespace in order to use a Package’s Controller.

4) Incorporate Views 

You need to communicate a clear view of your package. We can do this by this given piece of code.
For instance, if you want view be it a users/view, you can add a Package’s name as mentioned below:
You can use master layout instead of the previous ones. The logic goes like this:
The directory is:

5) Incorporating Eloquent 

Being the a very good learners we knew this piece of code from Laravel Documentation
class User extends Eloquent {}
However, this is not beneficial for making a package of your own. So, we can use this code in lieu of the pre-defined one.
<?php namespace ourSeller ourPackage;
use Illuminate Database Eloquent Model;
class ABC extends Model{}

However, there are still more ideas which I have in my mind in order to improve the packages. Will be back with more articles on how to improve the packages which we already have and how to create new ones. 
Why remain a user, when we can be an inventor.
Feel free to drop your queries and comments.

Take a look at an Indigo.Design sample application to learn more about how apps are created with design to code software.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}