Laravel env() value null - laravel

I'am trying to retrieve EXAMPLE_URL=www.google.com from .env in controller and everytime get null. Where is the problem because the same code works on another application. This function doesn't work after php artisan cache:clear.
Controller code
$hostname = env("EXAMPLE_URL");
dump($hostname);

You should not use env() outside of the config files.
Read: https://laravel.com/docs/8.x/configuration
You should add the env variable to a config file and use config('example.url');.
The example.php would look like:
return [
'url' => env('EXAMPLE_URL', 'https://example.com'),
];

Always use the app file as the middleman
see : https://laravel.com/docs/9.x/configuration

Related

Laravel, call Artisan from php differs from command line

I have a route which calls the Artisan facade to execute:
Artisan::call('queue:work --once');
And I get :
But it's strange because in the command line, if i do:
php artisan queue:work --once
Works everything ok:
I can use other routes to call for example:
Artisan::call('config:clear');
And works ok too. Any idea?
the options value staring with -- are not passed to the string
you can try :
Artisan::call('queue:work', ['--once' => true]);
Laravel 5.8 introduced this new method of calling artisan commands:
Artisan::call('queue:work --once');
In previous releases use this:
Artisan::call('queue:work', ['--once' => true]);
to call an artisan command from the code and passing some options you need to use an array as a 2nd argument to Artisan::call()
like so :
Artisan::call('queue:work', ['--once' => true]); // or whatever options you need

How to use ENV variables in Artisan Commands in Laravel 5.8?

I am creating my own artisan command and I want to use ENV variables, but when I use $_ENV['VariableName'] I get and error.
local.ERROR: Undefined index: VariableName
The same code works perfectly in a controller and error as this one is not being generated.
I am creating my commands with php artisan make:command CommandName
How can I start using ENV variables there? Thank you! I want to use the variables in a private function which is inside:
class CommandName extends Command but outside the public function handle()
Since the .env file is not pushed to the repository, the best approach is to use config files instead. So in the config directory, create your custom file for example: custom.php with the following content:
<?php
return [
'variable' => env('VARIABLE_NAME', 'DEFAULT_VALUE')
];
and in your .env you should put:
VARIABLE_NAME=something
Then to use it you use config('custom.variable');
You can use the Laravel Helper to access environment variables with something like this:
env('VariableName')
you can also specify a default value if the environment variable is not set
env('VariableName', 'myName')
Laravel Docs 5.8 Helpers

Laravel page speed - skip route

I'm using Laravel Page Speed.
This library create many problems with some styles in my admin pages and i want to skip that.
//config/laravel-page-speed.php
//You can use * as wildcard.
'skip' => [
'*.pdf', //Ignore all routes with final .pdf
'*/downloads/*',//Ignore all routes that contain 'downloads'
'assets/*', // Ignore all routes with the 'assets' prefix
];
But when i want skip my admin url, nothing changed
'skip' => [
'*/admin/*',
];
Do you have any idea about this problem?
Update: I also run these command after any change
php artisan config:cache
php artisan cache:clear
You have wrong syntax there. It should be like this :
'skip' => [
'admin\*'
];
Check this issue from Github :
https://github.com/renatomarinho/laravel-page-speed/issues/45

Artisan output not working for route:cache

Why doesn't this code show any output result?
$exitCode = \Artisan::call('route:cache');
$artisanOutput = \Artisan::output();
dd($artisanOutput);
Another artisan console code example shows a result, but this one does not work.
if your app environment is production then you should run
$exitCode = \Artisan::call('route:cache',['--force'=> true]);
$artisanOutput = \Artisan::output();
dd($artisanOutput);
Hope this helps
As per your comment i read. Redirect is what you are trying to achieve.
why not use this instead?
return redirect('adminSettings')->with('success', $successMessage);
And your route should look like this:
Route::get('adminSettings', 'AdminController#adminSettings');
Looking at your original question
$exitCode = \Artisan::call('route:cache');
$artisanOutput = \Artisan::output();
dd($artisanOutput);
Can you try 'php artisan route:cache' from your terminal? What is the output? I am using 5.6 and the output gives me some error. So i think that's the issue.

Php artisan - custom command

I would like to create my own php artisan command so when I write
php artisan env:[variable]
I want to get that variable, so I can check fast if I'm getting the right variable from .env file
Is this possible? How to do it?
You can create a console command with:
php artisan make:console
As per the docs.
You are probably better off creating a static signature and passing in the field you want to return.
protected $signature = 'env:return {field}';
This will allow you to type php artisan env:return APP_DEBUG.
You can retrieve the input like so:
public function handle()
{
$envVariable = $this->argument('field');
//
}
You can then write some code to output the field, perhaps something like:
$this->info(env($envVariable));
This could all be condensed down into something like the below, as an example only:
protected $signature = 'env:return {field}';
public function handle()
{
$this->info(env($this->argument('field')));
}
I would suggest you read the docs I linked above as this will help you understand what is happening here a lot more.
This should get you started though, and you can then shape it to be exactly what you need.

Resources