Laravel 5.1 ignores any values inside env() - php

I am uisn glravel 5.1 and setting up mail service with Mailgun. I've just found that my services file contains lines like the following:
'mailgun' => [
'domain' => env('<domain>'),
'secret' => env('<key>'),
],
Now for some reason, these values get ignored as-is. However, if I remove the env() method from the above, it works. So now I have this:
'mailgun' => [
'domain' => '<domain>',
'secret' => '<key>',
],
Can anyone explain why this is?

Because by
env('foo');
You are asking for the content of the "foo" constant defined in the .env file. Do you have a constant in your .env file named 'foo'?

Related

Using EC2 metadata credentials in the Laravel filesystem

How do I tell the Laravel filesystem layer to use the s3 metadata on an EC2 instance? I don't want to provide hardcoded keys and secrets for my s3 buckets. I'm unclear on what the configuration should look like. When I exclude the key and secret from the filesystem configuration I get the following error
ErrorException
Undefined index: key
The fix is to leave empty placeholder values in place for key and secret. eg, in config/filesystems.php
return [
'cloud' => 's3',
'disks' => [
's3' => [
'driver' => 's3',
'key' => '',
'secret' => '',
'region' => env('S3_REGION'),
'bucket' => env('S3_BUCKET'),
],
],
];
The correct way to provide your credentials is by using the .env file.
In your .env file, add something like that:
EC2_SECRET=your_ec2_secret
EC3_KEY=your_ec2_key
and in the `` config file, use something like that:
'ec2' => [
...
'key' => env('EC2_SECRET'),
'secret' => env('EC3_KEY'),
],
You should now be able to use the service without having the credentials stored in the repository.

Is it possible to define a constant in app.php and use it in filesystems.php in laravel?

I want to define some of my configuration in one file.
I want to put in config/app.php define("PATH", "path/to/uploaded/files");
and use it in config/filesystems
'local' => [
'driver' => 'local',
'root' => PATH,
],
It's a bad idea to put this data into app.php and then use it in filesystems.php. You should add PATH to the .env file, because PATH will be different for each machine:
PATH=path/to/uploaded/files
And then use this variable in the filesystems.php:
'local' => [
'driver' => 'local',
'root' => env('PATH'),
],
Certainly you can , in your config/filesystems just include /config/app.php, then you can use variables defied in config/app.php like they are in config/filesystems.

Error when installed Yii2

I've install Yii2 framework using composer but get this error in my browser (on localhost):
Invalid Configuration – yii\base\InvalidConfigException
yii\web\Request::cookieValidationKey must be configured with a secret key.
How can I solve this problem?
There is this problem with basic app now https://github.com/yiisoft/yii2-app-basic/issues/69 where composer install doesn't generate this key.
You need to add this key manually.
Go to /config/web.php.
Edit the line 'cookieValidationKey' => '', to include random string (you can use anything like 'cookieValidationKey' => 'jfsbkjsbfdskjgfdskjbgfsdhjgfajds',
You need to set cookieValidationKey in the config file to a random string. The config file is located under yii/your-projectfolder/config/main-local.php if you are using Yii 2.0 Advanced Template
You need to set cookieValidationKey value in project/config/web.php at line 12.
change at:
'cookieValidationKey' => '',
replace with:
'cookieValidationKey' => 'setyourkey',
That should address the issue.
Try this
open Frontend/ config / main.php
'components' => [
'request' => [
'enableCookieValidation' => true,
'enableCsrfValidation' => true,
// 'cookieValidationKey' => 'xxxxxxx', // if u dont hv key just comment it
],
],
if you have a web.php
'components' => [
'request' => [
'enableCookieValidation' => true,
'cookieValidationKey' => 'your-validation-key',
],

Lumen Authentication

Just can't get the Lumen authentication to work at all.
I have a fresh install and trying to follow the docs here:
https://lumen.laravel.com/docs/5.2/authentication
I've Uncommented the AuthProvider line in the app.php file (along with everything else, facade, etc). Then in a simple controller I just do dd(Auth::use()).
I just can't get around this error:
Undefined index: provider
in AuthManager.php line 152
at Application->Laravel\Lumen\Concerns\{closure}('8', 'Undefined index: provider', '/home/vagrant/Code/gryd/api.gryd.com/vendor/illuminate/auth/AuthManager.php', '152', array('name' => 'api', 'config' => array('driver' => 'token'))) in AuthManager.php line 152
Any ideas?
EDIT:
Since someone asked for a code sample.
Install Lumen
Uncomment everything in app.php
Put this in routes:
$app->get('/api/v1/users/{id}', function () {
dd(\Auth::user());
});
This is what I've got so far, which is working but not quite how I'd like it. The following works for Token-based auth, which is the default setting in Lumen.
Enable Authentication
Register routeMiddleware and AuthServiceProvider by un-commenting the following lines in bootstrap/app.php.
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
]);
and
$app->register(App\Providers\AuthServiceProvider::class);
Configuration
Copy vendor/laravel/lumen-framework/config/auth.php to config/auth.php. Create the root config folder if you have to.
Inside we will find four items (defaults, guards, providers, passwords). We're concerned with the first three.
First we name the default guard as ABC.
'defaults' => [
'guard' => env('AUTH_GUARD', 'ABC'),
],
Next we define the ABC guard with token as its driver and XYZ as its provider.
'guards' => [
'ABC' => [
'driver' => 'token',
'provider' => 'XYZ'
],
],
And the XYZ provider is defined with eloquent as the driver and App\User::class as the model.
'providers' => [
'XYZ' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
Completing Setup
Finally, we use the auth middleware in our routing setup, as usual.
$app->group(['middleware' => 'auth'], function () use ($app) {
So this is what gets the token auth up and running. It uses the api_token field in the users table to authenticate, which can be found in TokenGuard.
I still haven't found out what effect AuthServiceProvider and $this->app['auth']->viaRequest('api', function ($request) { have on my app yet.
Well I still haven't found out how to change the api request type via .env. But for now switching it to token seems to work.
Changed Auth::viaRequest('api', functi to Auth::viaRequest('token', funct.

Laravel 5 accessing global variable in mail.php

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

Categories