This episode is for members only

Sign up to access "Learn Inertia" right now.

Get started
Already a member? Sign in to continue
Playing
03. Creating pages

Transcript

00:00
So let's dive straight in and go through the workflow of creating a new root, a controller, and a page out in Inertia. So we're going to start over in our root file, and at the moment we don't have any controllers in here. There's nothing to stop you doing this, but typically within an
00:17
application we would build out these with controllers. So before we define the root, let's create out a controller in here, and let's call this comment index controller. So this is going to be the homepage for the comment system that we're building. Let's go ahead and create that root out now. So that's going to be slash comments, and we're going to reference that
00:37
comment index controller in here, and we can go ahead and give this a name as well. So let's say comments dot index. So if we open up the comment index controller, what are we doing in here? Well again, we're working within Laravel, nothing has changed here, so we can go ahead and create our constructor to give our middleware in here if we want to. Pretty much anything that we would
01:00
normally do, but we're going to go ahead and create a single invoke magic method, which we'll just call when this page gets hit, and we're going to go ahead and return a view component, which will be our page for this. Now if we head over to web roots, you can see that these are using this inertia class here to statically call this render method. Now I don't typically do this. If you do
01:21
not want to do this, you can actually use the inertia helper. I much prefer using the inertia helper to do this. Works in exactly the same way, I just find it a little bit cleaner and a little bit more convenient than pulling the class into here. So we're going to say inertia render, and we're going to choose the name of our page. So we know that our pages live over in this pages folder,
01:42
so we're going to go ahead and create a sub directory in here called comments, and I'm going to create index.view. So let's go ahead and say comments slash index. So we can go any level deep in here, it doesn't really matter how we do this. Remember this will always be prefixed with pages as we saw earlier over in our app.js file here when this gets resolved. So now that we've created
02:06
a root and we're rendering out this page, let's go ahead and just fill this in with some data. So we'll just keep this really simple and just create out a div here and just say comment index. Let's head over to the browser and see what this looks like. So if we hit this page, there we go. As easy as that, we have now created out our first inertia page. Now we're going to style this up to
02:28
make it look a little bit nicer, so we can actually go ahead and steal the entire dashboard component here to just get this to work. So I'm going to go ahead and just paste this into the index file, and I'm going to change this over to comment index. I'm going to go ahead and slightly reduce the width of the container here, and we're going to speak about this head element very, very shortly.
02:52
So let's go over, and there we go. We now have a comment index page. Now let's go ahead and just switch out the title here to comments, and let's switch this out as well to comments, and there we go. That looks a lot better. Let's just cancel off the npm run dev and rerun this just to, there we go, get that sizing right. So that's our first page created with inertia. Let's very quickly talk
03:18
about this authenticated layout here, which is pretty much just a view component with a default slot in. If we just have a look, we've got the header slot up here, which is where this slots into, and we've also got a default slot here, where all of our other content down here slots into. So this is nothing inertia specific, it's just how Laravel Breeze is set up. We're not going
03:43
to dive, like I said, too much into Laravel Breeze, but I thought I'd explain this just in case. Okay, so our first page has now been created. That is the new workflow you're going to have to get used to when you're working with inertia. Instead of rendering out a blade file, you just create a controller and instead render out a view component.
28 episodes2 hrs 20 mins

Overview

Single-page applications are great, but they come with the challenge of maintaining two separate projects — the client and the API.

Inertia serves as the solution, seamlessly connecting these two components and enabling you to build everything in one application (a monolith) while achieving the same outcome. The best part? You won't have to create an API.

To be honest, when I first started working with Inertia, I was a little confused about how everything worked. If that sounds familiar, then this course is made for you. I'll guide you through everything you need to know to kickstart your journey in building single-page applications using Laravel, Inertia, and Vue.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!