How create package in Laravel 5?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Create a Package in Laravel 5: Navigating Legacy Tooling

Creating reusable packages is a fundamental skill for any senior developer working within the Laravel ecosystem. When we look back at older versions, like Laravel 5, the tooling landscape was different. The command you cited, php artisan workbench vendor/package --resources, is indicative of more modern scaffolding tools that have evolved over time. If you are stuck in a Laravel 5 environment and need to build a package, understanding the underlying structure provided by Composer is the key.

This post will walk you through the correct, developer-focused method for creating a viable package in the context of Laravel 5, explaining why direct artisan commands might be missing and how to achieve the goal manually with best practices.

The Evolution of Laravel Tooling

The reason you cannot find that specific command in Laravel 5 is tied to the evolution of the framework itself. As frameworks mature, their scaffolding and package management tools are often refined or replaced. In older versions, development relied heavily on Composer for dependency management and PSR-4 autoloading rather than centralized artisan commands for basic structure generation.

When working with Laravel 5, the core mechanism for making your code usable across different projects remains consistent: proper organization within a Composer package structure. We must build this foundation ourselves. Understanding these principles is crucial, regardless of which version of Laravel you are targeting—whether you are building an application or a library for the community, much like when discussing architecture on platforms like https://laravelcompany.com.

Manual Package Creation in Laravel 5

Since we lack the high-level artisan command, creating a package involves setting up the standard PHP/Composer structure manually. This ensures that your package adheres to PSR standards and is easily discoverable by other developers.

Step 1: Initialize the Package Directory

First, create the root directory for your new package. Let's call it MyCustomPackage.

mkdir MyCustomPackage
cd MyCustomPackage

Step 2: Set up Composer Configuration

The heart of any PHP library is the composer.json file, which defines dependencies and autoloading rules. This file tells Composer how to load your package's classes.

Create a composer.json file in the root directory:

{
    "name": "vendor/my-custom-package",
    "description": "A custom utility package for Laravel 5 projects.",
    "type": "library",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Vendor\\MyCustomPackage\\": "src/"
        }
    },
    "require": {
        "php": ">=5.6"
    }
}

Explanation:

  • "name": This must be unique and follow the standard Composer namespace convention (e.g., vendor/package-name).
  • "autoload": {"psr-4": ...}: This is the most critical part. It maps the namespace (Vendor\MyCustomPackage\) to the physical directory where the source code resides (src/).

Step 3: Create Source Files

Now, create the necessary directories and files based on the autoloading definition.

mkdir src
touch src/Package.php

Populate src/Package.php with your package logic:

<?php
namespace Vendor\MyCustomPackage;

class Package
{
    public function greet()
    {
        return "Hello from the custom Laravel 5 package!";
    }
}

Step 4: Install and Test

Navigate back to the root directory (MyCustomPackage) and run Composer to install the package locally (or publish it for distribution later):

composer install

This process results in a valid, autoloaded PHP package that any Laravel 5 project can immediately use by referencing the namespace defined in the composer.json. For robust development practices, always ensure your code adheres to object-oriented principles, which aligns perfectly with modern architectural goals discussed at https://laravelcompany.com.

Conclusion

While automated tools streamline development significantly, understanding how packages are structured manually is essential for deep framework knowledge. By mastering the Composer setup—specifically defining autoloading via PSR-4 standards—you can create professional, maintainable packages in any environment, including legacy setups like Laravel 5. Focus on the structure first, and the tooling will follow.