What is the best practice for adding constants in laravel? (Long List)

后端 未结 11 742
猫巷女王i
猫巷女王i 2021-01-30 06:21

I am rather new to laravel. I have a basic question, What is the best way to add constants in laravel. I know the .env method that we use to add the constants. Also I have mad

11条回答
  •  [愿得一人]
    2021-01-30 07:02

    I use aliased class constants :

    First, create your class that contain your constants : App/MyApp.php for exemple

    namespace App;
    
    class MyApp {
       const MYCONST = 'val';
    }
    

    Then add it to the aliased classes in the config/app.php

    'aliases' => [
      //...
      'MyApp' => App\MyApp::class,
    

    Finally use them wherever you like (controllers or even blades) :

    MyApp::MYCONST
    

提交回复
热议问题