Support the ongoing development of Laravel.io →
Configuration Input Forms
Last updated 2 years ago.
0
Solution

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.

Last updated 2 years ago.
0

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 better
Last updated 2 years ago.
0

I 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'
);
Last updated 2 years ago.
0

Thanks a lot :)

Last updated 2 years ago.
0

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');
Last updated 2 years ago.
0

I had originally used the first solution in this thread, but like the real constant way as well from @devtime1. Thanks.

Last updated 2 years ago.
0

thanks alot. this quite good ;)

Last updated 2 years ago.
0

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');
0

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

0

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

0

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.

0

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?

0

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');
Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

akifreh akifreh Joined 5 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.