Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing and retrieving constants in laravel

Tags:

php

laravel

I have just started playing with laravel haveing come from codeigniter and I am trying to work out the best way to define a series of constants. CI uses a constants folder under app/config and I was largely happy with this approach for most things but wanted advice as to the best way to do this in Laravel.

My constants fall into 3 categories and I would like advice if possible on how best to store and retrieve each (baring in mind I'm completely new to Laravel.

Type 1: Constants which need to be loaded everytime a controller is called: e.g. I would like to start by defining a constant to tell me if the user is requesting content via ajax, this is what I used to do in CI constants file: define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')

Type 2: Constants which can be changed by the user: Are these best stored in a database or would writing to a config file be a better option? Again looking for advice on how to store and retrieve

Type 3: Constants which are only needed in specific controllers: Ideally I would like to be able to group constants into arrays or separate files and pull them out in groups as required. e.g. I could just retrieve my upload settings for my upload controller.

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

like image 765
SwiftD Avatar asked Feb 19 '23 08:02

SwiftD


1 Answers

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.

like image 82
crynobone Avatar answered Feb 26 '23 19:02

crynobone