1

I have an associative array which I use in approx all controllers and i was wondering if possible to define that array somewhere at one place and just use in in all controllers?

Kind of like we do in angular.

Like if i can define it in env file or something.

Please let me know if there is a way.

Abhay Maurya
  • 11,819
  • 8
  • 46
  • 64
  • 2
    why not define it in the `config/app.php` and access in your controller using `config('app.key')` – linktoahref Apr 13 '17 at 06:48
  • 1
    You can put in in a helper file? app/Helpers.php which you might add to your composer.json. Or create your own file in the Config folder? – Duikboot Apr 13 '17 at 06:48
  • 1
    or your own config file http://stackoverflow.com/questions/30152032/custom-config-file-access-within-laravel-5-config-files – Autista_z Apr 13 '17 at 06:49
  • 3
    Are you using it at the application or view level? if at the application level, bind it in your `AppServiceProvider`, if you're using it at the view level, use a `View Composer` and share it to all views. – Ohgodwhy Apr 13 '17 at 06:49
  • 2
    In summary: (1) Define as a config file (2) Define in a helper function (3) Share it with all your views (if using it in a view) (4) Singleton class? (define it as a static member of a class and just reference that class). Just pick one you like – apokryfos Apr 13 '17 at 06:56
  • I am using it in application not at view level. linktoahref, Autista_z and Ohgodwhy, please mention your methods as an answer. All will receive +1 and I will accept the most efficient answer – Abhay Maurya Apr 13 '17 at 06:59

3 Answers3

3
  1. Creating an entry in the config/app.php

    'myVar' => [
        'key' => 'value'
    ],
    

and accesing it via config('app.myVar')

  1. Put it in a helper file and access using that helper file

check out this answer https://stackoverflow.com/a/32772686/5808894

  1. Using AppServiceProvider

In your app/providers/AppServiceProvider.php in the boot method add this, make sure to import App use App in AppServiceProvider

public function boot()
{
    App::singleton('myVar', function(){
        return [
            'key' => 'value'
        ];
    });
}

and access the variable in your controller using app('myVar');

Reference https://stackoverflow.com/a/25190686/5808894

Community
  • 1
  • 1
linktoahref
  • 7,812
  • 3
  • 29
  • 51
2

Mentioning it here since no one else did:

public class SharedArrayContainer {
       public static $data = [ 'key' => 'value' ];        
}

and you can use it as:

SharedArrayContainer::$data

Not as good as adding it to the service container but this is what pre-framework me used to do.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
1

I would recommend to create a provider class (service) for it and use laravel Service Container for injection. This way you can create helper methods like get, find, etc... and make use of laravels Dependency Injection (having single instance injected whenever & almost everywhere you want)

Laravel docs

class ExampelService
{
    // associative array
    private arr = []

    public function get(item) {  }
    public function save(item) {  }
    public function has(item) {  }

}
VikingCode
  • 1,022
  • 1
  • 7
  • 20