Mostafa Norzade
Mostafa Norzade

Reputation: 1766

laravel - livewire Full-Page Components

I use laravel 8.

I define protected namespace in RouteServiceProvider:

protected $namespace = 'App\Http\Controllers';

then use livewire with the below route:

Route::get('/xxx' , App\Http\Livewire\Counter::class);

but I have the below error :

Invalid route action: [App\Http\Controllers\App\Http\Livewire\Counter].

Notice: this adds App\Http\Controllers first of my action !!! if remove protected $namespace all is OK. But I do not want to delete it.

Is there a way for me to have both ‍controller protected namespace (for controller namespace) and a Route::get for Full-Page Components?

Upvotes: 5

Views: 14189

Answers (5)

Anand Sri
Anand Sri

Reputation: 31

Removed: Route::livewire()

Livewire 1. x allowed you to register a component with a route for the entire page using the Route::livewire() method. Livewire 2.0 now allows you to pass Livewire components directly into routes using the standard Route::get() method and the fully qualified namespace.

Before

Route::livewire('/post', 'show-posts');

After

Route::get('/post', \App\Http\Livewire\ShowPosts::class);

Upvotes: 2

kodfire
kodfire

Reputation: 1783

In order not to modify the RouteServiceProvider.php file for its namespace, move app\Http\Livewire directory to app\Http\Controllers\Livewire.

Change this

namespace App\Http\Livewire;

use Livewire\Component;

class Counter extends Component
{
    //
}

to this

namespace App\Http\Controllers\Livewire; // <---- added Controllers

use Livewire\Component;

class Counter extends Component
{
    //
}

Modify config/livewire file from this

'class_namespace' => 'App\\Http\\Livewire',

to this

'class_namespace' => 'App\\Http\\Controllers\\Livewire', // <---- Controllers added
  • Then remove what's inside bootstrap\cache.
  • Then run composer dumpautoload.

After that any new livewire you make with php artisan livewire:make, should be modified. You should change namespace App\Http\Livewire to namespace App\Http\Controllers\Livewire.

Upvotes: 4

J Hanlon
J Hanlon

Reputation: 331

For those that are still looking for a solution to this. I ran across this problem when upgrading an existing install from Laravel 7 to 8 and added some new Livewire components.

You should be able to invoke the class directly in the Route like so,

Route::get('/blog', [\App\Http\Livewire\Pages\ShowPosts::class, '__invoke'])->name('blogs');

This should allow you to use Laravels regular web routing.

Then in your Livewire component you can extend the layout like so,

class ShowPosts extends Component{
   ...
   public function render(){
       return view('livewire.show-posts')
        ->layout('layouts.base');
   } 
 ...}

Rendering Components

The other option is to remove the namespacing from Routes as discussed in the link below.

Upgrading Livewire Routes.

Upvotes: 15

Mattias
Mattias

Reputation: 126

Unfortunately, you won't be able to use protected $namespace together with Livewire. You'll have to comment it back out and update all your routes to the laravel 8 way: https://laravel.com/docs/8.x/releases#routing-namespace-updates

Because livewire components don't live under App\Http\Controllers, using $namespace will never work.

As far as I understand it. Route::livewire existed in Livewire 1.x because of this issue in Laravel 7. But with Laravel 8 it could be removed in favor of the new way.

Upvotes: 5

You could try adding your livewire component at the beggining of web.php:

use App\Http\Livewire\Counter;

And in your route:

Route::get('/xxx', Counter::class);

Upvotes: -1

Related Questions