Last active
January 16, 2025 11:34
-
-
Save yepbro/1a0764b849216e4a490e21c590abb17a to your computer and use it in GitHub Desktop.
Moonshine & Laravel Modules (nwidart)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* How to integrate Moonshine Admin Panel and Laravel (Nwidart) modules? | |
* | |
* I would like to be able to store ModelResources in the module folder and then add them to the admin | |
* panel and menu from there. | |
* | |
* Moonshine 3, Laravel Modules 11, PHP 8.4 | |
* | |
* @link https://moonshine-laravel.com/ | |
* @link https://laravelmodules.com/ | |
*/ | |
// Modules/UpdateLoggerBot/app/MoonShine/Resources/UpdateResource.php | |
namespace Modules\UpdateLoggerBot\MoonShine\Resources; | |
use MoonShine\Laravel\Resources\ModelResource; | |
class UpdateResource extends ModelResource | |
{ | |
// | |
} | |
// Modules/UpdateLoggerBot/app/MoonShine/menu.php | |
use Modules\UpdateLoggerBot\MoonShine\Resources\UpdateResource; | |
use MoonShine\MenuManager\MenuGroup; | |
use MoonShine\MenuManager\MenuItem; | |
return [ | |
MenuGroup::make(static fn() => 'Update Logger Bot', [ | |
MenuItem::make('Updates', UpdateResource::class), | |
]), | |
]; | |
// Modules/UpdateLoggerBot/app/Providers/UpdateLoggerBotServiceProvider.php | |
namespace Modules\UpdateLoggerBot\Providers; | |
use Illuminate\Support\ServiceProvider; | |
class UpdateLoggerBotServiceProvider extends ServiceProvider | |
{ | |
// ... | |
public function register(): void | |
{ | |
$this->app->register(EventServiceProvider::class); | |
$this->app->register(RouteServiceProvider::class); | |
$this->app->register(MoonShineServiceProvider::class); // add line | |
} | |
// ... | |
} | |
// Modules/UpdateLoggerBot/app/Providers/MoonShineServiceProvider.php | |
namespace Modules\UpdateLoggerBot\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use Modules\UpdateLoggerBot\MoonShine\Resources\UpdateResource; | |
use MoonShine\Contracts\Core\DependencyInjection\ConfiguratorContract; | |
use MoonShine\Contracts\Core\DependencyInjection\CoreContract; | |
class MoonShineServiceProvider extends ServiceProvider | |
{ | |
public function boot(CoreContract $core, ConfiguratorContract $config): void | |
{ | |
$core | |
->resources([ | |
UpdateResource::class, | |
]); | |
} | |
} | |
// app/Services/MoonShineModulesMenuFactory.php | |
use Nwidart\Modules\Facades\Module; | |
readonly class MoonShineModulesMenuFactory | |
{ | |
public function __construct(public array $modules) | |
{ | |
// | |
} | |
public function getItems(): array | |
{ | |
$items = []; | |
/** @var \Nwidart\Modules\Module $module */ | |
foreach ($this->modules as $module) { | |
$menuFilePath = $this->getModuleMenuFilePath($module->getName()); | |
if (file_exists($menuFilePath)) { | |
$items = [ | |
...$items, | |
...include $menuFilePath, | |
]; | |
} | |
} | |
return $items; | |
} | |
protected function getModuleMenuFilePath(string $moduleName): string | |
{ | |
return $this->getModulePath($moduleName) . '/app/MoonShine/menu.php'; | |
} | |
protected function getModulePath(string $moduleName): string | |
{ | |
return $this->getBaseModulePath() . '/' . $moduleName; | |
} | |
protected function getBaseModulePath(): string | |
{ | |
return Module::getPath(); | |
} | |
} | |
// app/MoonShine/Layouts/MoonShineLayout.php | |
namespace App\MoonShine\Layouts; | |
use App\Services\MoonShineModulesMenuFactory; | |
use MoonShine\Laravel\Layouts\AppLayout; | |
use Nwidart\Modules\Facades\Module; | |
final class MoonShineLayout extends AppLayout | |
{ | |
// ... | |
protected function menu(): array | |
{ | |
return [ | |
...parent::menu(), | |
...new MoonShineModulesMenuFactory(Module::all())->getItems(), | |
]; | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment