0
2

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:プロジェクト内で定数を使う方法

Posted at

概要

Laravelで定数を管理したい時にどうすればいいかわからなかったので備忘録にする

結論

①app/config/配下に定数用のファイルを作る
②定数を呼び出す場合config('ファイル名.定数名')とすればOK

例えばapp/config/配下にconst.phpを作る

const.php
<?php
return [
  //'定数名' => '出力したい値',
    'FILE_URL' => 'public/file', 
];

コントローラーから定数を呼び出す場合config('ファイル名.定数名')とすればOK

TestController.php

$url = config('const.FILE_URL');

.envの定数を呼び出す場合

envの定数を使いたい場合下記のように行う

.env
MAIL_FROM_ADDRESS=test@gmail.com

app/config/app.phpに以下のような形式で追記する
'定数名' => env('envに記載している定数名', 'デフォルト値),

app.php
'email' => env('MAIL_FROM_ADDRESS', 'test@gmail.com'),

呼び出す場合はconfig('app.定数名')とすればOK

TestController.php
$sendEmail = config('app.email');

以上となります。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?