PHP code example of mehediishere / laravel-modular

1. Go to this page and download the library: Download mehediishere/laravel-modular library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mehediishere / laravel-modular example snippets


'enabled' => [
    'POS',
    'Ecommerce',
    'Account',
    'Payroll',
],


// Modules/Account/config/sidebar.php

return [
    'group_id' => 'finance',      // shared with Payroll — they merge into one dropdown
    'group'    => 'Finance',      // dropdown label (use the same across sharing modules)
    'icon'     => 'bar-chart',    // group header icon
    'order'    => 20,             // sidebar position (lower = higher)

    'items' => [
        [
            'label'      => 'Chart of Accounts',
            'route'      => 'account.coa.index',
            'icon'       => 'list',
            'order'      => 1,
            'permission' => 'account.coa.view',  // Laravel Gate ability; empty = all users
        ],
        [
            'label'      => 'Journal Entries',
            'route'      => 'account.journal.index',
            'icon'       => 'book',
            'order'      => 2,
            'permission' => 'account.journal.view',
        ],
    ],
];


// Modules/Payroll/config/sidebar.php

return [
    'group_id' => 'finance',      // same group_id → items merge with Account above
    'group'    => 'Finance',
    'icon'     => 'bar-chart',
    'order'    => 20,

    'items' => [
        [
            'label'      => 'Payroll Runs',
            'route'      => 'payroll.runs.index',
            'icon'       => 'dollar-sign',
            'order'      => 3,
            'permission' => 'payroll.runs.view',
        ],
        [
            'label'      => 'Tax Reports',
            'route'      => 'payroll.tax.index',
            'icon'       => 'file-text',
            'order'      => 4,
            'permission' => 'payroll.tax.view',
        ],
    ],
];

return [
    'group_id' => 'pos',          // unique — no other module uses this
    'group'    => 'Point of Sale',
    'icon'     => 'shopping-cart',
    'order'    => 10,
    'items'    => [...],
];

[
    [
        'group_id' => 'finance',
        'group'    => 'Finance',
        'icon'     => 'bar-chart',
        'order'    => 20,
        'items'    => [
            ['label' => 'Chart of Accounts', 'route' => 'account.coa.index',     'icon' => 'list',        'order' => 1, 'permission' => 'account.coa.view'],
            ['label' => 'Journal Entries',   'route' => 'account.journal.index', 'icon' => 'book',        'order' => 2, 'permission' => 'account.journal.view'],
            ['label' => 'Payroll Runs',      'route' => 'payroll.runs.index',    'icon' => 'dollar-sign', 'order' => 3, 'permission' => 'payroll.runs.view'],
            ['label' => 'Tax Reports',       'route' => 'payroll.tax.index',     'icon' => 'file-text',   'order' => 4, 'permission' => 'payroll.tax.view'],
        ],
    ],
    [
        'group_id' => 'pos',
        'group'    => 'Point of Sale',
        'icon'     => 'shopping-cart',
        'order'    => 10,
        'items'    => [...],
    ],
]

use Mehediishere\LaravelModular\Services\SidebarManager;

// Flush for the current user (call after role/permission changes)
app(SidebarManager::class)->flush();

// Flush for a specific user
app(SidebarManager::class)->flush($userId);

// Flush for all users
app(SidebarManager::class)->flushAll();



namespace Modules\Account\app\Providers;

use Mehediishere\LaravelModular\BaseModuleServiceProvider;

class AccountServiceProvider extends BaseModuleServiceProvider
{
    protected string $moduleName = 'Account';

    protected array $bindings = [
        \Modules\Account\app\Contracts\LedgerRepositoryInterface::class =>
        \Modules\Account\app\Repositories\LedgerRepository::class,
    ];

    protected array $singletons = [
        'account.currency' => \Modules\Account\app\Services\CurrencyService::class,
    ];

    protected array $commands = [
        \Modules\Account\app\Console\Commands\ReconcileCommand::class,
    ];
}

// Modules/POS/config/config.php
return ['per_page' => 25, 'currency' => 'BDT'];

// Anywhere in the app
config('pos.per_page');   // 25
config('pos.currency');   // BDT

// Fire from Order module
event(new \Modules\Order\app\Events\OrderPlaced($order));

// Listen in Inventory module's ServiceProvider boot()
\Illuminate\Support\Facades\Event::listen(
    \Modules\Order\app\Events\OrderPlaced::class,
    \Modules\Inventory\app\Listeners\ReserveStock::class,
);

// Define interface in the consuming module
// Modules/Order/app/Contracts/ProductStockInterface.php

// Implement in Product module
// Modules/Product/app/Services/ProductStockService.php

// Bind in Product's ServiceProvider
protected array $bindings = [
    \Modules\Order\app\Contracts\ProductStockInterface::class =>
    \Modules\Product\app\Services\ProductStockService::class,
];

return [
    'enabled' => ['POS', 'Ecommerce', 'Account', 'Payroll'],
    'path'    => base_path('Modules'),
];

return [
    'sidebar' => [
        'cache'     => true,    // env: MODULAR_SIDEBAR_CACHE
        'cache_ttl' => 3600,    // env: MODULAR_SIDEBAR_TTL (seconds)
    ],
];
bash
php artisan vendor:publish --tag=modular-config
bash
php artisan module:make POS
php artisan module:make Ecommerce
php artisan module:make Account
php artisan module:make Payroll
bash
composer dump-autoload
php artisan migrate
blade
@php
    $sidebarGroups = app(\Mehediishere\LaravelModular\Services\SidebarManager::class)->build();
@endphp

@foreach($sidebarGroups as $group)
    <div class="sidebar-group" data-group="{{ $group['group_id'] }}">

        <button class="sidebar-group-toggle">
            {{ $group['group'] }}
        </button>

        <div id="sidebar-group-{{ $group['group_id'] }}">
            @foreach($group['items'] as $item)
                <a href="{{ route($item['route']) }}"
                   class="{{ request()->routeIs($item['route'] . '*') ? 'active' : '' }}">
                    {{ $item['label'] }}
                </a>
            @endforeach
        </div>

    </div>
@endforeach
bash
php artisan vendor:publish --tag=modular-views
bash
php artisan vendor:publish --tag=modular-stubs
xml
<testsuites>
    <testsuite name="Application">
        <directory>tests/Feature</directory>
        <directory>tests/Unit</directory>
    </testsuite>
    <testsuite name="Account">
        <directory>Modules/Account/tests</directory>
    </testsuite>
    <testsuite name="POS">
        <directory>Modules/POS/tests</directory>
    </testsuite>
</testsuites>
bash
php artisan test --testsuite=Account