Notice: Migration to v1.0 || I need previous version (v0.3)
Automatically discover and load/register multiple/different class namespaces for Livewire components.
You can add the package via composer:
composer require joserick/laravel-livewire-discover
And then install the package with artisan:
php artisan livewire-discover:install
Add to LivewireDiscoverServiceProvider.php
public function boot(): void
{
// Load multiples namespace for Livewire components.
Livewire::discovers([
'my-components' => 'Namespaces\\Livewire',
'new-components' => 'User\\Repository\\Livewire',
]);
// Or individually
Livewire::discover('my-components', 'Namespaces\\Livewire');
Livewire::discover('new-components', 'User\\Repository\\Livewire');
}
Or if you like, use "componentNamespace" function as in Blade Templates
public function boot(): void
{
// Load multiples namespace for Livewire components.
Livewire::componentNamespace('Namespaces\\Livewire', 'my-components');
Livewire::componentNamespace('User\\Repository\\Livewire', 'new-components');
// ...
}
Or use the config: 'config/livewire-discover.php'
// Load the namespace to Livewire components.
'class_namespaces' => [
// 'prefix' => 'class\\namespace',
'my-components' => 'Namespaces\\Livewire',
'new-components' => 'User\\Repository\\Livewire',
],
Call Livewire Components:
<livewire:my-components.devices /> <!-- Class: Namespace\Livewire\Devices; -->
<livewire:new-components.auth.login /> <!-- Class: User\Repository\Livewire\Auth\Login; -->
<livewire:new-components.auth.register-admin /> <!-- Class: User\Repository\Livewire\Auth\RegisterAdmin; -->
Or use form Routes:
// Load Livewire Component from Route
use Namespaces\Livewire\Devices;
use User\Repository\Livewire\DevicesTable;
Route::get('/devices', Devices::class); // resolve name my-components.devices
Route::get('/devices_table', DevicesTable::class); // resolve name new-components.devices-table
"Obviously" you need to install the "layout" first for the Routes
php artisan livewire:layout
If you would like to automatically create components in a specific directory based on the prefix, you can configure it in the following way:
Livewire::discover(
'my-components',
'Namespaces\\Livewire',
app_path('/Path/Livewire') // <-- Components directory path
);
Now when you create the component it will be created in the path you have specified.
Do you also want it to create the view
at a specific route? Just add the view route and it will automatically create it, simple as that:
Livewire::discover(
'my-components',
'Namespaces\\Livewire',
app_path('/path/Livewire'),
resource_path('/views/path/livewire'), <-- Components views path
);
Remember that these are examples, you can specify any path within your project and it will create it.
You can create the files automatically using the following Artisan command. In the process it will ask you for the prefix to use, don't forget to put the path in the prefix settings.
php artisan make:livewire-discover RegisterAdmin
If you prefer kebab-cased names, you can use them as well:
php artisan make:livewire-discover register-admin
You may use namespace syntax or dot-notation to create your components in sub-directories. For example, the following commands will create a RegisterAdmin
component in the Auth
sub-directory:
php artisan make:livewire-discover Auth\\RegisterAdmin
php artisan make:livewire-discover auth.register-admin
Also if you don't want it to constantly ask you which prefix to select you can pass it directly with the --prefix
attribute
Replace Livewire::discover
for Livewire::componentNamespace
since the attributes in v1 are reversed but the componentNamespace
function maintains the structure of previous versions.
Livewire::discover('Namespaces\\Livewire', 'my-components');
to
Livewire::componentNamespace('Namespaces\\Livewire', 'my-components');
or in any case to maintain the use of the discover()
function you can invert the parameters.
The configuration file name has changed from laravel-livewire-discover.php
to simply livewire-discover-php
Change in concatenation of prefixes with class name, previously it was concatenated using the "-" notation, now the dot-notation is used, so it must be changed in all calls to Livewire-Discover components
<livewire:components-devices />
to
<livewire:components.devices />
composer require joserick/laravel-livewire-discover:0.3.2
The GNU Public License (GPLv3). Please see License File for more information.