Skip to content

Helpers and Blade directive

This page lists the global functions in src/helpers.php, the @module Blade directive, and the php artisan about entry. Composer loads src/helpers.php via autoload.files.

Each function is defined only when no function of that name exists yet.

module(string $name, bool $instance = false): bool|Nwidart\Modules\Module

Returns the status of a module, or the module.

Parameter Description
$name Module name. Matched like Module::find(): case-insensitive, and kebab-case and snake_case names find the studly-case module.
$instance false: return the status. true: return the Module instance.

Returns

Case Value
$instance is false true when the module is enabled, otherwise false.
$instance is true The Module.
The module does not exist false, whatever $instance is. The function catches ModuleNotFoundException.
if (module('Blog')) {
// Blog exists and is enabled.
}
$blog = module('Blog', true); // Module|false
module_path(string $name, string $path = ''): string

Returns the folder of a module, or a path inside it.

Parameter Description
$name Module name, matched like module().
$path Optional path inside the module. It is joined to the module folder with DIRECTORY_SEPARATOR.

Returns the module folder without a trailing slash, followed by the separator and $path when $path is not empty.

Throws ModuleNotFoundException when the module does not exist.

module_path('Blog'); // /var/www/app/Modules/Blog
module_path('Blog', 'routes/web.php'); // /var/www/app/Modules/Blog/routes/web.php
module_vite(string $module, string $asset, ?string $hotFilePath = null): Illuminate\Foundation\Vite

Returns a copy of the application Vite instance, set up for the build of one module. The shared instance that @vite uses stays unchanged.

Parameter Description
$module Build directory, passed to useBuildDirectory(). The generated layout passes build-<lowername>.
$asset The entry point, passed to withEntryPoints().
$hotFilePath Hot file passed to useHotFile(). Defaults to storage_path('vite.hot'). Generated layouts pass storage_path('vite-<lowername>.hot'), matching hotFile in the module vite.config.js.

Returns the configured Vite instance. Printing it in a Blade view ({{ module_vite(...) }}) outputs the tags.

{{ module_vite('build-blog', 'resources/assets/js/app.js') }}
{{ module_vite('build-blog', 'resources/assets/js/app.js', storage_path('vite-blog.hot')) }}

The first call reads storage/vite.hot. The second reads the hot file that npm run dev writes in a module generated by the current stubs. See Build a module on its own.

The build-<lowername> directory holds the standalone build of a module. A build from the application root with vite-module-loader.js writes to public/build, which @vite reads.

Deprecated Lumen compatibility shim, removed in version 14. Laravel defines config_path() itself.

config_path(string $path = ''): string

Returns the application config folder, or a path inside it. Defined only when no config_path() function exists yet.

Parameter Description
$path Optional path inside the config folder. Joined with DIRECTORY_SEPARATOR as given.

Returns <base path>/config, followed by the separator and $path when $path is not empty.

config_path('blog.php'); // /var/www/app/config/blog.php

Deprecated Lumen compatibility shim, removed in version 14. Laravel defines public_path() itself.

public_path(string $path = ''): string

Returns the application public folder, or a path inside it. Defined only when no public_path() function exists yet.

Parameter Description
$path Optional path inside the public folder. Leading separators are stripped before joining with DIRECTORY_SEPARATOR.

Returns the path.public binding, followed by the separator and $path when $path is not empty.

public_path('modules/blog'); // /var/www/app/public/modules/blog

LaravelModulesServiceProvider::boot() registers the directive via callAfterResolving('blade.compiler') using $blade->if('module', ...), so booting does not eagerly resolve Blade. The condition is module($name): true when the module exists and is enabled. Supports @else, @elseif, and @endmodule.

@module('Blog')
<a href="{{ route('blog.index') }}">Blog</a>
@endmodule

When running in the console, LaravelModulesServiceProvider::boot() adds a section to the about command.

Section Entry Value
Laravel-Modules Version The installed version of nwidart/laravel-modules, from Composer.
Terminal window
php artisan about