Download the PHP package aminulbd/laravel-packages without Composer
On this page you can find all versions of the php package aminulbd/laravel-packages. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-packages
Laravel Packages
A simple Laravel package that provides a way to make Laravel more modular and extensible.
Introduction
The laravel-packages package allows you to create modular packages within your Laravel application. This helps in organizing your codebase by grouping related functionalities into separate packages, making your application more maintainable and scalable. Each package can have its own routes, controllers, views, migrations, and more.
Requirements
- Laravel 9.x or higher
- PHP 8.0 or higher
Installation
You can install the package via Composer:
The vendor:publish
command will publish the configuration file config/packages.php
and a sample package in the /packages
directory.
Configuration
Open the config/packages.php
file and update the roots
array with the paths where your packages are located. By default, the configuration is set to use the /packages
directory of your Laravel project, but you can change or add as many paths as you need.
Creating a Package
To create a package, follow these steps:
-
Create a Package Directory: Create a new directory for your package inside one of the paths specified in the
roots
array. For example, create/packages/YourPackage
. -
Create an Index File: Inside your package directory, create an
index.php
file. This file will return an array with package configurations.Example
/packages/YourPackage/index.php
: -
Set Up Autoloading: The
autoload
key maps your package's namespace to its source directory. This allows Laravel to autoload your package classes. -
Create a Service Provider: In your package's
src
directory, create a service provider class that extendsIlluminate\Support\ServiceProvider
.Example
/packages/YourPackage/src/YourPackageServiceProvider.php
: - Add Package Functionality: Add your package's routes, controllers, views, migrations, etc., within the package's directory structure.
Using the Package
Once your package is set up, Laravel will automatically load it according to the configurations provided. You can use all Laravel features within your package, including routing, controllers, views, and more.
For example, to add routes in your package, you can create a routes
directory and define your routes in a web.php
file:
Then, in your YourPackageServiceProvider
, load the routes:
Handling Package Activation
One of the powerful features of the laravel-packages package is the ability to activate or deactivate packages, for instance, from your application's admin panel. To handle package activation, you have two options:
1. Using the Configuration File
Edit the enabled
key in the config/packages.php
file and set its value as an array of IDs representing the packages you want to enable.
2. Using a Custom Activation Handler
Implement a class that implements the \AminulBD\Package\Laravel\PackageActivationHandler
interface. This allows you to dynamically determine which packages are enabled, e.g., based on database records.
Example implementation:
Then, update your config/packages.php
to use the custom activation handler:
Publishing Package Resources
If your package contains resources that need to be published to the main application (like views, configurations, assets), you can use Laravel's publishing mechanism.
In your package's service provider, add:
Consumers of your package can then publish these resources using:
Conclusion
The laravel-packages package makes it easy to create modular, self-contained packages within your Laravel application. By organizing your code into packages, you can improve maintainability, encourage code reuse, and make your application more scalable.
For more information and advanced usage, please refer to the package's repository and Laravel's official documentation on service providers and package development.