Saving and getting constants in laravel

I just started playing with laravel hasing coming from codeigniter, and I'm trying to find a better way to determine the sequence of constants. CI uses the constants folder in the / config application, and I was pretty much pleased with this approach for most things, but wanted advice on the best way to do this in Laravel.

My constants are divided into 3 categories, and I would like, if possible, to advise on the best way to store and retrieve them (bearing in mind that I am completely not familiar with Laravel.

Type 1 : Constants that need to be loaded every time the controller is called: for example, I would like to start by defining a constant, to tell me if the user requests content via ajax, this is what I used in the constant file CI: define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')

Type 2 : Constants that can be changed by the user: Are they best stored in a database or written to a configuration file? Again looking for tips on how to store and retrieve

Type 3 : Constants that are only needed by specific controllers: Ideally, I would like to be able to group constants into arrays or individual files and pull them out in groups as needed. for example, I could just load the download settings for my download controller.

Thanks for any help / advice in advance - examples would be greatly appreciated

+4
source share
3 answers

Type 1 You can add any constant to the start.php Bundle file (or applications).

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

+8
source

My solution is to create a file inside the app / config folder with the name "constants.php" application /Config/constants.php

The code inside is in an array.

"some value", 'c2' => "some value"); ? >

then inside each controller you can Config :: get ('constants.c1');

I think this is an easy way to make it more scalable.

0
source
 <?php //file : app/config/constants.php return [ 'IS_AJAX' => isset($_SERVER['HTTP_X_REQUESTED_WITH'] ]; 

anywhere:

 echo Config::get('constants.IS_AJAX'); 
0
source

All Articles