myregistration's avatar

In-depth Tutorial on Creating Packages

Can someone please create an in-depth tutorial on creating packages? The tutorials or examples I've found are way too simple. We need a complex real-world example. I want to code features that can be used in multiple projects and don't want to maintain them per project. So ideally I want to create a package for each feature and then add them to a project via composer. Thanks!

0 likes
4 replies
LaryAI's avatar
Level 58

Creating a comprehensive tutorial on package development in a single response is quite extensive, but I can certainly get you started with some key concepts and steps you'll need to follow to create a reusable package for PHP projects, typically to be used with Composer.

Step 1: Setting Up Your Package

  1. Create a new directory for your package and cd into it.
  2. Initialize a new Git repository with git init.
  3. Create a composer.json file in the root of your package directory. This file will define your package's metadata and dependencies.
{
    "name": "vendor/package-name",
    "description": "A short description of your package",
    "type": "library",
    "license": "MIT",
    "authors": [
        {
            "name": "Your Name",
            "email": "[email protected]"
        }
    ],
    "require": {}
}
  1. Set up PSR-4 autoloading for your package's classes by adding an autoload section to your composer.json.
{
    "autoload": {
        "psr-4": {
            "Vendor\PackageName\": "src/"
        }
    }
}
  1. Create the source directory (src/) and any subdirectories as needed for your package structure.

Step 2: Developing Your Package

  1. Write the code for your package inside the src/ directory. Make sure to use the namespace you defined in your composer.json file.
  2. Write unit tests for your package. You can use PHPUnit or any other testing framework you prefer. Place your tests in a tests/ directory.
  3. Document your package. Create a README.md file with installation instructions, usage examples, and any other relevant information.

Step 3: Testing Your Package Locally

  1. Run composer install in your package directory to install any dependencies.
  2. Test your package by running your unit tests.
  3. To test your package within a local project before publishing, you can add a repositories section in the project's composer.json file pointing to your local package directory:
{
    "repositories": [
        {
            "type": "path",
            "url": "/path/to/your/package"
        }
    ]
}

Then, require your package as you normally would:

composer require vendor/package-name

Step 4: Publishing Your Package

  1. Push your package to a Git repository on GitHub, GitLab, Bitbucket, or any other service you prefer.
  2. Tag a release of your package. Semantic versioning (e.g., v1.0.0) is a common practice.
  3. Submit your package to Packagist, the main Composer repository, so others can easily find and install it.

Step 5: Maintaining Your Package

  1. Listen to user feedback and make improvements as necessary.
  2. Keep your dependencies up to date and ensure compatibility with the latest versions.
  3. Tag new releases with version numbers when you make changes.

This is a very high-level overview, and each step can be broken down into much more detail. For a real-world example, you might want to look at popular packages on GitHub and study their structure and setup. Additionally, consider contributing to open-source packages to get a feel for best practices and common patterns.

Remember, package development is an iterative process. Start simple, get feedback, and iterate to improve your package over time.

martinbean's avatar

@myregistration Here’s how I create packages:

Create a new folder on your computer. Inside that folder, put a /src sub-folder, and inside there, put a service provider class with the name of your package, i.e. FooServiceProvider. Your directory structure will look like this:

/src
    FooServiceProvider.php

Put a composer.json file in the root. Then initialise a Git repository in the folder (git init).

You can now either push the package to somewhere like GitHub, or use it locally in an application.

If you want to keep your package into another folder on your computer, then in the application you wish to use the application in, you need to edit its composer.json and add a repositories section that looks like this:

"repositories": [
    {
        "type": "path",
        "url": "./path/to/your/package"
    }
]

If you have specified the correct path, then you should be able to install your package using Composer with a command like composer require your-vendor-name/your-package-name. The your-vendor-name/your-package-name should be what’s in the name section of your package’s composer.json file.

In terms of developing the package itself, I treat the /src directory as a Laravel application almost. So I will have directories for console commands, controllers, queue jobs, and anything else the package should contain.

1 like
myregistration's avatar

@martinbean Thank you for the reply, but I'm more of a visual person and I am hoping Laracasts will create some advanced content that explains how to create packages that contain Livewire components, services, blade components, configurations, customizable files, etc. and also how to allow developer and users to update the packages. Also, I want to use these packages privately so I'm not sure if they can be used on Packagist.

1 like

Please or to participate in this conversation.