I have an admins array as a variable which is now declared in ViewComposer. However, now I want to use the same variable in middlewares too. Where is the best way to place a variable so I can share it through other files?
What I mean by ViewComposers:
$admins = ['hello#example.com'];
Where should I define it so that I can access it both in Middleware and ViewComposer? Or maybe completely globally through the app?
(If it was a string, I'd use .env but it doesn't accept arrays)
You can use configuration-values
$value = config('admins.list');// change admins and list is the key array returned
To set configuration values at runtime, pass an array to the config helper:
config(['admins.list' => [ 'admis list' ] ]);
admins would be inside config folder as admins.php
admins.php is something like following
return [
'list' => [
'admin1',
'admin2'
],
'other settings' => true
];
Related
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'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 want to access session or Yii::$app in config file like ( config/main.php or config/main-local.php) !
Is that possible or not?
I want to check session and make some style available!
'components' => [
'assetManager' => [
'assetMap' => [
'r.css' => Yii::$app->session['lang'] ? 'css/styleltr.css' : 'css/stylertl.css' ,
],
],
or how do something like that in assetmanager????
You can't access user session in your config file. But, you could use a conditional in your layout and register different assets based on session values.
try accessing what you need this way
\Yii::$app->getAssetManager()->assetMap;
I am using Laravel. I have referenced my project name in several different places in my views.
I then decided to change the name of my application! So is there a global variable I can use instead and then I only have to change the name in one place next time?
I looked through the docs but couldn't see this feature..
Of course global variables exist in Laravel as well. However just because the exist it doesn't mean you should use them.
A much more suitable approach would be to store the name in a config file.
You can use app/config/app.php and just add a row:
return array(
'name' => 'FooBar',
// existing values ...
'debug' => false,
'url' => 'http://localhost',
// etc ...
);
Or create your very own config file. For example app/config/site.php
return array(
'name' => 'FooBar'
);
And you use it like this:
Config::get('app.name');
Or
Config::get('site.name');
See the documentation for more information
You can make use of View Composers
http://laravel.com/docs/4.2/responses#view-composers
I use to develop with Drupal. In Drupal there are global variables such as $user or $_view, so you can use these in different modules. I wonder how can I make something like this in Laravel, after the user log in, I can use the global $user in different controller.Except using session is there any other ways to implement this one? Thank you.
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 app/config directory. Let's call it constants.php
In there you have to return an array of config values.
return [
'langs' => [
'es' => 'www.domain.es',
'en' => 'www.domain.us'
// etc
]
];
And you can access them as follows
Config::get('constants.langs');
// or if you want a specific one
Config::get('constants.langs.en');
And you can set them as well
Config::set('foo.bar', 'test');