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 1 year has passed since last update.

Laravelで定数をパブリック定数として定義してまとめる方法

Posted at

Laravelで定数をパブリック定数として定義する方法は、configファイルに記述する方法、Modelクラスファイルに記述する方法などいろいろあるようです。
とりあえずは、専用のConstクラスファイルを作成する、という方法がいいようなのでまとめをメモ。

作り方

Laravelルートに
\app\Consts\CommonConst.php
ファイルを作ります。ファイル名は任意。Consts ファルダも自分で作成します。
下記のように定義し、

<?php
namespace App\Consts;

class CommonConst
{
    public const EMAIL_SendInspiring = '****@gmail.com';
    public const TODO_WEEK = ["日", "月", "火", "水", "木", "金", "土"];

}

呼び出す際は
\App\Consts\CommonConst::EMAIL_SendInspiring
のようにすれば、呼び出すことができる。
配列で定義して、呼び出し側でforeach で回すことも可能。
(参考サイトに例あり)

参考

Laravelで定数を使うときにConfigを使う腰抜けはもう死んだ
[Laravel]定数(const)はどこに書くべきか?チーム内でやる場合のおすすめのマジックナンバー対処方法

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?