dump($_ENV) not working in laravel version 7x - laravel

Here the code
Controller :
public function index()
{
dump($_ENV);
}
Route :
Route::get('/', 'HomeController#index');
I can't display env file in the screen

Try:
php artisan config:clear
php artisan cache:clear
composer dump-autoload
chek:
{{ env('APP.ENV') }}

your doing things in wrong way try this
env('APP_ENV');
getenv('APP_ENV');
if your using cache then you can access the value from config like this .
config('app.env');
or using facede
Config::get('app.env');

If your $_ENV array is mysteriously empty, but you still see the variables when calling getenv() or in your phpinfo(), check your http://us.php.net/manual/en/ini.core.php#ini.variables-order ini setting to ensure it includes "E" in the string.

Related

Some URLs are 404 in production

I have a Laravel 7 project that I deploy using Deployer and everything works as expected. A few days later, I created a new route, /privacy-policy. I also created a controller and a Blade template. Everything seemed to be working great. However, upon deployment, surprisingly there was a 404 error. All the other links are working fine.
Route
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/privacy-policy', 'HomeController#privacy');
Controller
public function privacy()
{
return view('app.privacy-policy');
}
Even a closure doesn't work.
Route::get('/', function () {
return view('app.privacy-policy');
});
I ran the following commands, but it didn't help.
php artisan cache:clear
php artisan route:clear
php artisan config:clear
php artisan config:cache
Why don't you run php artisan route:list so that you can verify the URI and name are actually correct? For instance if you see 'app.privacy-policy' in the URI column then verify that it's named that and also in your controller. Basically fix your route.
Route::get('app.privacy-policy', 'HomeController#privacy')
->name('app.privacy-policy');
public function privacy()
{
return view('app.privacy-policy');
}

Mixed Content, This request has been blocked; the content must be served over HTTPS , Laravel

I have this error one production env. only on some routes, I see no difference between the routes that works and the few of them that don't work. All my routes are in web.php, and in front I try to access via Vuejs/Axios.
Also, the both url from error when I try to access them I get to https://, even if I try http:// , I get redirect to https.
What I tried till now.
.env
APP_URL=https://my.url
web.php
URL::forceScheme('https'); //at the top of the file
App/Providers/AppServiceProvider
public function boot()
{
URL::forceScheme('https');
}
Also:
composer dump-autoload
php artisan cache:clear
php artisan config:clear
php artisan view:clear
php artisan route:clear
php artisan config:cache
npm run prod
I found a strange solution.
I changed the route name from random_question to random, also the controller method name that was show to random and works.

Laravel - accessing .env variables

I tried to get the environment variable from the .env in my root with
Route::get('/test', function () {
return "value is". getenv('APP_ENV');
});
and
Route::get('/test', function () {
return "it is". env('APP_ENV');
});
It is in .env
APP_NAME=Laravel
APP_ENV=local
How can I get access to it?
With Laravel, you should avoid environmental variables outside of your configuration files.
In your config files, you can use environmental variables, example in config/app.php:
'env' => env('APP_ENV', 'production'),
Then you can access this using the config helper: config('app.env').
This allows you to cache your configuration and still access these values, since env('APP_ENV') will no longer work once your config is cached.
Route::get('/test', function () {
return "it is".config('app.name');
});
use env('ENVKEY')
Don't forget to clear cache sometime it cause because of cache.
php artisan config:clear
php artisan cache:clear
composer dump-autoload
For more info look at the doc
just run this commands in cmd.
php artisan config:cache
then
php artisan config:clear
then
php artisan cache:clear
As per the Laravel Documentation on Environment Configuration,
All of the variables listed in this file will be loaded into the $_ENV
PHP super-global when your application receives a request. You may use
the env helper to retrieve values from these variables.
So, it is possible to access the variable as
$_ENV['envKey'];
laravel provides a global helper function for this kind of task
$val = config('app.something');
you can also set new values with the following method
config(['app.something' => 'cat']);
reference
for your particular task it would be
$val = config('app.env');
or to determine the env globally
$environment = App::environment();
i hope this helps, have a nice one!
App::environment()
try this please

Laravel 5.6 env('APP_BASE_URL') returns null

In staging and production environment, when I try to get my custom defined variable from .env file, it returns null. I have tried creating new Key for APP and cleared all sort of caches, but result is same.
APP_NAME="App - Staging"
APP_ENV=staging
APP_KEY=<HIDDEN_KEY>
APP_DEBUG=true
APP_LOG_LEVEL=none
APP_URL=https://staging.app.com
APP_BASE_URL="https://app-staging.app.com"
Clearing Cache
php artisan config:clear
php artisan cache:clear
php artisan route:clear
php artisan view:clear
php artisan config:cache
But when using following in blade view gets nothing
env('APP_BASE_URL') returns null
It is because you have run php artisan config:cache. If you are using config:cache, your env() calls should only be made in your config files.
See here: https://laravel.com/docs/5.6/configuration#configuration-caching
This post solved my issue, I defined custom config in config/app.php
https://laracasts.com/discuss/channels/laravel/accessing-custom-environment-variable
/*
|-------------------------------------------------------------------------
| Custom config variables
|-----------
|
*/
'base_url' => env('APP_BASE_URL', 'http://localhost'),
Then in .env I definded:
APP_BASE_URL="https://app-staging.app.com"
Finally cleared the cache, worked.
In blade view
{{ config('app.base_url') }}
Yes it will return only null
If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.
See the laravel documentation https://laravel.com/docs/5.6/configuration#configuration-caching

Laravel 5.2.2 and Entrust error call to undefined method

When I am using latest Laravel 5.2.2 and Entrust ("zizaco/entrust": "5.2.x-dev") I face this error and not sure how to solve this:
Call to undefined method Zizaco\Entrust\EntrustServiceProvider::hasRole()
I tested this code on HomeController.php
use Entrust;
class HomeController extends Controller
{
public function index()
{
if (Entrust::hasRole('admin')) {
echo "string";
}
return view('home');
}
}
This is my config/app.php service provider
Zizaco\Entrust\EntrustServiceProvider::class
config/app.php facade alias
'Entrust' => Zizaco\Entrust\EntrustFacade::class
I also already generate the model needed
Did I miss something here?
I have same issue, here are the steps I have taken to solve the issue
In your .env file change to cache array
CACHE_DRIVER=array
and dont forget to run
php artisan config:cache
It seems all the steps is correct, and I just need to clear the cache with php artisan config:cache
And if you face an error like below
BadMethodCallException in vendor\laravel\framework\src\Illuminate\Cache\Repository.php line 380:
This cache store does not support tagging.
You need to change in .env this line to array
CACHE_DRIVER=array
Try this:
Open environment file of your laravel change CACHE_DRIVER=file to CACHE_DRIVER=array and save.
Now try your CLI command.
Laravel drivers does not support tagging. To solve this, go to your .env file and change
Cache_driver=file
to
Cache_driver=array
and run
php artisan config:cache

Resources