I need to set admin email in many place. so I created constants.php in config folder.
<?php
return array(
'admin_email' =>'joe#doe.com',
'admin_name' =>'Admin',
);
I was able to access this in my routes.php
dd(Config::get('constants.admin_email'));
However, when I try to access it in mail.php by
'from' => [
'address' => Config::get('constants.admin_email'),
'name' => Config::get('constants.admin_name')
],
I got Class 'Config' not found in mail.php.
Any suggestions? Thanks.
After some testing, I've found you can't use Config, \Config or config() in any files in your config folder. I believe they are not available to any of these files, but I'm not 100% sure why this is.
Regardless, to solve this issue and still have them available in other parts of your application, use env or environment variables. In your .env file, add the following:
ADMIN_EMAIL=joe#doe.com
ADMIN_NAME=Admin
Then, in your mail.php and anywhere else you want to use them, access them using:
'from' => [
'address' => env('ADMIN_EMAIL'),
'name' => env('ADMIN_NAME')
],
You can actually see them already in use in your mail.php and other config files, so it makes sense to use what already works. Hope that helps!
Use
config('constants.admin_email');
Related
I have made a config test.php
return [
'name' => 'Testname',
'street' => 'Teststraße',
'street_number' => '69',
'zip' => '42077',
'city' => 'Winterfell',
'telephone' => '0123456789',
'email' => 'hsw#hsw.hsw'
];
created a ConfigServiceProvider:
public function register()
{
config([
'config/test.php'
]);
}
but in my test.blade.php I can access the contents only via
{{config('test.0.name')}}
There has got to be a better, easier way for this right? The data is supposed to be used in multiple blades. Yet the ".0." feels so unnecessary.
I am new to PHP and Laravel, and I am using PHP 8.1 and Laravel 9.14.
Thanks in advance!
#######################
Edit: In my case I had the problem, that my cache wasn't cleared "php artisan config:cache" thanks to #matheenulla for hinting at that!. Thats why I tried the route with the ServiceProvider. I hope this may help someone in the future!
You don't need to create a service provider to use your config, unless you want it to be in some other location (in case you are creating a custom Laravel package), so you may delete your ConfigServiceProvider. In fact, you're just using config wrong:
{{config('test.0.name')}}
should be:
{{config('test.name')}}
I need to get address and name variables on my Mail.php config:
'from' => ['address' => env('MAIL_USERNAME'), 'name' => env('MAIL_NAME')],
I tried this:
Config::get('mail.from.*.name');
Config::get('mail.from["name"]');
But it doesn't work, what do I do to get them? Thanks.
I think you can directly access MAIL_NAME from .env
env('MAIL_NAME');
OR
Config::get('mail.from.name');
Hope this work for you !!!
this is the shortest method i know using helper methods :
config('mail.from.name')
or you can access it using your .env file with env('MAIL_NAME')
it is preferable put your mail name in your configuration file and access it with env function like this :
env('MAIL_NAME')
I'm having trouble understanding how combine&compress of Yii2 assets work. I read the section in the guide (http://www.yiiframework.com/doc-2.0/guide-structure-assets.html) still I cannot make it work or at least understand the very basics of this matter.
From the example given there I am not able to reproduce that and solve the following scenario.
AppAsset contains the main css/js for the application. Shared and essential code is supposed to be included here. This one depends on another file called ExternalAssets which in turn depends on YiiAsset, BootstrapAsset and so forth.
CheckoutAsset contains code only relevant to the checkout process. Should be included as well with the above because it add custom functionality but only relevant to this section. $depends on AppAsset.
ProductViewAsset is the same as above but related to the visualization of a given product.
The asset configuration is given:
'bundles' => [
'yii\web\JqueryAsset',
'rmrevin\yii\fontawesome\AssetBundle',
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'yii\bootstrap\BootstrapPluginAsset',
'yii\widgets\ActiveFormAsset',
'frontend\assets\ExternalAssets',
'frontend\assets\AppAsset',
],
'targets' => [
'app' => [
'class' => 'yii\web\AssetBundle',
'basePath' => '#webroot/assets',
'baseUrl' => '#web/assets',
'js' => 'js/app-{hash}.js',
'css' => 'css/app-{hash}.css',
'depends' => [ ],
],
]
At this point I'm able to compress everything that is supposed to be common (although it maybe possible to strip some unnecessary files) but in the pages that use the ProductViewAsset or the CheckoutAsset the files included there do not get compressed in the same fashion.
Maybe it is possible to compress everything to a single file in the checkout process (common data + checkout data) or maybe not due to caching reasons. Don't know if this is possible yet I am unable to make it work. This is as far as I got with this. I tried doing this because it seemed natural but I started having errors like this one PHP Notice 'yii\base\ErrorException' with message 'Undefined index: frontend\assets\ExternalAssets' when trying to make different asset bundle groups:
'targets' => [
'app' => [
'class' => 'yii\web\AssetBundle',
'basePath' => '#webroot/assets',
'baseUrl' => '#web/assets',
'js' => 'js/app-{hash}.js',
'css' => 'css/app-{hash}.css',
'depends' => [
// 'frontend\assets\ExternalAssets'
],
'checkout' => [
'class' => 'yii\web\AssetBundle',
'basePath' => '#webroot/assets',
'baseUrl' => '#web/assets',
'js' => 'js/checkout-{hash}.js',
'css' => 'css/checkout-{hash}.css',
'depends' => [
'frontend\assets\AppAssets'
],
],
Maybe I'm looking at the wrong approach here but I'm not able to quite figure out what is the best way of doing this. I think that compressing everything to a single file should work better than not combine&compress the files yet I think it is possible to optimize the combine&compression approach in this case.
Thanks!
I am not sure if i understand the question you are giving, this is what i interpret as your requirements.
Make a normal action in your site controller calling a view 'site/assets/'
public function actionAssets()
{
return $this->render('assets');
}
Arrange your view like this example -
<?php
use dmstr\web\AdminLteAsset;
use backend\assets\AppAsset;
use dosamigos\fileupload\FileUploadPlusAsset;
use dosamigos\fileupload\FileUploadAsset;
use dosamigos\gallery\GalleryAsset;
use dosamigos\fileupload\FileUploadUIAsset;
use dosamigos\gallery\DosamigosAsset;
use budyaga\cropper\assets\CropperAsset;
use kartik\base\Html5InputAsset;
use kartik\base\WidgetAsset;
use kartik\form\ActiveFormAsset;
use kartik\affix\AffixAsset;
use kartik\alert\AlertAsset;
use kartik\color\ColorInputAsset;
use kartik\date\DatePickerAsset;
use kartik\datetime\DateTimePickerAsset;
use kartik\depdrop\DepDropAsset;
use kartik\depdrop\DepDropExtAsset;
use kartik\file\FileInputThemeAsset;
use kartik\growl\GrowlAsset;
use kartik\base\AnimateAsset;
use kartik\select2\Select2Asset;
use kartik\sidenav\SideNavAsset;
use kartik\spinner\SpinnerAsset;
use kartik\switchinput\SwitchInputAsset;
use kartik\time\TimePickerAsset;
use kartik\touchspin\TouchSpinAsset;
use kartik\typeahead\TypeaheadAsset;
use kartik\grid\GridViewAsset;
use kartik\dialog\DialogAsset;
$view = $this;
AdminLteAsset::register($this);
AppAsset::register($this);
FileUploadPlusAsset::register($view);
FileUploadAsset::register($view);
GalleryAsset::register($view);
FileUploadUIAsset::register($view);
DosamigosAsset::register($view);
CropperAsset::register($view);
Html5InputAsset::register($view);
WidgetAsset::register($view);
ActiveFormAsset::register($view);
AffixAsset::register($this);
AlertAsset::register($view);
ColorInputAsset::register($view);
DatePickerAsset::register($view);
DateTimePickerAsset::register($view);
DepDropAsset::register($view);
DepDropExtAsset::register($view);
FileInputThemeAsset::register($view);
GrowlAsset::register($view);
AnimateAsset::register($view);
Select2Asset::register($view);
SideNavAsset::register($this);
SpinnerAsset::register($view);
SwitchInputAsset::register($view);
TimePickerAsset::register($view);
TouchSpinAsset::register($view);
TypeaheadAsset::register($view);
GridViewAsset::register($view);
DialogAsset::register($view);
?>
<?php $this->beginPage() ?>
<?php $this->beginBody() ?>
<p>Done</p>
<?php $this->endBody() ?>
<?php $this->endPage() ?>
Then make sure to hit this link before you do a deployment to prod to recompile assets.
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 wrote an API using Yii2 and following the REST guide. My API is working and I want to write some tests for it, so I once again followed the guide on how to run tests and got unit tests working. I then looked around Codeception documentation about testing WebServices and got this working too.
My problem is that API calls are not using my test database. I have two databases, one called db and the other testdb. Here is my config.php file in tests/codeception/config/:
return [
'components' => [
'db' => [
'dsn' => 'mysql:host=localhost;port=8889;dbname=testdb;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock',
],
'mailer' => [
'useFileTransport' => true,
],
'urlManager' => [
'showScriptName' => true,
],
],
];
I wrote a simple test that send a GET request to an endpoint to retrieve data. My test database is empty so I am expecting to receive an empty response, but I get the content of my other database instead.
I then tried to set YII_ENV to test as described in the Environment Constant section here so that I could test against the env variable YII_ENV_TEST and change the db configuration accordingly. I tried to set this variable in the _bootstrap.php file in the tests/codeception/ folder:
defined('YII_ENV') or define('YII_ENV', 'test');
I then logged the value of YII_ENV in the web/index.php file (index-test.php is not called, might be a problem too), and it is undefined.
What am I doing wrong? I tried including the Yii2 module in my api.suite.yml file but requests don't have return code anymore if I do that, it returns N/A. Is there another way to change which database Yii should use?
You can make an test_config.php file and at the end of the config place this
if (file_exists('protected/config/test_config.php'))
{
include 'test_config.php';
}
the file will be included if it exists. And the file test_config.php should contain the overwritten value for the db connection.
Hope this helps!
Keep on coding!
Ares.
Well I found a "solution" by using this other app template: https://github.com/githubjeka/yii2-rest
The file organization fits my needs better and I can easily configure which database to use.