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
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
subpath importsで始めるモック生活
10tera
0
300
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
4
640
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
170
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
280
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
CSC509 Lecture 09
javiergs
PRO
0
140
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
290
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
A Tale of Four Properties
chriscoyier
156
23k
Embracing the Ebb and Flow
colly
84
4.5k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Music & Morning Musume
bryan
46
6.2k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Fireside Chat
paigeccino
34
3k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
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