Laravel: Where to store data and constants of global arrays?

I just started working with Laravel. I need to rewrite the whole system that I created several years ago using Laravel 4 as the base structure. In my old system, I use a .php constant file with declared constants and a globals.php file containing many sets of arrays (e.g. category statuses, event types, languages, etc.). By doing so, I could use something like

foreach ( $langs as $code => $domain ) { // Some stuff } 

somewhere in my application.

My question is how can I store this information in the so-called "laravel way". I tried to use some kind of object to store this information, setting it as a service and creating a facade for it:

applications / libraries / Project / Constants.php

 namespace PJ; class Constants { public static $langs = [ 'es' => 'www.domain.es', 'en' => 'www.domain.us', 'uk' => 'www.domain.uk', 'br' => 'www.domain.br', 'it' => 'www.domain.it', 'de' => 'www.domain.de', 'fr' => 'www.domain.fr' ]; } 

applications / libraries / Project / ConstantsServiceProvider.php

 namespace PJ; use Illuminate\Support\ServiceProvider; class ConstantsServiceProvider extends ServiceProvider { public function register() { $this->app->singleton('PJConstants', function() { return new Constants; }); } } 

applications / libraries / Project / ConstantsFacade.php

 namespace PJ; use Illuminate\Support\Facades\Facade; class ConstantsFacade extends Facade { protected static function getFacadeAccessor() { return 'PJConstants'; } } 

composer.json

 "psr-4": { "PJ\\": "app/libraries/Project" }, 

and therefore I get access to this property as PJ \ Constants :: $ langs .

This works, but I doubt this is the most efficient / correct way to do this. I mean, is this the right way to “propagate” a variable by creating a service provider and facades and all such things? Or where should I put this data?

Thanks for any advice.

EDIT # 01

The data that I want to transfer to all controllers and views can be directly set in the script, as in the example at the beginning of my message , but it can also be generated dynamically, from a database, for example. This data may be a list of categories. I need them in all views to create a navigation bar, but I also need them to define some routing patterns (for example, category / subcategory / product), and also analyze some information in several controllers (for example, get information from a category that contains X).

My array looks something like this:

 $categories = [ 1 => ['name' => 'General', 'parent' => 0, 'description' => 'Lorem ipsum...'], 2 => ['name' => 'Nature', 'parent' => 0, 'description' => 'Lorem ipsum...'], 3 => ['name' => 'World', 'parent' => 0, 'description' => 'Lorem ipsum...'], 4 => ['name' => 'Animals', 'parent' => 2, 'description' => 'Lorem ipsum...'] ] 

As an example. An index is a category identifier, and a value is information associated with a category.

I need this array, also available in all controllers and views.

So, if I save it as a Config variable, is that normal? Or how else can I store this data? What would be the best and semantically correct way?

thank

+74
php laravel laravel-4
Nov 10 '14 at 22:16
source share
6 answers

For most constants used globally in the application, it is enough to store them in configuration files. It is also quite simple.

Create a new file in the app/config directory. Let me call him constants.php

In this case, you should return an array of configuration values.

 return [ 'langs' => [ 'es' => 'www.domain.es', 'en' => 'www.domain.us' // etc ] ]; 

And you can access them as follows

 Config::get('constants.langs'); // or if you want a specific one Config::get('constants.langs.en'); 

And you can also install them

 Config::set('foo.bar', 'test'); 

Please note that the values ​​you set will not be saved. They are available only for the current request.

Update

The configuration is probably not suitable for storing information generated from the database. You could just use the Eloquent Model , for example:

 class Category extends Eloquent { // db table 'categories' will be assumed } 

And request all categories

 Category::all(); 

If the whole model for some reason does not work, you can start thinking about creating your own class and facade. Or you can simply create a class with all the static variables and methods, and then use it without facade material.

+105
Nov 10 '14 at 22:25
source share

For constants

Create the constants.php file in the configuration directory: -

 define('YOUR_DEFINED_CONST', 'Your defined constant value!'); return [ 'your-returned-const' => 'Your returned constant value!' ]; 

You can use them as: -

 echo YOUR_DEFINED_CONST . '<br>'; echo config('constants.your-returned-const'); 

For static arrays

Create the static_arrays.php file in the configuration directory: -

 class StaticArray { public static $langs = [ 'es' => 'www.domain.es', 'en' => 'www.domain.us', 'uk' => 'www.domain.uk', 'br' => 'www.domain.br', 'it' => 'www.domain.it', 'de' => 'www.domain.de', 'fr' => 'www.domain.fr' ]; } 

You can use it like: -

 echo StaticArray::$langs['en']; 

Note. Laravel automatically includes all configuration files, so there is no need for manual mode :)

+20
Aug 25 '16 at 7:25
source share

For global constants in Laravel 5, I don't like to call Config for them. I define them in the Route group as follows:

 // global contants for all requests Route::group(['prefix' => ''], function() { define('USER_ROLE_ADMIN','1'); define('USER_ROLE_ACCOUNT','2'); }); 
+8
Jun 09 '15 at 8:41
source share

To add to the above answer, you will need to include a configuration class before you can start using it in Laravel 5.3

 use Illuminate\Support\Facades\Config; 
+2
Feb 06 '17 at 18:00
source share

I think the best way is to use localization.

Create a new messages.php file in resources/lang/en ( en because this is what is set in my config/app 'locale'=>'en' ) returns an array of all your values

 return [ 'welcome' => 'Welcome to our application' ]; 

to get for laravel 5.3 and below

 echo trans('messages.welcome'); 

or

 echo Lang::get('messages.welcome'); 

to use 5.4

 echo __('messages.welcome') 

localization laravel 5.0

or

laravel 5.4 localization

+2
Apr 12 '17 at 9:44 on
source share

At least in Laravel 5.4, in your constructor you can create them;

 public function __construct() { \Config::set('privileged', array('user1','user2'); \Config::set('SomeOtherConstant', 'my constant'); } 

Then you can call them that way in your methods;

 \Config::get('privileged'); 

Especially useful for static methods in a model, etc.

Link to Laracasts.com https://laracasts.com/discuss/channels/general-discussion/class-apphttpcontrollersconfig-not-found

+1
Aug 10 '17 at 17:34 on
source share



All Articles