LoginSignup
0
1

More than 3 years have passed since last update.

Laravel:レイアウト作成における継承

Last updated at Posted at 2020-11-10

【概要】

1.結論

2.継承とは何か

3.どのように記載するのか

4.開発環境

1.結論

@extendsはsection,yieldを使用するための土地
@section,@yieldは親子関係であり目的地に行くために道の入り口と出口
です

2.継承とは何か

継承とは、既に存在しているクラスやレイアウトを受け継いで、そのまま利用することです。つまり同じヘッダーやフッターを使いまわしたいときに1から作る手間が省けることをいいます。

3.どのように記載するのか

layouts/common.blade.php
<h1>@yield('title')</h1>
 @yeild('main-upper') //---❶
<div class="main-contents">
 @yield('content')
</div>
<div class="footer">
 @yield('footer')
</div>
index.blade.php
@extends('layouts.common')//---❷

@section('title',***)//---❸

@section('main-upper')//---❹
 メイン上部の表示です
@endsection

@section('content')
 内容になります
@endsection

@section('footer')
 フッターになります
@endsection

*cssは省いております。
❶:まず親(テンプレレイアウトになりうるcommon)になるレイアウトには@yeildを使用します。
❷:これがないと@section,@yeildが使えないのでこれを記載します。()の部分は1個上のディレクトリ名(layouts).最初のフォルダ名(common)になります。
❸:@sectionはもう一つの使い方で第二引数に文字や数字を入れれば簡単に文字列を表示できます。
❹:@sectionを記載したら適用範囲を指定するため@endsectionで閉じます。

4.開発環境

PHP 7.4.10
Laravel 8.9
Apache 2.4.41
Mysql 5.6.47
Sequl Pro 1.1.2

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