Windows用XAMPP+Laravel Bladeを使ってみる
- ※XAMPPのインストールフォルダは
D:/dev/xampp
にインストールします。 - ※XAMPPのインストールしていない場合は、[styled_link link=”https://techpjin.sakura.ne.jp/techpjin_new/archives/38933″ textColor=”#000000″]WINDOWS XAMPP にて LARAVEL をインストール (XAMPP V1.8.3インストール)[/styled_link]
- ※Composerのインストールしていない場合は、[styled_link link=”https://techpjin.sakura.ne.jp/techpjin_new/archives/38972″ textColor=”#000000″]WINDOWS XAMPP にて LARAVEL をインストール (COMPOSER 1.0.0-ALPHA8インストール)[/styled_link]
- ※Laravelのインストールしていない場合は、[styled_link link=”https://techpjin.sakura.ne.jp/techpjin_new/archives/39295″ textColor=”#000000″]WINDOWS XAMPP にて LARAVEL をインストール (LARAVEL V4.2インストール)[/styled_link]
- ※Laravelの環境設定していない場合は、[styled_link link=”https://techpjin.sakura.ne.jp/techpjin_new/archives/39416″ textColor=”#000000″]Windows XAMPP で Laravel 環境設定[/styled_link]
- ※LaravelのDB設定していない場合は、[styled_link link=”https://techpjin.sakura.ne.jp/techpjin_new/archives/40368″ textColor=”#000000″]Windows用XAMPP+Laravel DB設定[/styled_link]
- ※Modelを利用するので、Eloquentを読んでいない方は、[styled_link link=”https://techpjin.sakura.ne.jp/techpjin_new/archives/40435″ textColor=”#000000″]Windows用XAMPP+Laravel Eloquentを使ってみる[/styled_link]
Bladeとは
Laravelで利用できるテンプレートエンジンです。
フロー制御、条件分岐、エスケープ処理などが簡単に書くことができます。
特に継承が出来るので、親テンプレートでパターンを決めておくと、熟練度によりますが、かなり開発工数を削減出来ると思います。
共通ロジックなどは親テンプレートにまとめておくと、全体の変更を簡単に行うことが出来るようになります。
フロー制御、条件分岐、エスケープ処理などが簡単に書くことができます。
特に継承が出来るので、親テンプレートでパターンを決めておくと、熟練度によりますが、かなり開発工数を削減出来ると思います。
共通ロジックなどは親テンプレートにまとめておくと、全体の変更を簡単に行うことが出来るようになります。
今回は継承などは気にせず、簡単な使い方を説明します。
Userモデルを利用して、登録、更新、削除を実行する画面を作成
前回Userモデルを作成しましたので、今回はそのモデルを利用して、登録・更新・削除を実行する画面を作成します。
親テンプレートを利用していないので、ファイルは1つになります。
親テンプレートを利用していないので、ファイルは1つになります。
Bladeはファイルの拡張子を「.blade.php」とします。
今回はUserモデルを利用することから「user.blade.php」としています。
Bladeは「views」フォルダーの下に作成します。
まずは、user.blade.phpの内容を見てみましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | <!DOCTYPE html> <html lang= "ja" > < head > <meta charset= "utf-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1" > <title>Laravel dragon ユーザサンプル< /title > <link rel= "stylesheet" href= "//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" > <link rel= "stylesheet" href= "//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css" > <!--[ if lt IE 9]> <script src= "//oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js" >< /script > <script src= "//oss.maxcdn.com/respond/1.4.2/respond.min.js" >< /script > <![endif]--> <style> *{ font-family: "メイリオ" ; } body { padding- top : 30px; } < /style > < /head > <body> <div class= "navbar navbar-fixed-top navbar-inverse" role= "navigation" > <div class= "container" > <div class= "navbar-header" > <button type = "button" class= "navbar-toggle" data-toggle= "collapse" data-target= ".navbar-collapse" > <span class= "sr-only" >navigation< /span > <span class= "icon-bar" >< /span > <span class= "icon-bar" >< /span > <span class= "icon-bar" >< /span > < /button > <a class= "navbar-brand" href= "#" >Dragon< /a > < /div > <div class= "navbar-collapse collapse" > <ul class= "nav navbar-nav" > <li class= "active" ><a href= "#" >active< /a >< /li > < /ul > < /div ><!--/.nav-collapse --> < /div > < /div > <div class= "container" > <div class= "page-header" > <h2> <span class= "glyphicon glyphicon-user" >< /span > ユーザ一覧 < /h2 > < /div > <div class= "row" > <div class= "col-xs-12 col-lg-12" > <form action= "/user/create" method= "post" class= "form-horizontal" > <div class= "form-group" > <label for = "name" class= "col-xs-2" >名前< /label > <div class= "col-xs-4" > <input type = "text" name= "name" id = "name" class= "form-control" > < /div > < /div > <div class= "form-group" > <label for = "email" class= "col-xs-2" >メールアドレス< /label > <div class= "col-xs-4" > <input type = "text" name= "email" id = "email" class= "form-control" > < /div > < /div > <div class= "form-group" > <label for = "tel" class= "col-xs-2" >電話< /label > <div class= "col-xs-4" > <input type = "text" name= "tel" id = "tel" class= "form-control" > < /div > < /div > <div class= "form-group" > <label for = "zip" class= "col-xs-2" >住所< /label > <div class= "col-xs-10" > <div class= "row" > <div class= "col-xs-2" > <input type = "text" name= "zip" id = "zip" class= "form-control" > < /div > <div class= "col-xs-7" > <input type = "text" name= "address" id = "address" class= "form-control" > < /div > < /div > < /div > < /div > <div class= "form-group" > <div class= "col-xs-offset-2 col-xs-4" > <button type = "submit" class= "btn btn-success" >登録< /button > < /div > < /div > < /form > < /div > < /div > <hr> <div class= "row" > <div class= "col-xs-12 col-lg-12" > <table class= "table" > <thead> <th>切替< /th > <th>操作< /th > <th> #</th> <th>名前< /th > <th>メール< /th > <th>電話< /th > <th>住所< /th > < /thead > <tbody> @foreach ($ users as $user) < tr > <td> <button type = "button" class= "btn btn-info action-update" >操作切替< /button > < /td > <td> <form action= "/user/delete" method= "post" > <input type = "hidden" name= "id" value= "{{{$user->id}}}" > <button type = "submit" class= "btn btn-default" >削除< /button > < /form > < /td > <td>{{{$user-> id }}}< /td > <td>{{{$user->name}}}< /td > <td>{{{$user->email}}}< /td > <td>{{{$user->tel}}}< /td > <td>{{{$user->zip}}} {{{$user->address}}}< /td > < /tr > <form action= "/user/update" method= "post" > < tr class= "hidden" > <td> <button type = "button" class= "btn btn-info action-delete" >操作切替< /button > < /td > <td> <input type = "hidden" name= "id" value= "{{{$user->id}}}" > <button type = "submit" class= "btn btn-warning" >更新< /button > < /td > <td>{{{$user-> id }}}< /td > <td><input type = "text" class= "form-control" name= "name" value= "{{{$user->name}}}" >< /td > <td><input type = "text" class= "form-control" name= "email" value= "{{{$user->email}}}" >< /td > <td><input type = "text" class= "form-control" name= "tel" value= "{{{$user->tel}}}" >< /td > <td> <div class= "row" > <div class= "col-xs-3" > <input type = "text" class= "form-control" name= "zip" value= "{{{$user->zip}}}" > < /div > <div class= "col-xs-9" > <input type = "text" class= "form-control" name= "address" value= "{{{$user->address}}}" > < /div > < /div > < /td > < /tr > < /form > @endforeach < /tbody > < /table > < /div > < /div > < /div ><!-- /container --> <script src= "//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" >< /script > <script src= "//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js" >< /script > <script> $( function (){ $( '.action-update' ).on( 'click' , function (){ $(this).parents( 'tr' ).hide().next().next().removeClass( 'hidden' ).show(); }); $( '.action-delete' ).on( 'click' , function (){ $(this).parents( 'tr' ).hide().prev().prev().show(); }); }); < /script > < /body > < /html > |
Bladeの記述
このテンプレートで利用しているBladeの記述は2つです。
@foreach
–@endforeach
{{{ exp }}}
1.@foreach
phpのforeachと殆ど変わりがない感じで利用できます。
キーが欲しい場合は、phpと同様に
@foreach ($users as $user)
とありますが、$users
の一つの要素を $user
へ代入して、ループ内を実行します。キーが欲しい場合は、phpと同様に
$users as $key => $value
のように $key =>
を追加します。
ちなみにfor文は @for($i=0; $i<count($users); $i++)
のように記載します。
中括弧(波括弧)がないだけで、ほぼ同じような記述です。
2.{{ exp }} {{{ exp }}}
{{ exp }}
は <?= ?>
と同様な意味になります。中に指定した変数を出力するというものです。
中括弧が2つ指定と3つ指定があります。2つの場合はHTMLをエスケープしません。
3つ指定すると変数にHTMLの特殊文字がある場合にエスケープ処理を行います。
<?= htmlspecialchars( exp ) ?>
と同様な動きをします。
WEBアプリケーション関連 人気連載リンク
基礎からPHPWEBアプリ解発を学ぶなら・・
PHP基礎 連載
より実践的なWEBアプリ開発講座!Bootstrap活用を学ぶなら・・
魁!小野の塾 連載