What to expect in the next generation of Laravel Forge. Read the blog post
Rewatch this lesson

Next video in… 10

Deploying your app

Your first route

Getting Started with Laravel

Your first route
Create your first Laravel route and view. Learn how routes connect URLs to code and build a basic homepage with Blade templates.

Alright, let's dive into some actual code! When you first opened up that URL—that's http://127.0.0.1:8000 on port 8000 where your development server is running—you were greeted by Laravel's friendly default welcome page (at least for Laravel 12 at the time of this recording).

But how does Laravel know what to show you? That's thanks to the route in routes/web.php. Let's take a look at what's happening:

1Route::get('/', function () {
2 return view('welcome');
3});

This little bit of code tells Laravel: "When someone visits the homepage (that slash route), show them the welcome view." That's what this Route facade is doing—it's defining what happens when a user goes to the root directory of your application.

And since I'm using VS Code with the official Laravel extension, I can actually option-click (or ctrl-click on Windows) to follow that welcome view to resources/views/welcome.blade.php. That's where this view lives, and that's what you're seeing in your browser.

Step 1: Change the Rendered View

Let's take control! Update the route to render a view called home instead:

1Route::get('/', function () {
2 return view('home');
3});

If you refresh your browser now, you'll get an error—Laravel can't find the home view yet.

If you're using the Laravel VS Code extension, not only will it show you this error right in the editor (the view home is not found), but it also gives you an easy way to fix this—it can create the missing home.blade.php file for you. Handy!

Step 2: Create the Home View

So what can we do about this missing view? Let's create it! In the resources/views directory, create a new file named home.blade.php.

