Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
フロントにおけるLaravel Laravel.hiroshima
Search
Haruki Tazoe
November 24, 2019
Programming
0
190
フロントにおけるLaravel Laravel.hiroshima
Haruki Tazoe
November 24, 2019
Tweet
Share
More Decks by Haruki Tazoe
See All by Haruki Tazoe
ゼミ内LT「Web API: The Good Parts」 を読みました - I read "Web API: The Good Parts".
jdkfx
0
47
フレームワークの内部構造を理解するためにフレームワークを作ってみることにした / phpcon-2021
jdkfx
2
1.1k
陽気なギャングが「行けたら行くぜ」って言ってたよ #23grads / Building a php framework
jdkfx
0
320
Svelte/Sapperで自作ブログをやってみる - Create a blog with Svelte/Sapper
jdkfx
0
170
hiro-it-35
jdkfx
0
640
PHPのオープンソースフレームワークLaravelについて
jdkfx
0
78
Other Decks in Programming
See All in Programming
Outline View in SwiftUI
1024jp
1
330
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
110
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
TypeScript Graph でコードレビューの心理的障壁を乗り越える
ysk8hori
2
1.2k
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
240
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
1k
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
900
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Code Reviewing Like a Champion
maltzj
520
39k
The Cult of Friendly URLs
andyhume
78
6k
Thoughts on Productivity
jonyablonski
67
4.3k
Speed Design
sergeychernyshev
25
620
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
900
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Testing 201, or: Great Expectations
jmmastey
38
7.1k
GitHub's CSS Performance
jonrohan
1030
460k
Fireside Chat
paigeccino
34
3k
Transcript
ϑϩϯτʹ͓͚ΔLaravel 2019/11/24 Laravel.hiroshima vol.2
Laravel.hiroshima ౡͰLaravelΛͬͱΓ্͍͛ͨͱ͍͏ؾ࣋ ͔ͪΒൃͨ͠ษڧձίϛϡχςΟͰ͢ɻ • ༰࠲ஊձɺ͘͘ձɺLTձͳͲ… • ࠓճͰ2ճ
Laravel.hiroshima ͜ͷษڧձϘϥϯςΟΞϕʔεͰߦ͓ͬͯΓɺ ࣝͷγΣΞɾॿ͚߹͍ͰΓཱ͍ͬͯ·͢ɻ ࢀՃͷࡍ͝ཧղɾ͝ڠྗΛ͓ئ͍͍ͨ͠· ͢ɻ
Laravelͱ
20116݄ʹϦϦʔε͞ΕͨɺΦʔϓϯιʔεͷ PHPϑϨʔϜϫʔΫɻ ιʔείʔυGithubʹϗεςΟϯά͞Ε͍ͯ ͯɺGithubͰͷελʔ֫ಘ͕PHPϑϨʔϜ ϫʔΫͷதͰ࠷ଟ͍ɻ
ϑϩϯτʹ͓͚ΔLaravel
1.BladeςϯϓϨʔτ
BladeςϯϓϨʔτͱ Bladeγϯϓϧͳ͕ΒύϫϑϧͳLaravelͷς ϯϓϨʔτΤϯδϯͰ͢ɻଞͷਓؾͷ͋ΔPHP ςϯϓϨʔτͱҟͳΓɺϏϡʔͷதʹPHPΛ هड़͢Δ͜ͱΛڐ͍ͯ͠·͢ɻ
BladeςϯϓϨʔτͱ BladeϏϡʔʹ.blade.phpϑΝΠϧ֦ுࢠΛ ͚ͭɺ௨ৗresources/viewsσΟϨΫτϦͷத ʹઃஔ͠·͢ɻ
ͳʹ͕ศརͳͷʁ
2.ςϯϓϨʔτͷܧঝ
2-1.ϨΠΞτఆٛ
αϯϓϧίʔυ <!—- resources/views/layouts/app.blade.phpͱͯ͠อଘ —-> <html> <head> <title>αϯϓϧΞϓϦ - @yield(‘title’)</title> </head>
<body> @section(‘sidebar’) ͕͜͜ϝΠϯͷαΠυόʔ @show <div class=“container”> @yield(‘content’) </div> </body> </html>
@section໊લ͕ࣔ͢ͱ͓Γʹίϯςϯπͷη ΫγϣϯΛఆٛ͠ɺҰํͷ@yieldࢦఆͨ͠η Ϋγϣϯͷ༰Λදࣔ͢ΔͨΊʹ༻͠·͢ɻ
2-2.ϨΠΞτ֦ு
ࢠͷϏϡʔΛఆٛ͢Δʹɺʮܧঝʯ͢ΔϨΠ ΞτΛࢦఆ͢ΔɺBlade@extendsΛ༻͠· ͢ɻBladeϨΠΞτΛ֦ு͢ΔϏϡʔɺ @sectionΛ༻͠ɺϨΠΞτͷηΫγϣϯʹ ༰Λૠೖ͠·͢ɻ·ͨɺϨΠΞτͰηΫ γϣϯΛදࣔ͢Δʹ@yieldΛ༻͠·͢ɻ
αϯϓϧίʔυ <!-- resources/views/child.blade.phpͱͯ͠อଘ --> @extends('layouts.app') @section('title', 'Page Title') @section('sidebar') @@parent
<p>͜͜ϝΠϯͷαΠυόʔʹՃ͞ΕΔ</p> @endsection @section('content') <p>͕͜͜ຊจͷίϯςϯπ</p> @endsection
sidebarηΫγϣϯͰɺϨΠΞτͷαΠυ όʔͷ༰Λίϯςϯπʹ্ॻ͖͢ΔͷͰͳ ͘Ճ͢ΔͨΊʹ@@parentΛ༻͍ͯ͠·͢ɻ @@parentϏϡʔΛϨϯμ͢Δͱ͖ʹɺϨΠ Ξτͷ༰ʹஔ͖ΘΓ·͢ɻ
3.σʔλදࣔ
αϯϓϧίʔυ Route::get('greeting', function () { return view('welcome', ['name' => 'Samantha']);
}); Hello, {{ $name }}. BladeςϯϓϨʔτʹ͢ʹ…
Bladeͷ{{ }}ه๏XSS߈ܸΛ͙ͨΊɺࣗಈత ʹPHPͷhtmlspecialcharsؔΛ௨͞Ε·͢ɻ Ϗϡʔʹ͞Εͨมͷ༰Λදࣔ͢Δ͚ͩʹ ݶΒͣɺPHPؔͷ݁ՌΛecho͢Δ͜ͱͰ͖ ·͢ɻ The current UNIX timestamp
is {{ time() }} .
σʔλΛΤεέʔϓͨ͘͠ͳ͍߹ɺҎԼͷ ߏจΛ༻͍ͯͩ͘͠͞ɻ Hello, {!! $name !!}.
4.੍ޚߏจ
4-1.Ifจ
αϯϓϧίʔυ @if (count($records) === 1) ̍Ϩίʔυ͋Δʂ @elseif (count($records) > 1)
ෳϨίʔυ͋Δʂ @else Ϩίʔυ͕ͳ͍ʂ @endif
ifจͷߏจʹɺ@ifɺ@elseifɺ@elseɺ@endif Λ༻͠·͢ɻ͜ΕΒͷ͍ํPHPͷߏจͱ ಉ͡Ͱ͢ɻ
@issetͱ@emptyɺಉ໊ͷPHPؔͷศརͳ γϣʔτΧοτͱͯ͠༻Ͱ͖·͢ɻ @isset($records) // $recordsఆٛࡁΈͰnullͰͳ͍ @endisset @empty($records) // $records͕ʮۭʯͩ @endempty
4-2.Switchจ
αϯϓϧίʔυ @switch($i) @case(1) ࠷ॳͷέʔε @break @case(2) ̎൪Ίͷέʔε @break @default σϑΥϧτͷέʔε
@endswitch
@switchɺ@caseɺ@breakɺ@defaultɺ @endswitchΛ༻͠ɺSwitchจΛߏͰ͖· ͢ɻ
4-3.܁Γฦ͠
αϯϓϧίʔυ @for ($i = 0; $i < 10; $i++) ݱࡏͷɿ
{{ $i }} @endfor @foreach ($users as $user) <p>͜Ε {{ $user->id }} ϢʔβʔͰ͢ɻ</p> @endforeach @forelse ($users as $user) <li>{{ $user->name }}</li> @empty <p>Ϣʔβʔͳ͠</p> @endforelse @while (true) <p>ແݶϧʔϓத</p> @endwhile
5.ϑΥʔϜ
5-1.CSRFϑΟʔϧυ
ΞϓϦέʔγϣϯͰHTMLϑΥʔϜΛఆٛ͢Δ ߹ɺCSRFอޢϛυϧΣΞ͕ϦΫΤετΛݕ ࠪͰ͖ΔΑ͏ʹ͢ΔͨΊɺӅ͠CSRFτʔΫϯ ϑΟʔϧυΛؚΊΔඞཁ͕͋Γ·͢ɻ͜ΕΛ @csrfΛ༻ͯ͠ߦ͍·͢ɻ <form method="POST" action="/profile"> @csrf ...
</form>
5-2.MethodϑΟʔϧυ
HTMLϑΥʔϜͰɺPUTɺPATCHɺDELETEϦ ΫΤετΛ࡞Ͱ͖ͳ͍ͨΊɺݟ্͔͚ͷHTTP ಈࢺΛࢦఆ͢ΔͨΊͷ_methodϑΟʔϧυΛ Ճ͢Δඞཁ͕͋Γ·͢ɻ@methodͰ͜ͷϑΟʔ ϧυΛੜͰ͖·͢ɻ <form action="/foo/bar" method="POST"> @method('PUT') ...
</form>
5-3.όϦσʔγϣϯΤϥʔ
@errorɺࢦఆͨ͠ଐੑͷόϦσʔγϣϯΤ ϥʔϝοηʔδ͕͋Δ͔Λ؆୯ʹఆ͢ΔͨΊ ʹ༻͠·͢ɻ@errorͷதͰΤϥʔϝοηʔδ Λදࣔ͢ΔͨΊʹɺ$messageมΛΤίʔ͢ Δ͜ͱՄೳͰ͢ɻ
αϯϓϧίʔυ <!-- /resources/views/post/create.blade.php --> <label for="title">Post Title</label> <input id="title" type="text"
class="@error('title') is-invalid @enderror"> @error('title') <div class="alert alert-danger">{{ $message }}</div> @enderror
6.αϒϏϡʔͷಡΈࠐΈ
Bladeͷ@includeΛ͑ɺϏϡʔͷத͔Β؆ ୯ʹଞͷBladeϏϡʔΛऔΓࠐΊ·͢ɻಡΈࠐΈ ݩͷϏϡʔͰඞཁͳมɺऔΓࠐΈઌͷ ϏϡʔͰར༻ՄೳͰ͢ɻ <div> @include('shared.errors') <form> <!-- ϑΥʔϜͷ༰ -->
</form> </div>
·ͱΊ
Bladeศརʂʂ
ࢀߟ https://readouble.com/laravel/6.x/ja/blade.html