Skip to content

Commit

Permalink
feat: Added dot-notation to find or create of the components
Browse files Browse the repository at this point in the history
  • Loading branch information
joserick committed Sep 17, 2024
1 parent 96eea3e commit 44a1723
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 8,510 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
composer.lock
87 changes: 77 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@


![laravel_livewire_discover.jpg](https://joserick.com/livewire_discover.jpg)
# Laravel Livewire Discover
![Packagist Downloads](https://img.shields.io/packagist/dt/joserick/laravel-livewire-discover?color=blue) ![GitHub License](https://img.shields.io/github/license/joserick/laravel-livewire-discover) ![GitHub Release](https://img.shields.io/github/v/release/joserick/laravel-livewire-discover?color=2da711)
> Notice: [Migration to v1.0](https://github.com/joserick/laravel-livewire-discover#migration-to-v1) || [I need previous version (v0.3)](https://github.com/joserick/laravel-livewire-discover#i-need-for-v0.3.2)
Automatically discover and load/register multiple/different class namespaces for Livewire components.

Expand All @@ -19,39 +21,104 @@ Add to *AppServiceProvider.php*
public function boot(): void
{
// Load multiples namespace for Livewire components.
Livewire::discover('Namespaces\\Livewire', 'my-components');
Livewire::discover('User\\Repository\\Livewire', 'new-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](https://laravel.com/docs/blade#clipText-53)
``` php
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/laravel-livewire-discover.php'*
``` bash
# Publish the config
php artisan vendor:publish --tag livewire-discover-config
```
``` php
// Load the namespace to Livewire components.
'class_namespaces' => [
// 'prefix' => 'namespace\\package',
// 'prefix' => 'class\\namespace',
'my-components' => 'Namespaces\\Livewire',
'new-components' => 'User\\Repository\\Livewire',
],
```
## Use
Call Livewire Components:
``` html
<!-- Call Livewire Components. -->
<livewire:my-components-devices /> <!-- Class: Namespace\Livewire\Devices; -->
<livewire:new-components-devices-table /> <!-- Class: User\Repository\Livewire\DevicesTable; -->
<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
Or use form Routes:
``` php
// 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
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
```bash
php artisan livewire:layout
```
## Config path for component creation
If you would like to automatically create components in a specific directory based on the prefix, you can configure it in the following way:
``` php
public function boot(): void
{
Livewire::discover('my-components', ['Namespaces\\Livewire', '/path/livewire']);
}
```
## Creating components
You can create the files automatically using the following Artisan command. In the process it will ask you for the suffix to use, don't forget to put the path in the prefix settings.
```bash
php artisan make:livewire-discover RegisterAdmin
```
If you prefer kebab-cased names, you can use them as well:
```bash
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:
```bash
php artisan make:livewire-discover Auth\\RegisterAdmin
php artisan make:livewire-discover auth.register-admin
```
## Migration to v1
Replace `Livewire::discover` for `Livewire::componentNamespace` since the attributes in v1 are reversed but the `componentNamespace` function maintains the structure of previous versions.
``` php
Livewire::discover('Namespaces\\Livewire', 'my-components');
```
to
``` php
Livewire::componentNamespace('Namespaces\\Livewire', 'my-components');
```
or in any case to maintain the use of the `discover()` function you can invert the parameters.

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
``` html
<livewire:components-devices />
```
to
``` html
<livewire:components.devices />
```
## I need for v0.3.2
```
composer require joserick/laravel-livewire-discover:0.3.2
```

## License

The GNU Public License (GPLv3). Please see [License File](https://github.com/joserick/laravel-livewire-discover/blob/master/LICENSE) for more information.
Loading

0 comments on commit 44a1723

Please sign in to comment.