I have a CakePHP 3.3.14 application where I've created 2 subdirectories, webroot/data/downloads/ and webroot/data/master
I want to put these paths in a custom configuration file and reference them in a Controller. But I can't see how to do this.
I've followed the documentation on Configuration but it's not very clear.
So what I've done:
Created config/my_config.php
The above file defines an array:
return [ 'downloadsPath' => 'webroot/data/downloads/', 'masterPath' => 'webroot/data/master/' ];
In config/bootstrap.php I've put: Configure::load('my_config', 'default');
How do I then use this in a Controller? If I put Configure::read('my_config.masterPath'); it gives an error saying: Class 'App\Controller\Configure' not found
If I add use Cake\Core\Configure; to the top of my Controller, that clears the error but the return value is null:
debug(Configure::read('my_config.masterPath')); // null
Loading another config file just extends the default App.config. So just use \Cake\Core\Configure::read('masterPath') and you are good.
EDIT
If it is your goal to have different config paths you could do it like this:
// my_config.php
return [
'MyConfig' => [
'masterPath' => '...',
...
]
];
Then use the config like this:
<?= \Cake\Core\Configure::read('MyConfig.masterPath') ?>
I am rather new to laravel. I have a basic question, What is the best way to add constants in laravel.
I know the .env method that we use to add the constants.
Also I have made one constants file to use them for my project.
For example:
define('OPTION_ATTACHMENT', 13);
define('OPTION_EMAIL', 14);
define('OPTION_MONETERY', 15);
define('OPTION_RATINGS', 16);
define('OPTION_TEXTAREA', 17);
And so on. It can reach upto 100 or more records. So What should be the best approach to write the constants. The .env method. Or adding the constant.php file?
Thanks
For most constants used globally across the application, storing them in config files is sufficient. It is also pretty simple
Create a new file in the config directory. Let's call it constants.php
In there you have to return an array of config values.
return [
'options' => [
'option_attachment' => '13',
'option_email' => '14',
'option_monetery' => '15',
'option_ratings' => '16',
'option_textarea' => '17',
]
];
And you can access them as follows
config('constants.options');
// or if you want a specific one
config('constants.options.option_attachment');
I use aliased class constants :
First, create your class that contain your constants : App/MyApp.php for exemple
namespace App;
class MyApp {
const MYCONST = 'val';
}
Then add it to the aliased classes in the config/app.php
'aliases' => [
//...
'MyApp' => App\MyApp::class,
Finally use them wherever you like (controllers or even blades) :
MyApp::MYCONST
Your question was about the 'best practices' and you asked about the '.env method'.
.env is only for variables that change because the environment changes. Examples of different environments: test, acceptance, production.
So the .env contains database credentials, API keys, etc.
The .env should (imho) never contain constants which are the same over all environments. Just use the suggested config files for that.
You can create a file named paths.php in root directory/config/paths.php
Insert this data into paths.php
define('OPTION_ATTACHMENT', 13);
define('OPTION_EMAIL', 14);
define('OPTION_MONETERY', 15);
define('OPTION_RATINGS', 16);
define('OPTION_TEXTAREA', 17);
Note : make sure to run command : php artisan config:clear
First you make Constants folder inside your app directory.
And then you make Constants.php. Define your constants in this file
For Example :
define('ONE', '1');
define('TWO', '2');
And you modify the composer.json
Alternatively, you can use composer.json to load the bootstrap/constants.php file by adding the following code to the “autoload” section, like so:
"autoload": {
"files": [
"bootstrap/constants.php"
]
}
And update your composer !
I would personally create a class for that.
<?php
namespace App\Transaction\Constants;
/**
* Class TransactionTypeConstant.
*/
final class TransactionTypeConstant
{
public const TYPE_CREDIT = 'CREDIT';
public const TYPE_DEBIT = 'DEBIT';
}
and use it like this:
<?php
namespace App\Transaction;
use App\Transaction\Constants\TransactionTypeConstant;
class Transaction
{
/**
* Execute the task.
*
* #return object
*/
public function run()
{
if ($transaction->type === TransactionTypeConstant::TYPE_DEBIT) {
//do something
}
}
}
I think you should not have a single place for all your constants. Especially no file called constance.php.
I use PHP constance in classes and refer to them in code, so its more readable, typically to avoid magic numbers and typos in strings
Car::where('car_type','=', 'fast_car')->get();
and rather have
Car::where('car_type','=', CarType::FAST)->get();
If its a value that depends on your environment (like email setting, should be different local vs production), then you should add it to a matching files in the config folder (e.g. '/config/mail.php'). You may also just add a new file in the config folder. A config file returns an array, so it could look like this:
<?php
return [
'your_option' => env('YOUR_OPTION')
];
and you can read it using the config helper:
config('your_config_file.your_option');
Its important to never call the env function outside a configuration file, as the env function returns null outside a configuration file when cache is enabled.
If the option does not rely on your environment, you can just add it directly to the matching config file.
<?php
return [
'your_option' => 10
];
Another way as following:
create the constant.php file in app/config directory
in composer.json file, add the directives like this:
"autoload": {
"classmap": [
"database/seeds",
"database/factories"
],
"psr-4": {
"App\\": "app/"
},
"files": [
"app/helpers.php",
"app/config/constants.php"
]
}
You can define constants at the top of the web.php file located in routes and can be access the constants anywhere in project with just constant name
define('OPTION_ATTACHMENT', 13);
define('OPTION_EMAIL', 14);
define('OPTION_MONETERY', 15);
define('OPTION_RATINGS', 16);
define('OPTION_TEXTAREA', 17);
You can simply do this:
Put your constants to 'config/app.php' on main array, like:
'CONSTANT_NAME' => 'CONSTANT_VALUE',
Use them where ever you want with:
{{ Config::get('CONSTANT_NAME') }}
require app_path().'/constants.php';
define('ADMIN', 'administrator');
or -
You can also move more sensitive info
return [
'hash_salt' => env('HASH_SALT'),
];
And use it like before:
echo Config::get('constants.hash_salt');
i think best way to define constant using a helper file. check my solution.
Define file path in composer.json
"extra": {
"laravel": {
"dont-discover": []
}
},
"autoload": {
"files": [
"app/helpers.php",
"app/Helper/function.php" // constant defined here
],
app/Helper/function.php
define("assetPath","UI/");
define("viewPath","UI/");
use this constant anywhere in project. i am using in blade file.
<script src="{{asset(assetPath.'js/jquery.min.js')}}"></script>
<script src="{{asset(assetPath.'js/popper.min.js')}}"></script>
<script src="{{asset(assetPath.'js/bootstrap.min.js')}}"></script>
my approach is better than this
Config::get('constants.options');
Config::get('constants.options.option_attachment');
here another problem is this , you have to run cache:clear or cache command for this. but my approach not required this.
I'm having problem using trans() function in config file, I feel it not supposed to be used that way. However I've no clue on what would be the most efficient way to translate string text in config files (files in /config folder).
Original code
<?php
return [
'daily' => 'Daily'
];
When I try to implement trans() application crashes and laravel return white page without any error messages
<?php
return [
'daily' => trans('app.daily_text')
];
The config files are one of the first stuff Laravel initialize, it means you can't use Translator nor UrlGenerator inside a config file.
I don't know what you are trying to do, but you shouldn't need to use Translator inside a config file though...
You cannot not use trans or route method inside the Laravel config file. At the time the config file is loaded, these methods are not available to run. Also, the purpose of the configuration file is used for storing pure value and we should not trigger any actions inside the configuration file.
I know sometimes you want to put things into config file with dynamic data generated from route or text from language key. In my usecase is: configure the menu structure inside the config file. On that case, you should choose the approach of: storing only the translation key and an array which include information that you can generate the URL at run time.
I put my gist here for you to look up on the approach.
You can just store the key in config file like and then use the trans function in the view to get the translations:
Config file:
<?php
return [
'foo' => 'bar'
];
Then in the view:
{{ trans(config('config.foo') }}
I don't know if this is good practice but I ended doing this in my similar situation.
Config.php:
'Foo' => array('
'route' => 'route.name',
'name' => 'translated_line', //translated in lang file ex. /en/general.php
'),
Then in the view I used:
{{ Lang::get('general.'.Config::get('foo.name'))) }}
Maybe this is too late but I posted it here anyway so that maybe someone will find it useful, like me :))
As of Laravel v5.4, you can use the __ helper function to access the translations after Laravel has booted.
Example:
config/example.php
<?php
return [
'daily' => 'Daily',
'monthly' => 'app.monthly_text',
'yearly' => 'app.yearly_text'
];
resources/lang/en/app.php
<?php
return [
'monthly_text' => 'Monthly'
];
You can access the translations like so:
<?php
// ...
$daily = config('example.daily');
$a = __($daily); // "Daily"
$monthly = config('example.monthly');
$b = __($monthly); // "Monthly"
$yearly = config('example.yearly');
$c = __($yearly); // "app.yearly_text"
I am using laravel 4 and I have created a file inside app/config as follows : The file name is aws.php
<?php
return [
'key' => 'xyz',
];
Now, from my controller I tried to access the key as follows :
$key = Config::get('aws.key');
but it returns null. How can I solve this problem ? I have googled it but have not found the solution.
You can access "key" by Config::get('aws.key');
but need to run this command: php artisan config:clear
try
$key = \Config::get('aws.key');
Config is not in the same namespace as class that call this facade, so You want to go to global namespace to search Config class
I'm trying to set an alias in Yii2 but I'm getting a Invalid Parameter / Invalid path alias for the below code that is placed in the app config file:
'aliases' => [
// Set the editor language dir
'#editor_lang_dir' => '#webroot/scripts/sceditor/languages/',
],
If I remove the # it works.
I noticed you can do this:
Yii::setAlias('#foobar', '#foo/bar');
...but I would prefer to set it within the app config file. Is this not possible? If so, how?
Yii2 basic application
To set inside config file, write this inside $config array
'aliases' => [
'#name1' => 'path/to/path1',
'#name2' => 'path/to/path2',
],
Ref: http://www.yiiframework.com/doc-2.0/guide-structure-applications.html
But as mentioned here,
The #yii alias is defined when you include the Yii.php file in your entry script. The rest of the aliases are defined in the application constructor when applying the application configuration.
If you need to use predefined alias, write one component and link it in config bootstrap array
namespace app\components;
use Yii;
use yii\base\Component;
class Aliases extends Component
{
public function init()
{
Yii::setAlias('#editor_lang_dir', Yii::getAlias('#webroot').'/scripts/sceditor/languages/');
}
}
and inside config file, add 'app\components\Aliases' to bootstrap array
'bootstrap' => [
'log',
'app\components\Aliases',
],
In config folder create file aliases.php. And put this:
Yii::setAlias('webroot', dirname(dirname(__DIR__)) . '/web');
Yii::setAlias('editor_lang_dir', '#webroot/scripts/sceditor/languages/');
In web folder in index.php file put:
require(__DIR__ . '/../config/aliases.php');
Before:
(new yii\web\Application($config))->run();
If run echo in view file:
echo Yii::getAlias('#editor_lang_dir');
Show like this:
C:\OpenServer\domains\yii2_basic/web/scripts/sceditor/languages/
#webroot alias is not available at this point, it is defined during application bootstrap :
https://github.com/yiisoft/yii2/blob/2.0.3/framework/web/Application.php#L60
No need to define this alias yourself, you should simply use another one :
'aliases' => [
// Set the editor language dir
'#editor_lang_dir' => '#app/web/scripts/sceditor/languages/',
],
To improve on #vitalik_74's answer
you can place it in config/web.php instead(if you are using the basic yii app, I'm not sure about the main config file in the advance version, but the same applies, just put the require on the main config file) so that it gets shorten to:
require(__DIR__ . '/aliases.php');