title: How to make a class accessible globally in a Laravel application?
body: <p>I am trying to create a global helper to store site settings in my Laravel application. Meaning, I need a helper to set settings from a controller and access that settings object from anywhere using the same helper. I don't need to store these settings on a database or in files. That's why I need these kinds of functionality.</p>
  
  <p>What is did are as follows,</p>
  
  <ul>
  <li>Create a class in <code>app/Helpers</code> directory called <code>SettingsHelper</code> and auto load the directory.</li>
  </ul>
  
  <pre><code>namespace App\Helpers;
  
  class SettingsHelper{
  
      protected $vars;
  
      public function all(){
          return $this-&gt;vars;
      }
  
      public function get($key, $default = null){
  
          if (is_array($this-&gt;vars) &amp;&amp; array_key_exists($key, $this-&gt;vars)) {
              return $this-&gt;vars[$key];
          }
  
          return $default;
      }
  
      public function put($key, $value){
          return $this-&gt;vars[$key] =$value;
      }
  
  }
  </code></pre>
  
  <ul>
  <li>Create a helper function to resolve the class if not already resolved</li>
  </ul>
  
  <pre><code>if ( ! function_exists('settings')) {
  
      function settings(){
  
          if (app('\App\Helpers\SettingsHelper')) {
              return app('\App\Helpers\SettingsHelper');
          }
  
          return app()-&gt;make('\App\Helpers\SettingsHelper');
      }
  
  }
  </code></pre>
  
  <ul>
  <li>Set and get setting using the helper</li>
  </ul>
  
  <pre><code>settings()-&gt;put('test', 'test2');
  dd(settings()-&gt;all());
  
  </code></pre>
  
  <p>But <code>null</code> is returned. I tried dumping <code>settings()-&gt;put('test', 'test2')</code> and it returned the passed value <code>test2</code>. </p>
  
  <p>Is this the correct approach for this?</p>