Cannot access environment variable during jQuery AJAX POST - laravel

In my .env file I have a custom entry SECRET_FILE_NAME=mysecretfile.txt. Somewhere in my controller I need to access mysecretfile.txt, and I obtain its path as follows:
base_path(env('SECRET_FILE_NAME'))
This returns the correct path:
"C:\laragon\www\myproject\mysecretfile.txt"
However, during an AJAX POST call the result is
"C:\laragon\www\myproject"
It turns out that env('SECRET_FILE_NAME') returns "mysecretfile.txt" during a regular POST (tested through Postman Chrome extension), but it returns null during AJAX POST.
How is it possible? Both are POST-ing to the same URL, same controller action. Is this a caching issue? I tried php artisan config:clear but nothing changed.
On a side note, do you think it's ok to access ENV variables directly from .env file? Or should I instead put them in app/config/services.php and access them though
config('services.filenames.mysecretfile')
Thanks!
EDIT
With the help of #shalvah, I figured that putting a reference to the environment variable in config/services.php fixed the problem:
'secret_filenames' => [
'my_secret_file' => env('SECRET_FILE_NAME')
],
The entry still remains in the .env file: SECRET_FILE_NAME=mysecretfile.txt. But now I can use it as follows:
config('services.secret_filenames.my_secret_file')
This solves my problem; however I am curious why calling env('SECRET_FILE_NAME') directly results in a null value? Is this caching issue? Also, does it make sense to store this kind of info in services.php?
EDIT 2
Just stumbled upon a discussion on Laracasts that might shed some light on this.

Related

i cant run php artisan make:controller FallbackController

It gives me this error
ArgumentCountError
Too few arguments to function Illuminate\Routing\Router::fallback(), 0 passed in C:\xampp\htdocs\gmvcc\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php on line 338 and exactly 1 expected
im using laravel 9.
I suggest a combination of suggested places to check, based on the provided error response you pasted above:
First, check your web.php or api.php in routes folder and verify if you have unexpected additional parameters. For example, Route::post('/foo/{hello}/bar/{world}', [FallbackController::class, 'testFunction']); and we see that you'll next have to declared $hello and $world shortly.
Next, does your function looks like this? (following the example from #1) Such as public function testFunction($hello, $world, Request $request).
Third, based on the HTTP method used, and for the route defined using FallbackController, were you able to simplify it to test your HTTP method with the route again? For this, I still assume it is a route in api.php correct?
Lastly, if any of above fails to help, I recommend implementing a simple route to test with (GET or POST) first. This will help you trace back what you missed.
Using Docker (and a fresh Laravel 9) to help fill in other clues, like incorrect php or composer version and more - using a template like this might help, https://github.com/k90mirzaei/laravel9-docker.
Please note, I am answering based on the provided error first. Hope my checklist above helps.

Why can't Laravel access .env vars after four seconds of load time

My Laravel site uses SoapClient to access another site during an page load (with performs about 6 seconds of data processing before the soap call.) I noticed sometimes the SoapClient switches to non-wsdl mode and the process errors out. I discovered this was happening because the SoapClient was passed a NULL for its first constructor parameter (the URI of the WSDL file). I though this was strange, because this value came directly from the .env file. The site was having no trouble connecting to the database, so the .env file had to be working. I set up a function that access .env variables repeatedly during the page load, using env(...). During a Soap error, I discovered around the four second mark, the site lost access to the .env vars. Before that point, the information was accessible. After that point, calls to env() returned NULL. This may be related to other page request (possibly repeat calls to the same page, requesting the same process.) Also, I just upgraded php to 7.4.13 (xampp with 64 bit thread support: php-7.4.13-Win32-vc15-x64.) Has anyone seen this before, and has a way to address this issue?
EDIT ====
The SoapClient was created in a model, and the env() function was used to access the environmental vars. I have learned that env() should not be used anywhere but config files. This may explain my problem.
I have never seen this problem. But an approach might be to load env variable into a config variable and use that instead. For example: create extra.php file in the config directory like this:
<?php
return [
'api_url' => env('API_URL'),
];
And use it like this:
config('extra.api_url');
// Instead of env('API_URL')
Hope it works.

Replace localhost with my domain in Laravel when in production

I am using SPA (Laravel and Vuejs). When in development, website project worked perfect but when after running npm run production and put it on live server, I got errors saying
Access to Xmlhttprequest at http://localhost:8000/auth_check from origin https://hamariweb.com/auth_check has been blocked by Cors policy.
I have tried different solutions that fixed their problems but not mine. I want to know what is wrong with it and how can I fix it?
Sounds like somewhere in your code you're trying to send a request to http://localhost:8000/auth_check which isn't going to work in prod. You need to find the call to that URL and replace it with a call to the correct URL.
You can create an ENV setting in your .env file like this.
APP_URL=https://hamariweb.com
Then share that env file to your javascript code like this.
MIX_APP_URL="${APP_URL}"
Any ENV settings that start with MIX_ are passed to Javascript and this is passing it the initial APP URL value.
To finally grab the app url in the JS do this:
process.env.MIX_APP_URL
You can even assign the entire env settings globally like this.
if(process && process.env){
window.env = process.env;
}
Any .env MIX_ variables will be available in window.env at that point and in your SPA you'll be able to make the URL or any other variable configurable based on environment.

Laravel's pretty var_dump(), dd(), not working anymore

For some reason laravel's dd() function decided to stop functioning. I have no idea how this happened. I tried composer update already but I'm not sure what else can be going on. The debug key is set to true in the config.
Where should I look to solve this problem? I'm using Laravel 4.2.16
NOTE:
dd() now simply functions as var_dump(), it doesn't prettify it anymore
Solved it. I loaded my vagrant machine with the wrong config and was running hhvm instead of regular php-fpm. Hence xdebug, which handles the pretty dd(), was not being loaded. I reloaded my box with the correct settings (without hhvm and hack) and everything started working again
Is dd the only Laravel helper function that doesn't work? If not then check the contents of vendor/composer/autoload_files.php. The laravel support helpers are loaded here. If the other helper functions are fine you can check the contents of laravel/framework/src/Illuminate/Support/helpers.php to see what's going on - this is where the function is created.

How to get the primary route, if it doesn't exist and we are being sent to 404 page

So basically I am trying to debug my routes, because they are not working as intended, but when using the profiler, I can see the URI string, which is basically the second part of URL in the browser address bar and CLASS/METHOD which are always of the 404 page that I am being redirected to. So how can I get the primary routes Class, Method and arguments/parameters that were attempted to run before being sent to 404?
E.g.
$route['en/catalog/(.+)/(.+)'] = "ccatalog/index/$1/$2";
something's gone wrong and I get redirected to the 404, but I want to see which class (most likely "ccatalog" here), which method (hopefully "index") and arguments ($1, $2).
Thank you in advance to anyone who could help me with my problem!
I don't see a reason for your route to not work.
Check by directly opening your_path/ccatalog/index/whatever/whatever in the browser.
If it gives you a 404, it means the problem is with your controller, maybe the controller or function naming.
If it is working fine, then you may be able to use a pre_system hook to figure out the parameter values.
You may also consider hacking around with Routing files in the core(making sure you change them back), to figure out what the real issue is.
Actually, this was done really easy:
$this->uri->rsegment(1);

Resources