Laravel: Where to Store Global Arrays Data and Constants

Laravel: Where to store global arrays data and constants?

For most constants used globally across the application, storing them in config files is sufficient. It is also pretty simple

Create a new file in the app/config directory. Let's call it constants.php

In there you have to return an array of config 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 set them as well

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

Note that the values you set will not persist. They are only available for the current request.

Update

The config is probably not the right place to store information generated from the database. You could just use an Eloquent Model like:

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

And query all categories

Category::all();

If the whole Model thing for some reason isn't working out you can start thinking about creating your own class and a facade. Or you could just create a class with all static variables and methods and then use it without the facade stuff.

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

For most constants used globally across the application, storing them in config files is sufficient. It is also pretty simple

Create a new file in the config directory. Let's call it constants.php

In there you have to return an array of config values.

return [
'options' => [
'option_attachment' => '13',
'option_email' => '14',
'option_monetery' => '15',
'option_ratings' => '16',
'option_textarea' => '17',
]
];

And you can access them as follows

config('constants.options');
// or if you want a specific one
config('constants.options.option_attachment');

Storing and retrieving constants in laravel

Type 1
You can add any constant in Bundle (or application) start.php file.

Type 2 and 3
I would suggest using Config for both these requirement.

Where is better to place global variables at laravel?

global variable is a synonym for bad practice , i think you can't make it better

i also think that this question is opinion based.

anyway

why you dont do this

you can create a new controller lets say BaseController

BaseController extends Controller {

protected $hodor;

public function __construct()
{
$hodor = 'HOLD THE FREAKIN DOOR';
}

}

Then in your other controllers you can do this

MyOtherController extends BaseController {

public function hodor()
{
echo "today: $this->hodor";
}

}

But as i said Global variables are considered as bad practices so i think You should use sessions

i think you can use a config file or .env

global variable for all controller and views

At first, a config file is appropriate for this kind of things but you may also use another approach, which is as given below (Laravel - 4):

// You can keep this in your filters.php file
App::before(function($request) {
App::singleton('site_settings', function(){
return Setting::all();
});

// If you use this line of code then it'll be available in any view
// as $site_settings but you may also use app('site_settings') as well
View::share('site_settings', app('site_settings'));
});

To get the same data in any controller you may use:

$site_settings = app('site_settings');

There are many ways, just use one or another, which one you prefer but I'm using the Container.



Related Topics



Leave a reply



Submit