iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
๐Ÿ”

Learning Laravel with Osaru: Where to Find View Files! ๐Ÿ’

ใซๅ…ฌ้–‹

Hello everyone!
I'm Osaru ๐Ÿ’, and I'm currently obsessed with Blue Jeans/HANA lol.
Lately, I've been listening to my favorite songs on repeat while studying for certifications!

In the midst of that, an engineer in my company who is learning Laravel asked me this...

Osaru-san, where were the View files located in Laravel again?

So, in this article, I'm going to introduce the Laravel directory structure!

โ“ What is Laravel?

Laravel is a handy toolbox (framework) for building web apps with PHP.
https://laravel.com/
And Laravel operates using a mechanism called "MVC."

What is MVC?

I introduce it in the article below, so please take a look if you're interested ๐Ÿ™Œ

[Too late to ask?] What is "MVC Architecture"?
https://zenn.dev/osaru07m/articles/2af42e02027b86

๐Ÿ‘€ Let's look at the big picture!

Let's try creating a project called sample.
Let's take a rough look at the directory structure immediately after creating it with the following command.

laravel new sample

In this case, sample/ is the root directory of the Laravel project.

sample/
โ”œโ”€โ”€ app/
โ”œโ”€โ”€ bootstrap/
โ”œโ”€โ”€ config/
โ”œโ”€โ”€ database/
โ”œโ”€โ”€ public/
โ”œโ”€โ”€ resources/
โ”œโ”€โ”€ routes/
โ”œโ”€โ”€ storage/
โ”œโ”€โ”€ tests/
โ””โ”€โ”€ vendor/

At first, you might think "There are too many directories!", but don't worry!

The places you basically use often are limited, and specifically for frontend tasks, the one you'll touch is resources/views.

๐Ÿ–ผ Where are the View Files?

The "View files" mentioned in the question earlier are located in resources/views/!

This is where you place Blade template files (.blade.php).
For example, welcome.blade.php is included by default, and it's used to display the top page.

โœ๏ธ Let's Try Using It Easily

Create a View File

This time, we will create a Blade template file called hello!
And, I have written the following code.

resources/views/hello.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Hello</title>
</head>
<body>
    <h1>ใ“ใ‚“ใซใกใฏใ€Laravel๏ผ</h1>
</body>
</html>

Edit the Route File

Next, let's edit routes/web.php and try displaying the Blade template file we just created!

routes/web.php
Route::get('/hello', function () {
    return view('hello');
});

Now, if you access http://localhost/hello in your browser,
the hello.blade.php you just created will be displayed! ๐ŸŽ‰

๐Ÿ—‚ Quick Introduction to Other Directories

There are other directories that you will frequently touch besides View.

Directory Description
app/ A place to organize application logic.
As the name suggests, it's the "content of the app," so it contains controllers (responsible for receiving requests) and models (responsible for the database).
routes/ Responsible for page entry points, containing files for routing settings (e.g., web.php).
public/ Contains publicly accessible static files (index.php (containing logic to run Laravel), images, CSS, JS).
config/ Contains configuration files for basic settings.
database/ Contains all database-related files, such as migrations and seeders.

โœ… If You Can Understand and Remember This Much, You're OK!

Laravel's directory structure may look cluttered at first glance, but for now, you'll be fine if you keep these three in mind: resources/views, app/, and routes/. ๐Ÿ‘

  • Page appearance: resources/views/
  • Actual processing: app/
  • Page entry points (URL assignment): routes/

If you can get a sense of how these three are connected, developing with Laravel will become much easier ๐Ÿ’ก

๐Ÿ“ Summary

  • Laravel's View files are in resources/views/
  • You can write templates with the .blade.php extension
  • By combining them with routing, you can freely add pages

Laravel may seem complex, but it's actually a very simple and convenient framework ๐Ÿ™Œ
Let's become a Laravel Master together with me! lol ๐Ÿ’ช

Discussion