欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How to use my constants in Larvel

Hi, I am very new to Larvel. I have been doing development in CI. My question is that we used to define our own constants in CI in my_constants.php file containing all constant values specific to a site i.e. admin name, email, record per page, image_size, default_image...etc, so we don't have to change it in the whole project. :

$config['ADMIN_NAME'] = 'Administrator';

Then we could easily use them in our controllers,views etc by : $this->config->item('ADMIN_NAME');

How can this be achieved in Larvel ?

Thanks.

usm4n replied 3 years ago 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.

akifreh replied 3 years ago

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
usm4n replied 3 years ago

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'
);
akifreh replied 3 years ago

Thanks a lot :)

devtime1 replied 3 years ago

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');
iamface replied 3 years ago

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

greentornado replied 3 years ago

thanks alot. this quite good ;)

wbali replied 3 years ago

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');
mgsmus replied 3 years ago

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

wbali replied 3 years ago

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

mgsmus replied 3 years ago

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.

wolfgangx replied 2 years ago

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?

edwardkarlsson replied 1 year ago

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

来自  https://laravel.io/forum/06-05-2014-how-to-use-my-constants-in-larvel

普通分类: