I think you're looking for something like this:
Create a file constants.php inside app/config/ and put your settings in an array:
<?php
//file : app/config/constants.php
return [
'ADMIN_NAME' => 'administrator'
];
Then anywhere in your controllers or views you can get the value by using Config Facade:
echo Config::get('constants.ADMIN_NAME');
//output-> administrator
Hope it helps.
Thanks, Just what i wanted.
Is there a difference b/w :
// file: app/cofig/constants.php
<?php return array( "ADMIN" => "administrator" ); and what you suggested, and what is betterI was just using the php 5.4 array syntax which is cleaner. But, yes if you want your code to be backward compatible with php 5.3 use
return array(
'ADMIN' => 'administrator'
);
A bit late, but if you want real constants, you can do for example
<?php
//file : app/start/global.php
require app_path().'/constants.php';
and
<?php
//file : app/constants.php
define('ADMIN', 'administrator');
I had originally used the first solution in this thread, but like the real constant way as well from @devtime1. Thanks.
Hey guys,
I used this method for constants, but I upgraded to 5.0 and there is no more app/start/global.php file. How can I make this work again?
devtime1 said:
A bit late, but if you want real constants, you can do for example
<?php //file : app/start/global.php require app_path().'/constants.php';
and
<?php //file : app/constants.php define('ADMIN', 'administrator');
You can add to composer.json like this:
...
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/constants.php"
]
},
...
Don't forget composer dump-auto
I'm using a query in one of the constants, and the Auth method doesn't work.
if (Auth::check()) define('STARTUP_WALL', DB::table('walls')->where('id', Auth::user()->startup_wall)->pluck('name'));
mgsmus said:
You can add to composer.json like this:
... "autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/" }, "files": [ "app/constants.php" ] }, ...
Don't forget composer dump-auto
If I were you, I'd use Session instead of constants. Constants are good for constant, hard-coded values like database name, port etc. If you insist, you can create a provider and define constants in boot method. I'm still playing with 5.0, maybe there are other ways to do that which I don't know.
When using Real constants file approach I get
Use of undefined constant SITE_DOMAIN - assumed 'SITE_DOMAIN'
if i do $linkcode = 'https://'.SITE_DOMAIN.'/chest/'.$chestkey; in my controller it just spits out SITE_DOMAIN as a literal string. else i get the error above.
any ideas?
very odd. i changed it to SITE_URL and no issues... is SITE_DOMAIN a reserved laravel constant?
Tip: You can also move more sensitive info (that you don't want in the git-repo for instance) to the .env file even though you use this setup:
<?php
//file : app/config/constants.php
return [
'hash_salt' => env('HASH_SALT'),
];
And use it like before:
echo Config::get('constants.hash_salt');
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community