Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Full-page Components to Replace Laravel Controllers

Premium
3 min read

In general, there are two main ways to use Livewire:

  • You build the full project with Laravel Controllers, and use Livewire only for small dynamic elements on the pages
  • You use Livewire Components instead of Laravel Controllers, with so-called Full-Page Components

The choice is yours, it's a personal preference. In this lesson, let's see how to do it with full-page components.


Routes

Instead of mapping to the Controller in the Routes files, you just need to map directly to the Livewire component.

use App\Livewire\CreatePost;
 
Route::get('/posts/create', CreatePost::class);

Layout Files

Livewire uses Blade component as an application layout. So it needs to have a {{ $slot }} placeholder. By default, Livewire uses...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

S
shahidbahader ✓ Link copied!

If we are using resources/views/components/layouts/app.blade.php as global layout

then the config/livewire.php should read

'layout' => 'components/layouts.app',
PK
Povilas Korop ✓ Link copied!

Yes, but you don't necessarily have to have this config, as it is the DEFAULT value.

Quote from the docs: "By default, Livewire will automatically look for a layout file named: resources/views/components/layouts/app.blade.php"

RA
Richard A. Hoyle ✓ Link copied!

I’m sorry but in my version Laravel/framework 10.35.0 and laravel/breeze 1.26.2 with livewire/livewire 3.3.0 Out of the box Laravel puts the layouts in its one directory. Resources/views/layouts/app.blade.php

Not in resources/views/components/layouts/app.blade.php.

However if you create a new layout using the php artisan make:component AppLayout command it will put the file in the components directory but not in a layouts subdirectory in less you tell it to.

PS. under the latest “Laravel new” you can get Breeze to setup using livewire 3 and the old problems with Alpin.js has been taken care of.

LS
Luis Scura ✓ Link copied!

I have the same issue! My app layout is in 'resources/views/layouts/app.blade.php' - What was your workaround for this, Richard? Did you just update the livewire config file to use layouts.app.blade?

N
Nerijus ✓ Link copied!

Please read the docs about layouts https://livewire.laravel.com/docs/components#layout-files

LS
Luis Scura ✓ Link copied!

What is the differnce between mapping directly to the Livewire component like the above example and doing this?

Route::view('/posts/create', 'admin.posts.create');

And then in the 'admin.posts.create' render the posts component?

'<x-app-layout>'<livewire:create-post /></x-app-layout>

The only thing that I can see is that we avoid having an extra file that only renders the component

N
Nerijus ✓ Link copied!

Using directly component it's called a full page component. Not everyone likes them and not always it's needed. Livewire can be used just in some parts of the page, for example only for comments on the page, or for like/dislike button. But it's more a personal preference.

LS
Luis Scura ✓ Link copied!

Got it! And in case that I want to use the index method for the post, is it possible to do it with livewire if we are using it already? I want to avoid having a PostController only for the index method

N
Nerijus ✓ Link copied!

You can fully use full page Livewire components. In the routes instead of calling controller call the Livewire component. In your case you can name it for example PostsIndex.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.