0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Laravel try catch finally文でDBとの接続を確認する

Last updated at Posted at 2020-09-02

目的

  • LaravelアプリとDBの接続を確認することのできるビューページの追加方法をまとめる

実施環境

  • ハードウェア環境
項目 情報
OS macOS Catalina(10.15.5)
ハードウェア MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports)
プロセッサ 2GHzクアッドコアIntel Core i5
メモリ 32 GB 3733 MHz LPDDR4
グラフィックス Intel Iris Plus Graphics 1536 MB
  • ソフトウェア環境
項目 情報 備考
PHPバージョン 7.4.3 Homwbrewを用いて導入
Laravelバージョン 7.0.8 commposerを用いてこちらの方法で導入→Mac Laravelの環境構築を行う
MySQLバージョン 8.0.19 for osx10.13 on x86_64 Homwbrewを用いてこちらの方法で導入→Mac HomebrewでMySQLをインストールする

前提条件

  • 実施環境に記載した環境と同じ、またはそれに準ずる環境が用意できていること。

前提情報

読後感

  • 作成したDB確認ページにアクセスした際、正常にLaravelアプリとDBが接続できていると「DB OK」と表示し、HTTPレスポンスヘッダメッセージのステータスコード200を返す。
  • DBと接続できていないと「ERROR DB connection NG」と表示し、HTTPレスポンスヘッダメッセージのステータスコード500を返す。

概要

  1. ルーティング情報の追記
  2. コントローラファイルの作成と記載
  3. 確認

詳細

  1. ルーティング情報の追記
    1. アプリ名ディレクトリで下記コマンドを実行してルーティングファイルを開く。

      $ vi routes/web.php
      
    2. 下記の内容を追記する。

      アプリ名ディレクトリ/routes/web.php
      Route::get('/db_check/index', 'DbCheckController@index');
      
  2. コントローラファイルの作成と記載
    1. アプリ名ディレクトリで下記コマンドを実行してコントローラファイルを作成する。

      $ php artisan make:controller DbCheckController
      
    2. アプリ名ディレクトリで下記コマンドを実行して作成したコントローラファイルを開く。

      $ vi app/Http/Controllers/DbCheckController.php
      
    3. コントローラファイルに下記の内容を追記する。(Throwableは基底インターフェース名である。Exceptionも本インターフェースを実装している。参考文献にPHP公式ドキュメントへのリンクあり。)

      アプリ名ディレクトリ/app/Http/Controllers/DbCheckController.php
      // 下記を追記
      use Illuminate\Support\Facades\DB;
      
      
      public function index()
      {
          try {
              $db_infos = DB::select('select 1 as status');
              $message = 'DB OK';
              $status = 200;
          } catch (\Throwable $th) {
              $message = 'ERROR DB connection NG ';
              $status = 500;
          } finally {
              return response($message, $status);
          }
      }
      
    4. 追記後のコントローラファイルの全体の内容を下記に記載する。

      アプリ名ディレクトリ/app/Http/Controllers/DbCheckController.php
      <?php
      
      namespace App\Http\Controllers;
      
      use Illuminate\Http\Request;
      // 下記を追記
      use Illuminate\Support\Facades\DB;
      
      class DbCheckController extends Controller
      {
          //
          public function index()
          {
              try {
                  $dbs = DB::select('select 1 as status');
                  $message = 'DB OK';
                  $status = 200;
              } catch (\Throwable $th) {
                  $message = 'ERROR DB connection NG';
                  $status = 500;
              } finally {
                  return response($message, $status);
              }
          }
      }
      
  3. 確認
    1. アプリ名ディレクトリで下記コマンドを実行してローカルサーバを起動する。

      $ php artisan serve
      
    2. 下記にアクセスする。

    3. 下記の様に表示されることを確認する。

      127_0_0_1_8000_db_check_index.png

    4. いったんローカルサーバを停止する。

    5. アプリ名ディレクトリで下記コマンドを実行して.envファイルを開く。

      $ vi .env
      ```
      
    6. DB_PASSWORD=の記載を実際とは異なる内容にして保存する。

    7. アプリ名ディレクトリで下記コマンドを実行してローカルサーバを起動する。

      $ php artisan serve
      
    8. 下記にアクセスする。

    9. 下記の様に表示されていれば例外処理の記載完了である。

      127_0_0_1_8000_db_check_index-2.png

参考文献

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?