ganyicz / volt-namespace
Adds support for namespaces in Volt
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- livewire/livewire: ^3.6
- livewire/volt: ^1.7
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
README
This is a (working) proof of concept.
Usage
Install the package via composer:
composer require ganyicz/volt-namespace
Mount a new namespace in your VoltServiceProvider:
public function boot(): void
{
Volt::mount([
config('livewire.view_path', resource_path('views/livewire')),
resource_path('views/pages'),
]);
+ VoltNamespace::mount('admin', resource_path('views/admin/livewire'));
}
Use the namespace when rendering a volt component:
<livewire:admin:search-users />
The above will load a volt component from the following path:
/resources/views/admin/livewire/search-users.blade.php
Why
Using namespaces allows for a cleaner directory structure when maintaining multiple frontends in one repo. With Blade, we can already register a namespace for anonymous components like this:
Blade::anonymousComponentPath(resource_path('views/admin/components'), 'admin');
Combined, this allows us to have the following structure that keeps all related views together, including livewire, blade components and regular views:
resources
└─ views
└─ admin
└─ auth
└─ pages
└─ livewire
└─ components
Whereas without namespaces, we would have to use subdirectories, which fragments the structure:
resources
└─ views
└─ admin
└─ auth
└─ pages
└─ components
└─ admin
└─ livewire
└─ admin
Disclaimer
This packge has only been tested in a single project with a specific setup and alters core functionalities of Laravel and Volt. This could have unintended consequences in your app. Use at your own risk.
If this functionality were to be added to the core of Volt, this could be achieved with a lot less hackery.
How it works
-
It binds a custom view finder that extends the
Illuminate\View\FileViewFinder
. This is required to load the correct view in the namespaced components.Volt uses a custom
volt-livewire
view namespace that contains all mounted paths and when a component is rendered it will try to load the following view for<livewire:search-users />
:volt-livewire::search-users
The first part is the custom view namespace and the second part is the component name that Volt will look for in all mounted paths. In our case the name of the component would contain the "sub-namespace" like this:
volt-livewire::admin:search-users
The package simply moves the primary namespace separator like this:
volt-livewire:admin::search-users
Where
volt-livewire:admin
is a new view namespace that is registered by the pacakge when a volt namespace is mounted. This namespace contains the paths defined by the user inVoltNamespace::mount()
method. -
It replaces the
Livewire\Volt\Precompilers\ExtractTemplate
precompiler to include the registered namespaced directories in the pre-compilation process.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.