Laravel Routing basics | Laravel 10 tutorial #4

Yelo Code
13 Jul 202310:01

Summary

TLDRこのビデオスクリプトでは、Laravelフレームワークでのルートとビューの作成方法について詳しく説明しています。まず、ルートとはアプリケーションが処理できるHTTPリクエストであり、web.phpファイルで定義されるという点に触れています。次に、ビューは、web.phpで指定されたルートに応じて表示されるHTMLであり、Bladeテンプレートエンジンを使用してPHPコードをHTMLに統合する方法が紹介されています。また、ルートに応じて異なるビューを表示する方法や、ビューフォルダー内のサブフォルダーを作成してファイルを整理する方法も学ぶことができます。最後に、Bladeの利便性と、Vanilla PHPと比較してコードが清潔で読みやすい理由が強調されています。このエピソードを通じて、ルートとビューの基本を理解し、今後のエピソードでコントローラーやBladeファイルについてさらに学ぶことができます。

Takeaways

  • 📖 まずはルートとビューの概念を理解しましょう。ルートはアプリケーションが処理できるHTTPリクエストを定義します。
  • 📂 `web.php` はルートを定義する重要なファイルで、`routes` フォルダ内にあります。
  • 🌐 `welcome.blade.php` は `views` フォルダ内のテンプレートファイルで、デフォルトのルートに関連付けられます。
  • 🔄 Laravelは`view`関数を使って、指定された名前のビューファイルを自動的に探して返します。
  • 🚫 HTMLを直接書く代わりに、`view`関数とビューファイルの名前を用いて、ビューを返すことが推奨されます。
  • 📝 ルートごとに異なるビューを表示するために、`views`フォルダ内に新しいビューファイルを作成します。
  • 📄 `feed.blade.php` や `profile.blade.php` などの新しいビューファイルは、特定のルートに応じて表示されます。
  • 📁 ビューファイルは、サブフォルダに整理して管理することができ、Laravelはドット(`.`)を使ってサブフォルダを表します。
  • 🔍 Laravelはエラーが発生したときに、具体的なエラーメッセージと問題のある行を表示してくれます。
  • 🛠️ BladeテンプレートエンジンはPHPコードをHTML内に直接書く必要性を排除し、コードをクリーンに保ちます。
  • 📚 今後のエピソードでは、コントローラーとBladeファイルの詳細について学ぶ予定です。
  • 🎉 このエピソードを通じて、ルートとビューの作成方法、そしてLaravelでの作業の理解が深まりました。

Q & A

  • ルートとビューとは何ですか?

    -ルートは、アプリケーションが処理できるHTTPリクエストを定義したものであり、ビューはユーザーに表示されるHTMLコンテンツを定義するものです。

  • web.phpファイルとは何ですか?

    -web.phpは、Laravelのroutesフォルダ内にあり、アプリケーションのルートを定義する場所です。

  • ルートを定義する際に使用されるメソッドは何ですか?

    -ルートを定義する際には、static関数`get`を使用してGETリクエストを参照します。必要に応じて、`post`メソッドも使用可能です。

  • Bladeテンプレートエンジンとは何ですか?

    -BladeはLaravelに搭載されているテンプレートエンジンで、PHPコードをHTMLの中に直接書く必要がなく、コードをきれいに保つことができます。

  • ビューファイルの拡張子は何ですか?

    -ビューファイルは`.blade.php`という拡張子を持ちます。Laravelは拡張子を自動的に認識して処理します。

  • ルートとビューファイルの名前を一致させる必要がありますか?

    -はい、ルートとビューファイルの名前は一致させる必要があります。Laravelはビューファイル名に基づいてルートに応答を返します。

  • サブフォルダ内のビューファイルを参照するにはどうすればよいですか?

    -サブフォルダ内のビューファイルを参照する場合は、ドット(`.`)を使ってフォルダ構造を示します。例えば、`users.profile`のように指定します。

  • ルートに応じて異なるビューを表示するにはどうすればよいですか?

    -ルートに応じて異なるビューを表示するためには、それぞれのルートに対して異なるビューファイル名を返すようにルーティングを設定します。

  • Laravelのエラー表示はどのように機能しますか?

    -Laravelはエラーが発生したときに、エラーの詳細と問題のある行を表示してデバッグを支援します。

  • ルートに関連付けられたビューファイルが見つからない場合、どうなりますか?

    -ルートに関連付けられたビューファイルが見つからない場合、Laravelは404エラー(ページが見つからない)を返します。

  • Laravelのルーティングとビューを使用する利点は何ですか?

    -Laravelのルーティングとビューを使用することで、アプリケーションのURL構造を整理し、コードを再利用しやすく保ち、メンテナンスしやすくなります。

  • Laravelのルーティングで使用されるHTTPリクエストメソッドは何ですか?

    -Laravelのルーティングで使用されるHTTPリクエストメソッドには、一般的にGETとPOSTが用いられます。

