Laravel Routing basics | Laravel 10 tutorial #4
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
🚀 Laravelのルーティングとビューの作り方
このセクションでは、Laravelフレームワークでのルーティングとビューの作成方法について詳しく説明しています。まず、以前のエピソードでファイル構造を見てきましたが、今回は`web.php`と`welcome.blade.php`という2つのファイルに焦点を当てています。`web.php`はルートを定義する場所で、HTTPリクエストを扱うためのものです。ルートは、ダッシュボード、プロフィールページ、ユーザーページ、フィードページなど、特定のリンクにアクセスしたときに表示されるページを定義します。また、`welcome.blade.php`はビューファイルで、Laravelのビルトイン関数を使用してリソースフォルダ内のビューを表示します。HTMLを直接書くことも可能ですが、`view`関数を使用してビューファイルを返すことで、プロセスを簡素化できます。最後に、ルートを追加して、フィードページとプロフィールページを表示する方法も学びます。
📁 Bladeテンプレートエンジンの活用とサブフォルダの作成
このセクションでは、Bladeテンプレートエンジンの活用方法とビューフォルダ内のサブフォルダを作成する方法について学びます。BladeはLaravelに搭載されているテンプレートエンジンで、PHPコードをHTMLの中に直接書く必要がなく、コードをクリーンに保ちます。新しいビューファイル`feed.blade.php`と`profile.blade.php`を作成し、各ルートでそれらを返すようにルーティングを設定します。また、サブフォルダを作成し、ビューファイルの整理と管理を行います。エラーが発生した際には、Laravelはエラーの具体的な場所を示してくれます。最後に、Bladeの代わりに`.php`ファイルを使用することも可能ですが、Bladeの方がコードが読みやすく、メンテナンスしやすくなります。
Mindmap
Keywords
💡ルート(routes)
💡ビュー(Views)
💡web.php
💡Bladeテンプレートエンジン
💡ルートのコメントアウト
💡サブフォルダ
💡静的メソッド get
💡ルートの定義
💡ビューファイルの命名規則
💡エラー表示
💡PHPファイル
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
all right guys welcome back so on this
episode we're gonna dive more in depth
into routes and Views and exactly how to
create our own views and our own routes
so let's get started so on the previous
episode we basically kind of looked into
different files and the folder structure
so and as I mentioned the most important
files for now for us is going to be the
web.php which is inside the routes
folder and
welcome.blade which is inside the views
folder so let's go and take a look at
the web.php so this is where we Define
our routes and by routes basically I'm
referring to http requests that I will
application can handle so we could have
for example a dashboards routes right
so for example users go to this link we
want to you know display a specific
phage we might have a profile page we
might have a users page or a feed page
so this is where we Define these
different routes right inside this
web.php
and for each of these we may want to
display something different to the user
now
if I'll control Z all the way back
initially this is what we had on the
page basically route
slash which basically this means our
root folder right you know if you have
for example google.com that's the Slash
now and what we are doing here is we are
basically we have a closure here and we
are telling laravel to return
this welcome dot blade file right so
welcome blade.php
that's what we are telling this so this
this view is a function a built-in
function in laudable and all it does is
it goes inside the resources folder the
views folder and finds a file matching
the name you provided right and you
don't need to tell it the blade.php it's
smart enough you just automatically
figures that okay oh you want to view
this view file fine I'll go find it and
I basically return it to the page as a
matter of fact we can actually get rid
of this
and write our own HTML here so I'm just
going to put H1 tag
and here I'm going to say hello world
that's it so I removed that view
function I'll save it I'll go back and
you can see it actually got a bit bigger
so this is basically it's returning the
HTML so but obviously writing HTML like
this is going to be a hassle we're not
going to be doing it this way instead
we're going to basically use View and
pass in the HTML file we want so we
already have a welcome.blade and as a
matter of fact here I'll
add a H1 tag here as well
all right
so and I type in welcome and again we
don't need to type blade.php we don't
need to do that a lot of all this is
smart enough to figure that out
and if you go back we'll we see the same
thing right so nothing changed so you
can either return the HTML or use the
view again we're not going to be using
the HTML tag here anymore we just type
this use this view function to speed up
the process
now what I actually want to do is I want
to have
obviously we have the root folder which
is going to be our main page I also want
to have a feed page and also a profile
page right so I want these two pages to
also exist so what I'm going to do is
actually I'm going to go create them now
if you pay attention this route is
imported here it's a lot it comes with
laurable and
we're using a static function called get
so this get function is
basically referring to a get request you
can also use post if you want to do a
post request we won't be doing that for
now now you don't need to memorize it
all you can actually just copy it for
now until you learn it
and instead of this I'm going to make
feed
and one more thing I'm going to do is
I'm going to also add profile now before
I'll I actually comment them for now
I'll go to the browser I hope you guys
can see at the top I'm going to type in
profile
and if I write this I actually get not
found right so that's because I
commented this out so right now
lauderable does not know about this page
and if someone tries to access it tells
it hey man not found I don't know what
you're looking for so I'm going to go
ahead and uncomment these two
and right now what we are telling
laurable to do is basically display our
welcome.blade HTML right
so if someone accesses slash profile
show them
the welcome view right that's what we
are telling larval to do so I reload and
now all of a sudden we see hello world
right
so that's very cool and it makes it easy
to create a bunch of different pages
now I want different actually HTML for
these pages so I want to actually show a
profile here if I view feed I want to
show feed and if I go to the route to
the root I want to show hello world so
how do we do that
well what I'm going to do is actually
I'm going to go ahead and inside our
views folder create two new files and
I'm going to name them
feed Dot blade.php
and you may have noticed okay what is
displayed blade is a templating engine
that comes with laudable and it
eliminates the need to use PHP code
inside our HTML so we will learn about
blade in the
two episodes from now so for now just
bear with me it's basically a templating
engine for a lot of all
so just name your files.blade.php
like that and I create another one and I
call it profile Dot blade.php
and inside feed I'm going to add an H1
tag and I call it feed
and I'll do the same thing on the
profile page
all right I'm going to save it
now I go back to the VIP file and
instead of returning the welcome page on
the feed route I'm going to return feed
and on the profile page I'm going to
return profile and again we don't need
to add dot play.php laurable is smart
enough it'll figure it out so I'll go
back to these pages so on the route root
folder on the on the root route we see
Hello World I'll type in slash profile
we'll see profile
and I'll do slash
I'll zoom in a bit more
and I'll do slash feed and we see feed
so as you can see this is working we
have
three different paths on our application
and so yeah now we know how to basically
create different routes how to create
our own views and inside this views
folder we can also create subfolders if
you want so for example I'm going to say
you know profile
or let's say users
and inside these users I'm going to
create a profile Dot blade.php
right and I'll actually delete this one
this previous profile we had so I'll
move it inside users dot profile instead
now obviously I move the file so if I go
back to the profile it's gonna give me
an error right profile not found
and a lot of as you can see has a very
nice way of showing you guys errors
and it tells you exactly where the line
that has the error is so it's telling us
hey this profile is not found
so the way you're going to fix it is
actually I'm going to type users dot
profile right so that's where the file
is inside the users folder and the name
is profile now if you see here I'm not
putting slash I'm putting dot that's
basically the convention in Laurel
for The View files we just put a dot to
indicate that it's a subfolder or inside
a folder so if we had another folder
maybe we had a full profile folder and
then we had
I don't index that's how you would
Define it here so I'm going to put a dot
that's the convention we're using the
laurable community just put it like that
I reload the fade and
I'm not seeing anything I think I forgot
to save this file oh yeah
so I put this here I'll reload and you
can see we have profile
all right so you can actually create
subfolders inside the view folder as
well as many as you like to organize
your files and that's the basics and one
more thing I want to show you guys about
this blade is you're not technically
forced to use that blade you can
actually just use
I'll go to the profile you can just use
dot PHP
right so instead of a blade.php we use
PHP and the code will actually still
work
we did the same thing so
and actually you don't need to tell if
it's like dot PHP or dot blade.php
Laurel is very smart it'll just figure
out oh okay it's a PHP or dot blade
it'll figure that on its own
but from now on I'm actually not going
to be using PHP right and the reason is
it's actually easier to use blade and it
makes your code a lot cleaner and easier
to read than using PHP here so if
previously used vanilla PHP you had to
write PHP code
at you know combined with your HTML but
with laurable you know no longer need to
do that so it's going to make things a
lot easier for us
so yeah that's it guys I hope you guys
enjoyed this episode you got a better
idea of what are routes how to create
routes
how to create our own views and just get
a better understanding of it in the
later episodes we're going to learn
about controllers and after that we will
learn more about blade files which is
basically the files we just created what
exactly is blade what it is what can we
do with it and yeah see you guys on the
next episode have a great day bye
Voir Plus de Vidéos Connexes
Next.js 14 Tutorial - 13 - Route Groups
鍋がさらに美味しくなる!しゃぶしゃぶ用とすき焼き用のお肉の違いについて
Learn NEXT.JS 14 🔥 Build a Static Markdown Blog Site
You'll Never Make A Bad Chord Progression Again After Watching This - FL Studio 20 Tutorial
LPサイトを自動生成するAI😆"概要"からWEBページを作成してくれる「AI Landing Page Generator」の使い方!ランディングページの制作もAIに手伝ってもらう時代に突入😎
【知らなきゃ損】相手JGと差をつけれる最強JGルートを徹底解説します!
5.0 / 5 (0 votes)