mhmiton / laravel-modules-livewire

Using Laravel Livewire in Laravel Modules package with automatically registered livewire components for every modules.
MIT License
184 stars 35 forks source link

Livewire Component Problem within Laravel Modules #37

Closed manojdhrma closed 9 months ago

manojdhrma commented 9 months ago

Laravel Framework 10.37.3 Livewire v3.3.0 nwidart laravel-Modules 10.0.4 Laravel-modules-livewire v2.2.0

The following issues happened when trying to use.

  1. After creating a Livewire component.. the route cannot access it. It says Target Class does not exist. Actually this didn't happen before. But, it works by removing the default $moduleNamespace from the RouteServiceProvider of the "module".

  2. Then, BadMethodCallException comes.

Error::

Method Modules{module_name}\Livewire\HomeController::index does not exist.

Before it actually works with livewire default render() method but now it requires index() method by default. It works by changing the livewire render() method to index().

  1. Then, another "ErrorException" comes when try to access the livewire component property within blade.

Error::

Undefined variable $count

Livewire Component

namespace Modules\Test\Livewire;

use Livewire\Component;

class HomeController extends Component
{
    public $count = 1;

    public function index()
    {
        return view('test::livewire.home-controller');
    }
}

Blade file

<div>

    <h3>The <code>HomeController</code> livewire component is loaded from the <code>Test</code> module.</h3>

    {{ $count }}

</div>

I am able to re-create these issues using fresh installation.
Is there something required to be modified manually to resolve these issues ?

mhmiton commented 9 months ago

@manojdhrma Did you try the full page component? Also, can you share with me your dummy source code, please? So, I will test this issue properly on my end.

manojdhrma commented 9 months ago

@mhmiton Yes. Actually this is a full page component.

It's easier to create a repo, so I created one specifically for this purpose so you can get the full source code https://github.com/manojdhrma/Laravel-modules-livewire--TestApp-

mhmiton commented 9 months ago

Are you sure that the resource route is supported by Livewire? I think the resource route is not supported on the livewire.

On the livewire full-page component, you need to declare the render method. So, if you using the render method, the route should be -

Route::get('test', HomeController::class)->name('test.index');

For the resource route things, you can use this, according the the livewire docs - https://livewire.laravel.com/docs/components#full-page-components

Route::get('test', TestIndex::class)->name('test.index');
Route::get('test/create', TestCreate::class)->name('test.create');
Route::get('test/edit/{id}', TestEdit::class)->name('test.edit');
manojdhrma commented 9 months ago

Yes. I check it and you are correct.! Livewire does not support the resource route.

This is the primary problem. As you explained, I used the get route and it started working smoothly. And it seems, Laravel-modules only accepting resource route to the default controllers.

Thank you for clearing up the confusion.