Hello Laravel Friends,
In thisblog, I will explain how to setup package development in Laravel.
Laravel Development Package allows developers to create reusable components that can be easily shared and integrated into Laravel applications. Whether you’re building utility libraries, integrating with third-party services, or extending Laravel’s core functionality, package development in Laravel offers a robust framework for building and distributing reusable components.
In this tutorial, we’ll walk through the process of creating a simple Laravel package step by step. Our package will be a basic utility for generating random strings. Let’s get started!
Contents
First, let’s create a new Laravel package using the artisan command-line tool:
php artisan make:package Acme/RandomStringGenerator
This command will generate the necessary directory structure for our package under packages/Acme/RandomStringGenerator.
Navigate to the packages/Acme/RandomStringGenerator directory and create a new class for our random string generator.
mkdir src touch src/RandomStringGenerator.php
Open RandomStringGenerator.php and add the following cod
<?php namespace Acme\RandomStringGenerator; class RandomStringGenerator { public static function generate($length = 10) { return substr(str_shuffle(str_repeat('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $length)), 0, $length); } }
Let’s write some tests to ensure our package works as expected. Create a new directory for tests and add a test file
mkdir tests touch tests/RandomStringGeneratorTest.php
Open RandomStringGeneratorTest.php and add the following code
<?php namespace Acme\RandomStringGenerator\Tests; use PHPUnit\Framework\TestCase; use Acme\RandomStringGenerator\RandomStringGenerator; class RandomStringGeneratorTest extends TestCase { public function testRandomStringGenerator() { $string = RandomStringGenerator::generate(8); $this->assertEquals(8, strlen($string)); } }
Run the tests using PHPUnit:
./vendor/bin/phpunit
Generate API documentation for your package using Laravel Docs Generator:
php artisan make:docs
This command will generate API documentation based on the PHPDoc comments in your code.
Publish your package to Packagist, the default package repository for Composer:
Create a composer.json file in the root of your package directory
{ "name": "acme/random-string-generator", "description": "A simple utility for generating random strings.", "type": "library", "license": "MIT", "authors": [ { "name": "Your Name", "email": "your@email.com" } ], "autoload": { "psr-4": { "Acme\\RandomStringGenerator\\": "src/" } }, "require": {} }
composer require acme/random-string-generator
You’ve successfully created and distributed a Laravel package. Package development in Laravel offers a powerful way to encapsulate and share reusable components with the community. By following best practices and leveraging Laravel’s conventions and tools, you can create high-quality packages that streamline development tasks and accelerate project delivery.
Hire Laravel Developers for any customization requirement for your web application.
Happy Coding!
Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…
The mobile app development field has witnessed a rapid revolution over the past few years.…
Hello Magento mates, Today we will learn to add a call JS on the checkout…
Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…
Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…
In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…