PHPackages                             williamug/modular - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. williamug/modular

ActiveLibrary[Framework](/categories/framework)

williamug/modular
=================

The Modular provides a modular architecture for Laravel applications, allowing you to organize your application into self-contained modules. This package is inspired by the concept of modular programming, making it easier to manage large applications.

v0.1.0-alpha.5(6mo ago)05MITPHPPHP ^8.3||^8.4CI passing

Since Oct 24Pushed 6mo agoCompare

[ Source](https://github.com/Williamug/modular)[ Packagist](https://packagist.org/packages/williamug/modular)[ Docs](https://github.com/williamug/modular)[ RSS](/packages/williamug-modular/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (5)Dependencies (9)Versions (9)Used By (0)

[![Latest Version on Packagist](https://camo.githubusercontent.com/58660e2f5bae4918a288c90048fcca58bdf4f13c94a5e8f207679ee14d96a2ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f77696c6c69616d75672f6d6f64756c61722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/williamug/modular/stats#major/all)[![run-tests](https://github.com/Williamug/modular/actions/workflows/run-tests.yml/badge.svg)](https://github.com/williamug/modular/actions/workflows/run-tests.yml)[![Made With](https://camo.githubusercontent.com/3994cc4f86157eaf13cc05557f8edaffcfad5ab2810bc3530a38f7eb5629f8ec/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d6164655f776974682d7068702d626c7565)](/docs/requirements/)[![License](https://camo.githubusercontent.com/58022077e3d2d3c225d5049a44a12bcdee75a1e8b7bfa28d1ef37e64e480bd38/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f77696c6c69616d75672f6d6f64756c61722e737667)](https://github.com/williamug/modular/blob/master/LICENSE.txt)

Modular
=======

[](#modular)

The Modular provides a modular architecture for Laravel applications, allowing you to organize your application into self-contained modules. This package is inspired by the concept of modular development, making it easier to manage large applications.

Features
--------

[](#features)

- Create, enable, disable, and delete modules.
- Run migrations, seeders, and publish assets for specific modules.
- Generate controllers, models, and migrations within modules.
- Dynamic module loading and management.
- Supports both API-only and full-stack Laravel projects.

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require williamug/modular
```

Run the installation command to set up the package:

```
php artisan modular:install
```

This will publish the configuration file and configure your frontend.

Available Commands
------------------

[](#available-commands)

### Creating a Module

[](#creating-a-module)

To create a new module, use the `module:create` command:

```
php artisan module:create Expense
```

This will create a `Expense` module in the `Modules` directory with the following structure:

```
Modules/
  Expense/
    Providers/
    Http/
    Models/
    Database/
    routes/
    resources/
    hook.php
    module.json

```

### Enabling and Disabling Modules

[](#enabling-and-disabling-modules)

Enable a module:

```
php artisan module:enable Expense
```

Disable a module:

```
php artisan module:disable Expense
```

### Deleting a Module

[](#deleting-a-module)

Delete a module:

```
php artisan module:delete Expense
```

### Running Migrations

[](#running-migrations)

Run migrations for a specific module:

```
php artisan module:migrate Expense
```

### Seeding Data

[](#seeding-data)

Seed data for a specific module:

```
php artisan module:seed Expense
```

### Publishing Assets

[](#publishing-assets)

Publish assets for a specific module:

```
php artisan module:publish Expense
```

### Generating Files

[](#generating-files)

Generate a controller within a module:

```
php artisan module:controller Expense PostController
```

Generate a model within a module:

```
php artisan module:model Expense Post --migration
```

Generate a migration within a module:

```
php artisan module:migration Expense create_posts_table
```

### Viewing Module Information

[](#viewing-module-information)

View detailed information about a module:

```
php artisan module:info Expense
```

Modular Navigation
------------------

[](#modular-navigation)

Modules can register navigation items by adding a `navigation` key to their `module.json`. You can use advanced features like icons, groupings, and permissions:

```
{
  "name": "Customers",
  "slug": "customers",
  "enabled": true,
  "navigation": [
    { "label": "Customers", "url": "/customers", "icon": "fa fa-users", "group": "CRM", "permission": "view-customers" },
    { "label": "Invoices", "url": "/invoices", "icon": "fa fa-file-invoice", "group": "Accounting", "permission": "view-invoices" }
  ]
}
```

### Helper Usage

[](#helper-usage)

In your sidebar Blade view, use the helper:

```

    @foreach(modular_navigation() as $item)
        @if(!$item['permission'] || auth()->user()?->can($item['permission']))

                @if($item['icon'])@endif
                {{ $item['label'] }}

        @endif
    @endforeach

```

#### Grouped Navigation Example

[](#grouped-navigation-example)

```
@php
    $groups = [];
    foreach(modular_navigation() as $item) {
        if(!$item['permission'] || auth()->user()?->can($item['permission'])) {
            $groups[$item['group'] ?? 'Other'][] = $item;
        }
    }
@endphp

    @foreach($groups as $group => $items)

            {{ $group }}

                @foreach($items as $item)

                        @if($item['icon'])@endif
                        {{ $item['label'] }}

                @endforeach

    @endforeach

```

### Blade Directive Usage

[](#blade-directive-usage)

Or use the Blade directive for a concise syntax:

```

    @modularNavigation

```

This will automatically render grouped navigation items from all enabled modules, showing icons and respecting permissions.

Modular Content Injection
-------------------------

[](#modular-content-injection)

Modules can inject custom content into parent layouts or pages (e.g., settings, dashboard widgets, or any slot) by adding a `settings`, `widgets`, or `content` key to their `module.json`:

```
{
  "name": "Customers",
  "slug": "customers",
  "enabled": true,
  "settings": [
    { "label": "Customer Settings", "view": "Modules/customer/resources/views/settings.blade.php", "icon": "fa fa-cog", "group": "CRM", "permission": "manage-customers" }
  ],
  "widgets": [
    { "label": "Customer Stats", "view": "Modules/customer/resources/views/widgets/stats.blade.php", "icon": "fa fa-chart-bar", "group": "CRM", "permission": "view-customers" }
  ],
  "content": [
    { "label": "Promo Banner", "view": "Modules/customer/resources/views/banner.blade.php", "icon": "fa fa-bullhorn", "group": "Marketing", "permission": "view-banner" }
  ]
}
```

### Helper Usage for Settings

[](#helper-usage-for-settings)

In your unified settings page:

```
@foreach(modular_settings() as $setting)
    @if(!$setting['permission'] || auth()->user()?->can($setting['permission']))
        @if($setting['icon'])@endif
        @include($setting['view'])
    @endif
@endforeach
```

Or use the Blade directive:

```
@modularSettings
```

### Helper Usage for Widgets

[](#helper-usage-for-widgets)

In your dashboard:

```
@foreach(modular_widgets() as $widget)
    @if(!$widget['permission'] || auth()->user()?->can($widget['permission']))
        @if($widget['icon'])@endif
        @include($widget['view'])
    @endif
@endforeach
```

Or use the Blade directive:

```
@modularWidgets
```

### Helper Usage for Generic Content

[](#helper-usage-for-generic-content)

In any parent layout or page:

```
@foreach(modular_content() as $content)
    @if(!$content['permission'] || auth()->user()?->can($content['permission']))
        @if($content['icon'])@endif
        @include($content['view'])
    @endif
@endforeach
```

Or use the Blade directive:

```
@modularContent
```

### Example Module Content (banner.blade.php)

[](#example-module-content-bannerbladephp)

```
{{-- Modules/customer/resources/views/banner.blade.php --}}

    Special Promotion!
    Get 20% off for all new customers this month.

```

Example Project
---------------

[](#example-project)

### Setting Up a Expense Module

[](#setting-up-a-expense-module)

1. **Create the Module**:

    ```
    php artisan module:make Expense
    ```
2. **Add Routes**: Edit `Modules/Expense/routes/web.php`:

    ```