Outlines

00:00

🚀 Laravelのルーティングとビューの作り方

このセクションでは、Laravelフレームワークでのルーティングとビューの作成方法について詳しく説明しています。まず、以前のエピソードでファイル構造を見てきましたが、今回は`web.php`と`welcome.blade.php`という2つのファイルに焦点を当てています。`web.php`はルートを定義する場所で、HTTPリクエストを扱うためのものです。ルートは、ダッシュボード、プロフィールページ、ユーザーページ、フィードページなど、特定のリンクにアクセスしたときに表示されるページを定義します。また、`welcome.blade.php`はビューファイルで、Laravelのビルトイン関数を使用してリソースフォルダ内のビューを表示します。HTMLを直接書くことも可能ですが、`view`関数を使用してビューファイルを返すことで、プロセスを簡素化できます。最後に、ルートを追加して、フィードページとプロフィールページを表示する方法も学びます。

05:01

📁 Bladeテンプレートエンジンの活用とサブフォルダの作成

このセクションでは、Bladeテンプレートエンジンの活用方法とビューフォルダ内のサブフォルダを作成する方法について学びます。BladeはLaravelに搭載されているテンプレートエンジンで、PHPコードをHTMLの中に直接書く必要がなく、コードをクリーンに保ちます。新しいビューファイル`feed.blade.php`と`profile.blade.php`を作成し、各ルートでそれらを返すようにルーティングを設定します。また、サブフォルダを作成し、ビューファイルの整理と管理を行います。エラーが発生した際には、Laravelはエラーの具体的な場所を示してくれます。最後に、Bladeの代わりに`.php`ファイルを使用することも可能ですが、Bladeの方がコードが読みやすく、メンテナンスしやすくなります。

Mindmap

Keywords

💡ルート(routes)

ルートとは、アプリケーションが処理できるHTTPリクエストのことです。このビデオでは、ルートを定義することで、ユーザーが特定のリンクにアクセスしたときに表示されるページを制御する方法が説明されています。たとえば、ダッシュボードやプロフィールページなどのルートを作成することが可能です。

💡ビュー(Views)

ビューは、ユーザーがブラウザで表示されるHTMLコンテンツを意味します。Laravelでは、ビューファイル(例:welcome.blade.php)を使用して、ルートに関連付けられたHTMLテンプレートを定義します。このビデオでは、ルートに応じて異なるビューを表示する方法が説明されています。

💡web.php

web.phpは、Laravelアプリケーションのルートフォルダ内にあるファイルで、アプリケーションのルートを定義するために使用されます。このファイル内で、ルートとそれに対応するアクションを設定します。ビデオでは、web.phpを使用してルートを定義し、ビューを表示する方法が紹介されています。

💡Bladeテンプレートエンジン

Bladeは、Laravelに組み込まれたテンプレートエンジンで、PHPコードとHTMLを分離して、コードの可読性とメンテナンス性を向上させます。ビデオでは、Bladeを使用して、よりクリーンで読みやすいコードを作成することが強調されています。

💡ルートのコメントアウト

ルートをコメントアウトすることで、一時的にアプリケーションからそのルートを無効にできます。ビデオでは、ルートをコメントアウトして、ブラウザでアクセスしたときに'404 Not Found'エラーが発生することを示しています。

💡サブフォルダ

ビューフォルダ内にサブフォルダを作成することで、ビューファイルの整理と階層構造の構築ができます。ビデオでは、'users'というサブフォルダを作成し、その中に'profile.blade.php'ファイルを格納することで、ルートの階層構造を示しています。