Now we won't get an error anymore, but if you refresh your browser, it's just a blank page. So let's actually build something! I'm going to paste in the entire code so we can get this up and running as quickly as possible (if you're following along on laravel.com/learn, you can find this code below the lesson):

1<!DOCTYPE html>
2<html lang="en" data-theme="lofi">
3 
4<head>
5 <meta charset="UTF-8">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <title>Chirper - Home</title>
8 <link rel="preconnect" href="<https://fonts.bunny.net>">
9 <link href="<https://fonts.bunny.net/css?family=instrument-sans:400,500,600,700>" rel="stylesheet" />
10 <link href="<https://cdn.jsdelivr.net/npm/daisyui@5>" rel="stylesheet" type="text/css" />
11 <link href="<https://cdn.jsdelivr.net/npm/daisyui@5/themes.css>" rel="stylesheet" type="text/css" />
12 @vite(['resources/css/app.css', 'resources/js/app.js'])
13</head>
14 
15<body class="min-h-screen flex flex-col bg-base-200 font-sans">
16 <nav class="navbar bg-base-100">
17 <div class="navbar-start">
18 <a href="/" class="btn btn-ghost text-xl">🐦 Chirper</a>
19 </div>
20 <div class="navbar-end gap-2">
21 <a href="#" class="btn btn-ghost btn-sm">Sign In</a>
22 <a href="#" class="btn btn-primary btn-sm">Sign Up</a>
23 </div>
24 </nav>
25 
26 <main class="flex-1 container mx-auto px-4 py-8">
27 <div class="max-w-2xl mx-auto">
28 <div class="card bg-base-100 shadow mt-8">
29 <div class="card-body">
30 <div>
31 <h1 class="text-3xl font-bold">Welcome to Chirper!</h1>
32 <p class="mt-4 text-base-content/60">This is your brand new Laravel application. Time to make it
33 sing (or chirp)!</p>
34 </div>
35 </div>
36 </div>
37 </div>
38 </main>
39 
40 <footer class="footer footer-center p-5 bg-base-300 text-base-content text-xs">
41 <div>
42 <p>© 2025 Chirper - Built with Laravel and ❤️</p>
43 </div>
44 </footer>
45</body>
46 
47</html>

Now, a couple things to note here. First, Blade is just HTML! We could have a whole script tag in here if we wanted, or a style tag. Everything you see in this file is just HTML. Blade just has a little bit of templating magic on top that we'll get into.

Notice that @vite directive? That's telling Laravel we want to use the app.css and app.js files. Laravel gives us Tailwind CSS out of the box and it's already set up for our application. And just to keep things simple and focus on Laravel itself (not styling), we're including DaisyUI as one of the CDN links. It uses Tailwind CSS, so we're just building on top of what Laravel already gives us. You can see we're using the data-theme="lofi" for that clean, minimalist look.

Refresh your browser—you should now see your custom Chirper homepage, complete with a header and navigation links. It's starting to look a little bit better!

Step 3: Extract a Layout with Blade Components

Alright, we have this HTML file here, our home.blade.php, and you can imagine this would get a little bit complex. If we were to add additional pages—like say a sign in or sign up page—we'd copy everything and then add it to a new sign in page and change what we needed to change. But then if something small changed that we needed to update everywhere... you see where this is going.

This is where Blade layouts come into play—or in this case, a layout component that is a Blade file. This is where the fun actually begins!

What Are Blade Components?

Blade components let you take your content from these views, and that content gets injected into the layout via this special $slot variable. So any new page that uses that layout automatically gets all of the extra stuff attached to it from the get-go.

Let's see how this works:

Defining the Layout Component

What we're going to do is create a new layout component. We'll take everything—let's say everything except what's in the main div—and put it in a reusable layout.

In your resources folder, in views, create a new folder called components (this is what Laravel looks for). In that components folder, create a new file called layout.blade.php:

1<!DOCTYPE html>
2<html lang="en" data-theme="lofi">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <title>{{ isset($title) ? $title . ' - Chirper' : 'Chirper' }}</title>
7 <link rel="preconnect" href="<https://fonts.bunny.net>">
8 <link href="<https://fonts.bunny.net/css?family=instrument-sans:400,500,600,700>" rel="stylesheet" />
9 <link href="<https://cdn.jsdelivr.net/npm/daisyui@5>" rel="stylesheet" type="text/css" />
10 <link href="<https://cdn.jsdelivr.net/npm/daisyui@5/themes.css>" rel="stylesheet" type="text/css" />
11 @vite(['resources/css/app.css', 'resources/js/app.js'])
12</head>
13<body class="min-h-screen flex flex-col bg-base-200 font-sans">
14 <nav class="navbar bg-base-100">
15 <div class="navbar-start">
16 <a href="/" class="btn btn-ghost text-xl">🐦 Chirper</a>
17 </div>
18 <div class="navbar-end gap-2">
19 <a href="#" class="btn btn-ghost btn-sm">Sign In</a>
20 <a href="#" class="btn btn-primary btn-sm">Sign Up</a>
21 </div>
22 </nav>
23 
24 <main class="flex-1 container mx-auto px-4 py-8">
25 {{ $slot }}
26 </main>
27 
28 <footer class="footer footer-center p-5 bg-base-300 text-base-content text-xs">
29 <div>
30 <p>© {{ date('Y') }} Chirper - Built with Laravel and ❤️</p>
31 </div>
32 </footer>
33</body>
34</html>

Notice that {{ $slot }} in the main section? That's where the content from our individual pages will be injected. So basically, any other stuff that we add to a file using this layout will be put right there.

Also, look at how we're handling the title: {{ isset($title) ? $title . ' - Chirper' : 'Chirper' }}. We're using some PHP here to say: if this title variable is set, let's use it and append " - Chirper". If not, let's just revert to "Chirper". This gives us consistent branding across all pages!

Configuring Tailwind v4 with app.css

With Tailwind CSS v4, we can customize our design system directly in app.css. Update resources/css/app.css:

1@import 'tailwindcss';
2 
3@source '../../vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php';
4@source '../../storage/framework/views/*.php';
5@source '../**/*.blade.php';
6@source '../**/*.js';
7 
8@theme {
9 --font-sans: 'Instrument Sans', ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji',
10 'Segoe UI Symbol', 'Noto Color Emoji';
11 
12 --animate-fade-out: fade-out 4s ease-in-out forwards;
13 
14 @keyframes fade-out {
15 0%, 70% {
16 opacity: 1;
17 }
18 100% {
19 opacity: 0;
20 pointer-events: none;
21 }
22 }
23}

This configuration:

  • Sets Instrument Sans as our default sans-serif font (used when we apply font-sans)

  • Creates a custom animate-fade-out animation for notifications

  • Uses Tailwind v4's @theme directive to extend the design system

Applying the Layout Component

Now comes the magic part. Let's update our home.blade.php to use this layout. We're going to take just the content within the main div and wrap it with our layout component:

1<x-layout>
2 <x-slot:title>
3 Welcome
4 </x-slot:title>
5 
6 <div class="max-w-2xl mx-auto">
7 <div class="card bg-base-100 shadow mt-8">
8 <div class="card-body">
9 <div>
10 <h1 class="text-3xl font-bold">Welcome to Chirper!</h1>
11 <p class="mt-4 text-base-content/60">This is your brand new Laravel application. Time to make it
12 sing (or chirp)!</p>
13 </div>
14 </div>
15 </div>
16 </div>
17</x-layout>

Laravel knows to look for a layout in the views/components directory, and if you're using the Laravel extension, it'll even validate this for you automatically.

Now the content between the x-layout tags is injected into the layout's $slot. And see that x-slot:title? That's how we can pass the title prop to our layout. We're setting it to "Welcome", so in the browser you'll see "Welcome - Chirper" as the page title.

If we look at our browser and refresh, we shouldn't see any change visually—we just took one file and made it into two files to make it more composable for the future. But now we have an application that's ready to start adding pages. We just have to use this x-layout to get everything we'd expect: the nav, the footer, and a simple layout to add things to.

Step 4: Routing to Your View

Your route in routes/web.php doesn't change:

1Route::get('/', function () {
2 return view('home');
3});

Visit http://localhost:8000 and you'll see your home page, now using your shiny new layout component!

And there you have it! You've just learned how to create a custom view, build a reusable layout with Blade components, and wire everything up with a route. This is the foundation for every page you'll build in Laravel. With all this in mind, we now have an application that's ready to start growing. We're ready for the next lesson!

00:00
So when you first opened up that link, that URL that the server is running
00:05
on when we ran the composer run dev command, that's 1 2 7 0.0 0.01.
00:11
This is on port 8,000.
00:12
You're greeted by Laravel friendly default welcome page at least for
00:17
Laravel 12 at the time of this recording.
00:19
And when we take a look at our code, that's thanks to this route in route,
00:24
and that's that directory web php.
00:27
It looks like this.
00:28
And this little bit of code tells Laravel when someone visits, thanks to
00:33
this route facade, when someone visits the homepage, the slash route, we're
00:38
going to show them this welcome view.
00:41
And again, since I'm using VS code with the official Laravel extension,
00:45
I can have this option click to be able to follow that welcome view
00:50
to this welcome do blade dot php.
00:53
This is where that web PHP route is saying, when a user goes to slash or the
00:59
route directory of your application, we're going to return the view that is welcome
01:05
and that's what this view looks like.
01:07
Let's make some changes.
01:08
Let's update this to, um, let's say the home route.
01:12
I'm going to save that and because I have the VS code extension,
01:15
it is actually giving us an error right here in the editor.
01:18
The view home is not found, and if we were to open up our application,
01:22
we get that same server error.
01:24
This view home is not found.
01:27
So what can we do about this?
01:29
And again, if you're using the Laravel vs code extension, not only does it tell
01:32
you that this view cannot be found, but it also gives you an easy way to fix
01:37
this, to create a new Home Blade PB file.
01:41
But we'll do that by hand for everyone following along in our views directory.
01:46
We're just going to create a new file and I'm gonna call this Home Blade pb.
01:52
Now we're not gonna get an error anymore, but if I was to go
01:55
back, it's just a blank page.
01:57
So why don't we go ahead and create this home view now I'm just going to paste in
02:02
the entire code so that we can get this up and running as quickly as possible.
02:05
And we'll walk through a step by step if you're following along on the Laravel
02:08
Learn page, you can find it below this lesson if you're not following
02:11
along on the laravel.com/learn page.
02:15
That's a great place to find this code.
02:18
Now a couple things to note is that Blade is just HTML.
02:23
We could have a whole script tag in here if we wanted to,
02:27
or we could have a style tag.
02:29
Everything that you see in this file is just HTML Blade, just has a little
02:35
bit of templating on top of that that we can get into in a little bit.
02:40
Now for this application, we have a couple of things that we are giving
02:42
you out of the box, even on top of what Laravel already provides, just to make
02:47
things a little bit easier and keep the focus on what Laravel does and how we can
02:52
build applications with Laravel and less about styling and everything like that.
02:58
Laravel already gives us tailwind CSS out of the box and it's set
03:02
up for our application to use.
03:05
So when I go into this home directory, what's happening is when we use this at
03:10
VT directive, we are saying we want to use the app, CSS file, and the app js
03:16
and again, just to keep things as simple as possible, focusing on Laravel
03:20
itself, we're including Daisy UI as one of the links that we're bringing in.
03:26
Mostly to be able to have, uh, some styles out of the box.
03:30
It uses Tailwind CSS, so we're just building on top of
03:33
what Laravel already gives.
03:35
You can see here that we have this data theme of lo-fi that is Daisy ui,
03:39
all right.
03:40
Now that we have that saved in our code editor, if we go back to our
03:43
browser, we should see the yes.
03:45
Now updated chopper.
03:47
So it's starting to look a little bit better, but this is where
03:51
all the fun actually begins.
03:53
All right.
03:54
We have this HTML file here, our home blade php, and you can imagine this
03:59
would get a little bit complex because if we were to use additional pages,
04:04
like say a sign in or sign up page, we would copy everything and then add it
04:09
to a new sign in page and then change the things that we needed to change.
04:14
But then if something small changed, we needed to change on everything.
04:18
This is where blade layouts, or in this case a layout
04:22
component that is a blade file.
04:24
This is where those come into play.
04:26
You can take your content from these views, and then that gets injected into
04:31
the layout via this special slot variable.
04:35
It looks something like this.
04:37
And so what we're going to do is we're going to create a new layout component to
04:41
take everything here, let's say everything in this main div and push it into a slot.
04:46
So any new page that uses that layout then automatically gets all of.
04:51
This extra stuff attached to it.
04:53
From the get go,
04:54
what we can do is in our resources folder, in our views, we're gonna
04:59
create a new folder, a components folder, and in that components folder,
05:05
this is what Laravel looks for.
05:08
We're gonna create a new layout blade php.
05:12
Now, for right now, I'm just going to copy all of this and
05:15
put this in layout blade PP.
05:19
And in this main class, I'm gonna take this out and add this slot variable.
05:28
So in that way, any time we are using this layout, blade pb, it's
05:33
going to use all of this, including the HDML document, the head, the
05:39
vet, as well as any other additional things that we add to this file.
05:44
And it's going to pass it in to this slot.
05:47
So basically any other stuff that we add to a file in this
05:51
layout will be put right here.
05:54
So that's what we're gonna do in home.
05:56
Um, we're just going to take all of this, actually, let's just
05:59
copy this within the main diviv, I'm going to replace everything.
06:05
We're gonna say X dash.
06:07
Layout.
06:08
And then I'm going to paste everything in X layout, and this is wrapping this up.
06:14
Now Laravel knows to look for a layout in the views components
06:21
directory, and you can see here that the Laravel extension automatically
06:25
says that, Hey, this is valid.
06:27
so if we were to look at our browser, we shouldn't see any change because
06:31
we just kind of took this one file and made it into two files to make
06:35
it more composable in the future.
06:37
And yeah, we, if we refreshed even, we still had and have this application
06:43
looking exactly how we expect.
06:45
We're gonna do one more thing in our layout.
06:49
We're going to make this title a little bit more composable instead of something
06:55
that is hard coded into this file.
06:59
we're actually going to use some PHP here to say, okay, if this title
07:07
variable, and we will get to what this looks like in a little bit.
07:11
If it's set, let's use the title.
07:13
If not, let's just revert to chirp.
07:18
And what this is going to do is now we can set this prop of title in our layout
07:24
where we're using that in our home.
07:25
Do a php.
07:25
Okay, what does this look like?
07:29
Well now we have this X dash slot variable allowed where we can pass it, the title
07:35
and say, we're just going to use welcome.
07:37
So if I was to save this and go back to our browser refresh.
07:41
Here in the browser we see that title.
07:43
Welcome chopper.
07:44
If I was to change this to something else, such as, hi there, bootcamp.
07:49
We should see exactly the same thing.
07:52
Yep.
07:52
Hi there.
07:53
Bootcamp chi.
07:54
I'm gonna change it back to welcome.
07:56
Next to start setting up our application for a little bit more composability
07:59
in the future, we're gonna add some styles to our app CSS file.
08:05
You can find these in the Laravel Learn page for this course lesson.
08:10
And pasting this in.
08:11
This is just a way for us to have a fade out animation.
08:15
That way we don't have to think about this later when we set up notifications.
08:19
And this is Tailwind Cs S'S V four.
08:22
Way of doing this within an application.
08:25
With all this in mind, we now have an application that is
08:28
ready to start adding pages.
08:30
We just have to use this X dash layout in order to get everything that we
08:34
would expect within our application.
08:36
This nav, a footer, and then a simple layout to be able to add things here.
08:42
We're ready for the next lesson.