header thumbnail image

Advantages to Building Packages within PHP Frameworks

August 6, 2018 - Romin Patel

A PHP Framework

Laravel, Symfony, CodeIgniter and Zend; these are all types of PHP framework that are widely used for Web Application development. While there are multiple reasons for using a PHP framework, the most common and beneficial reason behind using a framework is extendibility. The ability to develop on top of the PHP framework, which can be achieved by using packages.

Packages

The term packages can be fairly confusing at first, especially for someone who is new to Web Application development. Packages are very much like WordPress plugins; the difference is that a package can vary in terms of scalability. For example, a package can solely be feature based such as blogging system or an independent package that can support API services, which can be extended by other packages.

Build your own package

In order to get started with building your own package, it’s important to setup composer, which is a tool for dependency management. Packages are stored within a vendor file, which then can be used throughout the project.

Composer’s default repository for packages is Packagist, this is where you will find all the packages. Let’s say we want to integrate Facebook SDK, simply searching Facebook will bring up the SDK package you require. Packages have two versions, one is “dev-master”, which is a recent built, also known as beta. However, it’s not always stable, therefore it’s important to get the most stable tagged version:

require: “facebook/php-sdk”: “3.2.3”

Run the above command using the terminal in the root directory of your project. Alternatively, you can require the package like so:

"require": {

"facebook/php-sdk": "3.2.*",

}

Once done, you will have Facebook SDK package in your project’s vendor file. Follow the developers guide by Facebook in order to get started.

Returning back to packages, there are quite a few steps when developing one, which may differ according to the PHP framework you are using:

  1. Create a package folder
    1. Set up a symbolic link
      1. Creates a clone of the package, which would automatically update on your changes, which is useful for development
    2. Create a composer file
      1. As shown above.
    3. Create a service provider
      1. A service provider would have on load settings for the following:
        1. Config
          1. Normally an array of settings that a package would have, example: urls, prefixes and general settings.
        2. Migration
          1. A blueprint of your database tables, eg columns, type, foreign and primary keys.
        3. Seeders
          1. At times, you want to automatically insert data on package installation. For example, countries, categories and other default settings.
        4. Views
          1. Front end templates.
    4. Create models
    5. Create controllers
    6. Set up views
  2. Run composer
    1. See if package is discovered
    2. Publish Package

At first glance, these steps can be quite daunting and may urge you to give up or find an alternative. However, it’s not as complicated, and all can be done very smoothly as long as you take your time, understand each step and not overcomplicate things. From here, its a simple process of building the package, just like you would build on top of any PHP framework.

Package Advantages

There are many advantages to building packages, the most beneficial advantage is that you only have to build the package once. Once built, all clients would have the same package, which accommodates the developer as it creates a centralised management structure.  Furthermore, packages are modular which is perfect for maintainability as you can release packages in versions, which enables you to revert to versions or even extend on top.

Overall, packages are good for everything and the advantages are endless. It’s certainly something that every developer should look into, may that be a new or an advanced developer.

Like to stay up to date with the latest from our web development team? Read the most recent post discussing the new Laravel Nova

Back to Blog

Let's start something special

Get in touch with Fifteen today to start your project.

Get In Touch
Footer Call to Action
Sending