💡静的メソッド get

Laravelのルーティングでは、静的メソッド get を使用して、GETリクエストを扱うルートを作成します。ビデオでは、getメソッドを使用して、ルートを定義し、それに対応するビューを表示する方法が説明されています。

💡ルートの定義

ルートの定義とは、特定のURLパターンにアクセスされたときに実行される処理を指定することです。ビデオでは、ルートを定義して、それに応じたビューを表示する方法が詳しく説明されています。

💡ビューファイルの命名規則

Laravelのビューファイルは、'.blade.php'という拡張子を持ちます。サブフォルダ内のビューファイルは、ドット(.)を使って区別されます。ビデオでは、'users.profile'という形式でビューを参照することで、サブフォルダ内のビューファイルを指定する方法が紹介されています。

💡エラー表示

Laravelは、エラーが発生したときに詳細なエラーページを表示し、問題の原因を特定するのに役立ちます。ビデオでは、ビューファイルの移動後に発生したエラーと、その解決方法が説明されています。

💡PHPファイル

Laravelでは、Bladeテンプレートを使用することが推奨されていますが、必要に応じて'.php'という拡張子のPHPファイルを直接使用することもできます。ビデオでは、Bladeの代わりにPHPファイルを使用する場合の例が示されていますが、Bladeの利便性が強調されています。

Highlights

Exploring in-depth the creation of routes and views in a web application

Introduction to the importance of the `web.php` file for defining routes

Explanation of the `welcome.blade.php` file's role within the views folder

Demonstration of how to define HTTP request routes in `web.php`

Use of the `view` function to return a specific view file

Directly writing HTML within the route definition as an alternative to using view files

The convenience of Laravel's automatic recognition of `.blade.php` files

Creating additional pages like a feed and profile page using routes

Commenting and uncommenting routes to control their availability

Assigning different views to different routes for a dynamic user experience

Creating new view files like `feed.blade.php` and `profile.blade.php`

Returning specific views based on the route accessed

Organization of view files using subfolders for better structure

Error handling and debugging tips provided by Laravel when a view file is not found

Convention of using dots to indicate subfolders within the views folder

Option to use `.php` files instead of `.blade.php` for simpler PHP code

Advantages of using Blade templating engine for cleaner and more readable code

Upcoming episodes will cover controllers and an in-depth look at Blade files

Transcripts

play00:01

all right guys welcome back so on this

play00:03

episode we're gonna dive more in depth

play00:05

into routes and Views and exactly how to

play00:08

create our own views and our own routes

play00:10

so let's get started so on the previous

play00:13

episode we basically kind of looked into

play00:16

different files and the folder structure

play00:18

so and as I mentioned the most important

play00:20

files for now for us is going to be the

play00:23

web.php which is inside the routes

play00:25

folder and

play00:26

welcome.blade which is inside the views

play00:29

folder so let's go and take a look at

play00:31

the web.php so this is where we Define

play00:34

our routes and by routes basically I'm

play00:37

referring to http requests that I will

play00:40

application can handle so we could have

play00:42

for example a dashboards routes right

play00:46

so for example users go to this link we

play00:49

want to you know display a specific

play00:51

phage we might have a profile page we

play00:53

might have a users page or a feed page

play00:56

so this is where we Define these

play00:59

different routes right inside this

play01:02

web.php

play01:03

and for each of these we may want to

play01:06

display something different to the user

play01:09

now

play01:10

if I'll control Z all the way back

play01:12

initially this is what we had on the

play01:15

page basically route

play01:17

slash which basically this means our

play01:20

root folder right you know if you have

play01:22

for example google.com that's the Slash

play01:25

now and what we are doing here is we are

play01:28

basically we have a closure here and we

play01:30

are telling laravel to return

play01:33

this welcome dot blade file right so

play01:36

welcome blade.php

play01:38

that's what we are telling this so this

play01:40

this view is a function a built-in

play01:43

function in laudable and all it does is

play01:45

it goes inside the resources folder the

play01:48

views folder and finds a file matching

play01:50

the name you provided right and you

play01:52

don't need to tell it the blade.php it's

play01:54

smart enough you just automatically

play01:56

