Successfully reported this slideshow.
Your SlideShare is downloading. ×

3 tips of Laravel

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 40 Ad

More Related Content

Slideshows for you (19)

Viewers also liked (20)

Advertisement

Similar to 3 tips of Laravel (20)

Advertisement

Recently uploaded (20)

3 tips of Laravel

  1. 1. 3 tips of Laravel 2014.07.04 @kurikazu Laravel Meetup Tokyo vol.4 http://am4.jp/?p=124
  2. 2. Kazuaki KURIU • twitter: @kurikazu • 所属: ディップ株式会社 • 求人広告サービスのWebサイト、 スマートフォンアプリの 開発マネジメントを担当 • 普段は「進捗どうですか」と 聞く方 • Laravel歴は1年くらい • 認定スクラムマスター (2011年6月受講)
  3. 3. Laravel 4 で CMS 作りました
  4. 4. 作った機能 • 管理系 • 記事登録(文字、画像) / 編集 / 削除 • 限定記事を見られる会員の管理 • 会員にメール一斉送信 • 閲覧系 • 一般記事 / 限定公開記事 の出し分け • ガラケー対応
  5. 5. 今日の話 •CMSを作ってみて得られた LaravelのTipsをお裾分け
  6. 6. 1.ペジネーション
  7. 7. Laravelのペジネーション $users=DB::table('users')->paginate(10); $users=User::where('age','>',40)->paginate(10); Controllerとか
  8. 8. Laravelのペジネーション $users=DB::table('users')->paginate(10); <div class="container"> <?php foreach ($users as $user): ?> <?php echo $user->name; ?>, <?php echo $user->age; ?> <?php endforeach; ?> </div> <?php echo $users->links(); ?> ←ペジネーション表示 $users=User::where('age','>',40)->paginate(10); Controller View
  9. 9. 結果 << 1 2 3 4 5 6 7 8 ... 20 21 >> Yamada, 20 Tanaka, 31 Suzuki, 21 Honda, 45 Nomura, 12 Mori, 33 Takayama, 54 Takano, 23 Aoki, 61 Nishimura, 47
  10. 10. 検索フォームでやってみる age: User Search Form Submit 30 over
  11. 11. if (Input::has('age')){ $age = Input::get('age'); $users = User::where('age','>',$age)->paginate(3); } else { $users = User::all()->paginate(3);  } <div class="container">  <?php foreach ($users as $user): ?>  <?php echo $user->name; ?>  <?php endforeach; ?>  </div>  <?php echo $users->links(); ?> 検索フォームでやってみる
  12. 12. 1ページ目 OK << 1 2 3 4 5 6 7 >> Tanaka, 31 Honda, 45 Mori, 33 User Search Result
  13. 13. 2ページ目... << 1 2 3 4 5 6 7 8 ... 20 21 >> Honda, 45 Nomura, 12 Mori, 33 User Search Result 条件が引き 継がれない
  14. 14. 原因 ページ数しか渡されない << 1 2 3 4 5 6 7 8 ... 20 21 >> Honda, 45 Nomura, 12 Mori, 33 User Search Result user/list?page=2
  15. 15. 修正 <div class="container"> <?php foreach ($users as $user): ?>  <?php echo $user->name; ?>  <?php endforeach; ?>  </div>  <?php echo $users->appends(array('age' => $age))- >links(); ?> appends で条件も渡してやる
  16. 16. 結果 検索条件を保持して遷移できる << 1 2 3 4 5 6 7 >> User Search Result Takayama, 54 Aoki, 61 Nishimura, 47 user/list? age=30&page=2
  17. 17. 2. ガラケー対応
  18. 18. やりたいこと Laravel PC スマホ ガラケー UTF-8 Shift_JIS
  19. 19. やりたいこと Laravel PC スマホ ガラケー UTF-8 Shift_JIS ここで変換したい
  20. 20. Before/Afterフィルタを使う 方法
  21. 21. ガラケー → Laravel App::before(function($request) { if (ガラケーだったら) { // リクエストをUTF8に変換 $converted = 文字コード変換(Input::get());   Input::merge($converted);   } }); Filter.php UAは Request::server('HTTP_USER_AGENT') で取れる
  22. 22. Laravel → ガラケー App::after(function($request, $response) { if (ガラケーだったら) { // 出力文字列を ShiftJIS に変換 $content = $response->getContent();    $content = mb_convert_encoding($content, 'SJIS', 'UTF-8');    $response->header('Content-Type', 'text/html; charset=Shift_JIS');    $response->setContent($content); } }); Filter.php getContent で取り出して 文字コード変換
  23. 23. Laravel → ガラケー App::after(function($request, $response) { if (ガラケーだったら) { // 出力文字列を ShiftJIS に変換 $content = $response->getContent();    $content = mb_convert_encoding($content, 'SJIS', 'UTF-8');    $response->header('Content-Type', 'text/html; charset=Shift_JIS');    $response->setContent($content); } }); Filter.php デフォルトUTF8なので 設定を上書きする
  24. 24. 3. TwitterにPOST
  25. 25. やりたいこと 記事を更新した時にTwitterにPOST
  26. 26. 方法 thuohn/twitter-l4 パッケージ https://github.com/thujohn/twitter-l4
  27. 27. 設定方法  "thujohn/twitter": "dev-master" composer.jsonに下記の記載を追加  composer update 下記のコマンドを実行する
  28. 28. 設定方法 'providers' => array(   'ThujohnTwitterTwitterServiceProvider', ) ... 'aliases' => array( 'Twitter' => 'ThujohnTwitterTwitterFacade', ) app/config/app.phpに下記の記載を追加 php artisan config:publish thujohn/twitter twitterパッケージの初期化を行う
  29. 29. 設定方法 <?php // You can find the keys here : https://dev.twitter.com/ return array(     'API_URL'             => 'api.twitter.com',     'API_VERSION'         => '1.1',     'USE_SSL'             => true,     'CONSUMER_KEY'        => 'xxxxxxxxxxxxxxxxxxx',     'CONSUMER_SECRET'     => 'yyyyyyyyyyyyyyyyyyy',     'ACCESS_TOKEN'        => 'zzzzzzzzzzzzzzzzzzz',     'ACCESS_TOKEN_SECRET' => 'vvvvvvvvvvvvvvvvvvv', ); app/config/packages/thujohn/twitter/config.php
  30. 30. POSTするときは...  Twitter::postTweet(array('status' => $message, 'format' => 'json'));
  31. 31. おまけ. MismatchToken?
  32. 32. 記事をPOSTする時にエラーが出た Route::filter('csrf', function() {   if (Session::token() != Input::get('_token'))   {    throw new IlluminateSessionTokenMismatchException;   } }); CSRFフィルタでトークンが一致しないエラー
  33. 33. Input を見てみた dd(Input::all());
  34. 34. array(0){} 空っぽでした...
  35. 35. 原因 アップロードしようとしてた 画像ファイルの容量が upload_max_filesize を超えていた
  36. 36. ちなみに
  37. 37. 各環境へのデプロイは Capistrano で簡素化
  38. 38. 簡単デプロイで たのしく 開発しよう
  39. 39. 詳しくは以下で http://qiita.com/kurikazu/items/ e63bc30806aeb3214f93
  40. 40. Enjoy Laravel !!!

×