[L5.1] How i can define my own constants?

Published 2 years ago by giwrgos

How i can define my own constants on laravel 5.1 by using the Config::get?

Thank you

Best Answer (As Selected By giwrgos)
pmall

Create a file in config.

# file named config/foo.php
<?php

return [
    'bar' => 'baz',
];
echo config('foo.bar'); // outputs baz
pmall
pmall
2 years ago (581,095 XP)

Create a file in config.

# file named config/foo.php
<?php

return [
    'bar' => 'baz',
];
echo config('foo.bar'); // outputs baz
giwrgos

@pmall inside the app folder? I did like this but it returns null

pmall
pmall
2 years ago (581,095 XP)

reread my post and try again

smccoy78

I have done this exact same thing in 5.2 and I am still not getting a value returned from my config.

<?php
    return [
        'SHIPPING_PENDING'      =>      '0',
        'SHIPPING_PROCESSED'    =>      '1',
    ];

I have tried to output the config two different ways:

$type = config('constants.SHIPPING_PENDING');
$type = \Config::get('constants.SHIPPING_PENDING');

I have also tried to output the config value in the view as well:

<a href="{{ route( 'shipping.index', \Config::get('constants.SHIPPING_PENDING') ) }}">

No luck. I don't get why it isn't working. Is there anything else that needs to be changed to allow this to work?

Widespace

Add two line in composer.json file like below:

"autoload": { ....... "files": [ "app/yourConfilFileName.php", ] }

And dump autoload

aalaap

You need to clear the config cache after making a change in an existing config file or adding a new one:

php artisan config:clear

jawadmultani

Thanks aalaap. php artisan config:clear worked

ThDev

Thank you for the tips, it was very useful ;)

Please sign in or create an account to participate in this conversation.