figures that okay oh you want to view

play01:58

this view file fine I'll go find it and

play02:01

I basically return it to the page as a

play02:03

matter of fact we can actually get rid

play02:04

of this

play02:06

and write our own HTML here so I'm just

play02:09

going to put H1 tag

play02:12

and here I'm going to say hello world

play02:16

that's it so I removed that view

play02:19

function I'll save it I'll go back and

play02:23

you can see it actually got a bit bigger

play02:24

so this is basically it's returning the

play02:27

HTML so but obviously writing HTML like

play02:30

this is going to be a hassle we're not

play02:32

going to be doing it this way instead

play02:34

we're going to basically use View and

play02:36

pass in the HTML file we want so we

play02:39

already have a welcome.blade and as a

play02:41

matter of fact here I'll

play02:43

add a H1 tag here as well

play02:47

all right

play02:49

so and I type in welcome and again we

play02:52

don't need to type blade.php we don't

play02:54

need to do that a lot of all this is

play02:56

smart enough to figure that out

play02:58

and if you go back we'll we see the same

play03:00

thing right so nothing changed so you

play03:03

can either return the HTML or use the

play03:05

view again we're not going to be using

play03:07

the HTML tag here anymore we just type

play03:09

this use this view function to speed up

play03:12

the process

play03:14

now what I actually want to do is I want

play03:17

to have

play03:18

obviously we have the root folder which

play03:21

is going to be our main page I also want

play03:25

to have a feed page and also a profile

play03:28

page right so I want these two pages to

play03:30

also exist so what I'm going to do is

play03:32

actually I'm going to go create them now

play03:34

if you pay attention this route is

play03:36

imported here it's a lot it comes with

play03:38

laurable and

play03:40

we're using a static function called get

play03:43

so this get function is

play03:46

basically referring to a get request you

play03:49

can also use post if you want to do a

play03:51

post request we won't be doing that for

play03:53

now now you don't need to memorize it

play03:54

all you can actually just copy it for

play03:56

now until you learn it

play03:58

and instead of this I'm going to make

play04:00

feed

play04:02

and one more thing I'm going to do is

play04:04

I'm going to also add profile now before

play04:07

I'll I actually comment them for now

play04:09

I'll go to the browser I hope you guys

play04:11

can see at the top I'm going to type in

play04:13

profile

play04:15

and if I write this I actually get not

play04:18

found right so that's because I

play04:20

commented this out so right now

play04:22

lauderable does not know about this page

play04:24

and if someone tries to access it tells

play04:26

it hey man not found I don't know what

play04:28

you're looking for so I'm going to go

play04:30

ahead and uncomment these two

play04:32

and right now what we are telling

play04:34

laurable to do is basically display our

play04:36

welcome.blade HTML right

play04:39

so if someone accesses slash profile

play04:42

show them

play04:43

the welcome view right that's what we

play04:45

are telling larval to do so I reload and

play04:48

now all of a sudden we see hello world

play04:50

right

play04:51

so that's very cool and it makes it easy

play04:53

to create a bunch of different pages

play04:55

now I want different actually HTML for

play04:59

these pages so I want to actually show a

play05:00

profile here if I view feed I want to

play05:03

show feed and if I go to the route to

play05:06

the root I want to show hello world so

play05:08

how do we do that

play05:09

well what I'm going to do is actually

play05:11

I'm going to go ahead and inside our

play05:12

views folder create two new files and

play05:16

I'm going to name them

play05:17

feed Dot blade.php

play05:21

and you may have noticed okay what is

play05:23

displayed blade is a templating engine

play05:25

that comes with laudable and it

play05:27

eliminates the need to use PHP code

play05:29

inside our HTML so we will learn about

play05:31

blade in the

play05:33

two episodes from now so for now just

play05:36

bear with me it's basically a templating

play05:38

engine for a lot of all

play05:40

so just name your files.blade.php

play05:44

like that and I create another one and I

play05:46

call it profile Dot blade.php

play05:50

and inside feed I'm going to add an H1

play05:53

tag and I call it feed

play05:56

and I'll do the same thing on the

play05:58

profile page

play06:00

all right I'm going to save it

play06:02

now I go back to the VIP file and

play06:04

instead of returning the welcome page on

play06:07

the feed route I'm going to return feed

play06:10

and on the profile page I'm going to

play06:11

return profile and again we don't need

play06:13

to add dot play.php laurable is smart

play06:16

enough it'll figure it out so I'll go

play06:18

back to these pages so on the route root

play06:21

folder on the on the root route we see

play06:23

Hello World I'll type in slash profile

play06:26

we'll see profile

play06:28

and I'll do slash

play06:31

I'll zoom in a bit more

play06:33

and I'll do slash feed and we see feed

play06:36

so as you can see this is working we

play06:39

have

play06:40

three different paths on our application

play06:44

and so yeah now we know how to basically

play06:46

create different routes how to create

play06:48

our own views and inside this views

play06:50

folder we can also create subfolders if

play06:53

you want so for example I'm going to say

play06:56

you know profile

play06:59

or let's say users

play07:01

and inside these users I'm going to

play07:03

create a profile Dot blade.php

play07:07

right and I'll actually delete this one

play07:10

this previous profile we had so I'll

play07:13

move it inside users dot profile instead

play07:17

now obviously I move the file so if I go

play07:19

back to the profile it's gonna give me

play07:21

an error right profile not found

play07:24

and a lot of as you can see has a very

play07:27

nice way of showing you guys errors

play07:30

and it tells you exactly where the line

play07:32

that has the error is so it's telling us

play07:35

hey this profile is not found

play07:37

so the way you're going to fix it is

play07:39

actually I'm going to type users dot

play07:41

profile right so that's where the file

play07:43

is inside the users folder and the name

play07:45

is profile now if you see here I'm not

play07:47

putting slash I'm putting dot that's

play07:49

basically the convention in Laurel

play07:52

for The View files we just put a dot to

play07:55

indicate that it's a subfolder or inside

play07:57

a folder so if we had another folder

play07:59

maybe we had a full profile folder and

play08:02

then we had

play08:03

I don't index that's how you would

play08:06

Define it here so I'm going to put a dot

play08:08

that's the convention we're using the

play08:10

laurable community just put it like that

play08:13

I reload the fade and

play08:16

I'm not seeing anything I think I forgot

play08:18

to save this file oh yeah

play08:21

so I put this here I'll reload and you

play08:24

can see we have profile

play08:27

all right so you can actually create

play08:28

subfolders inside the view folder as

play08:30

well as many as you like to organize

play08:32

your files and that's the basics and one

play08:34

more thing I want to show you guys about

play08:35

this blade is you're not technically

play08:38

forced to use that blade you can

play08:40

actually just use

play08:41

I'll go to the profile you can just use

play08:43

dot PHP

play08:47

right so instead of a blade.php we use

play08:49

PHP and the code will actually still

play08:52

work

play08:53

we did the same thing so

play08:56

and actually you don't need to tell if

play08:58

it's like dot PHP or dot blade.php

play09:01

Laurel is very smart it'll just figure

play09:03

out oh okay it's a PHP or dot blade

play09:06

it'll figure that on its own

play09:08

but from now on I'm actually not going

play09:10

to be using PHP right and the reason is

play09:13

it's actually easier to use blade and it

play09:15

makes your code a lot cleaner and easier

play09:18

to read than using PHP here so if

play09:20

previously used vanilla PHP you had to

play09:23

write PHP code

play09:25

at you know combined with your HTML but

play09:28

with laurable you know no longer need to

play09:30

do that so it's going to make things a

play09:32

lot easier for us

play09:33

so yeah that's it guys I hope you guys

play09:35

enjoyed this episode you got a better

play09:37

idea of what are routes how to create

play09:39

routes

play09:41

how to create our own views and just get

play09:44

a better understanding of it in the

play09:45

later episodes we're going to learn

play09:46

about controllers and after that we will

play09:49

learn more about blade files which is

play09:51

basically the files we just created what

play09:53

exactly is blade what it is what can we

play09:56

do with it and yeah see you guys on the

play09:58

next episode have a great day bye

Rate This

5.0 / 5 (0 votes)

Related Tags
Laravelウェブ開発ルートビューPHPテンプレートカスタマイズウェブページエラーハンドリングプログラミング
Do you need a summary in English?