14
7

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 5 years have passed since last update.

Laravel5.7: ビューをレイアウトと個別ビューに分ける

Last updated at Posted at 2017-05-25

全ページで共通する部分をレイアウトとして抜き出し、それを各ページのビューが継承するようにします。

親記事

Laravel 5.7で基本的なCRUDを作る - Qiita

レイアウトを作る

練習用のレイアウトを作ります。

:link: readouble.com: Bladeテンプレート

resources/views/layouts/foo.blade.php
<!DOCTYPE html>
<html lang="ja">
    <head>
        <title>@yield('title')</title>
    </head>
    <body>
        <h1>練習用レイアウト</h1>
        {{-- 個別ページの内容はここに挿入される --}}
        @yield('content')
    </body>
</html>

個別ビューを作る

レイアウトを継承した個別ビューを作ります。

resources/views/foo/foo4.blade.php
@extends('../layouts/foo')
@section('title', $title)
@section('content')
    {{-- 個別ページの内容 --}}
    <h2>{{ $title }}</h2>
    <p>{{ $body }}</p>
@endsection
app/Http/Controllers/FooController.php
    // foo4アクションを追加
    public function foo4()
    {
        return view('foo.foo4', [
            'title' => 'Foo4',
            'body' => 'Hello World!'
        ]);
    }
routes/web.php
// ルートを追加
Route::get('foo/foo4', 'FooController@foo4');

005.png

14
7
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
14
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?