0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

laravel 【ページの表示フロー】

Posted at

どのようにlaravelのページが表示されているのかを見ていきたいと思います。

まずは新規のプロジェクトを作成します。プロジェクト名を blog とします。
laravel new blog cd blog php artisan serve

まずはこの図のAccess〜showを頭に入れておくと理解しやすいと思います。
3AE1D46A-51B1-4461-B705-DB36A7032905.png

ルーティング

routes/web.php
Route::get('/', function () {
    return view('welcome');
});
HTTPメソッド パス アクション
GET / クロージャー

関数の中でwelcomeビューを表示するようにしています。
HTTPメソッドやパスの組合せに応じたアクションを実行するのが、Routing の役割です。

ビュー

resouces/views/welcome.blade.php
<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
        ...
        <title>Laravel</title>
        ...
    </head>
    <body>
        <div class="flex-center position-ref full-height">
            ...
            <div class="content">
                <div class="title m-b-md">
                    Laravel
                </div>
                ...
            </div>
        </div>
    </body>
</html>

http://localhost:8000/ にアクセスした時に表示されるHTMLです
Laravel では bladeというテンプレートエンジンを使って、ビューファイル(HTMLテンプレート)からHTMLを生成しています。

コントローラ

php artisan make:controller WelcomeController
としてコントローラを作りindexメソッドを追加しましょう

app/Http/Controllers/WelcomeController.php
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class WelcomeController extends Controller
{
    public function index()
    {
        return view('welcome');
    }
}

indexメソッドはview関数を実行し、その戻り値を返しています。
viewの戻り値がブラウザへのレスポンスになります

Routing 設定のアクションをクロージャーからコントローラに変更します。

routes/web.php
//Route::get('/', function () {
//    return view('welcome');
//});
Route::get('/', 'WelcomeController@index');
HTTPメソッド パス アクション
GET / WelcomeControllerクラスのindex メソッドを実行する

まとめ

このことから
Route::get('/', 'WelcomeController@index');
ルーティングでアクセスするコントローラを決め、実行されると
コントローラのindexメソッドからwelcome.blade.phpを表示命令し
welcome.blade.phpが表示されるのが分かります。

簡単にですが一連の流れを覚えておくとイメージしやすいかと思います。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?