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 ) {
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