ziggy
ziggy copied to clipboard
CommandRouteGenerator Urls are different from BladeRouteGenerator
Hey,
I am using v1.4.6 and ran into a weird issue today. At first I was wondering why my route('login') would return a "https:" url, which had the effect that I could not login or logout, etc on my local dev system without https.
Then I started digging around what would be the issue because as far as I can see ziggy pretends to use the app.url for route generation. For testing purposes I changed my app.url to xxx.test and it seems this is only reflected in the ziggy.js created by the CommandRouteGenerator. The BladeRouteGenerator in the directive still returns "patdbv2.test" as url and I have no clue where this even comes from, its not present in my .env file, cache is cleared etc.
Anyone ran into a similar issue?
Edit:
when I explicitly use the app.url via an the api endpoint like documented it works
Route::get('ziggy', fn () => response()->json(new Ziggy(null, config('app.url'))));
just calling the class will revert to something else
Route::get('ziggy', fn () => response()->json(new Ziggy));
Edit 2: it seems the change with app.url was reverted a while ago. So this make a little bit more sense now, but the question remains why does it generate https urls when https is not used / available and why is the result of both generators different?
The generated JS file and the JS in the browser are different because the console command uses your APP_URL
environment variable and the Blade directive uses the current request URL.
I'm not sure about the http/https thing though... what is your APP_URL
and what is the actual full URL in your browser on that page? Are you serving your site with Valet?
Thanks for the reply. Yes I am using valet without https for this site and my app url is just "xxx.test" (as shown in the Screenshot) for testing around with this issue, the url in the browser is "http://patdbv2.test". So even the console command is turning my app.url into https.
Your APP_URL
environment variable is set to exactly xxx.test
? Does setting it to http://xxx.test
fix this? Run php artisan view:clear
too and check.
Yeah it was set for testing purposes to http://xxx.test. Given patdbv2.test as app_url etc like the default setup its the same:
Sorry just to be super clear—does your APP_URL
environment variable include http://
? It's a complete, valid URL, not just a domain?
yes it is, please have a look at my screenshot, I added everything in there.
Sorry, completely missed that 🤦🏻 thanks.
What does config('app.url')
display if you echo it from that Blade template or run it in Tinker?
Basically the same as above:
I'm having the exact same problem, I cannot get the URLs to change from HTTP to https when I'm deploying my application to production, even though I have it working as https locally.... but locally I can't change the URL at all either.
Any updates on this? Any ideas how to get ziggy to recognise the correct URL with https?
@coatesjonathan as a workaround you could "upgrade" your request to https via the Webserver and leave the urls untouched with http
I'll spend some time looking into this tomorrow.
@twdnhfr can you share your original screenshot with the app URL and routes Blade directive with one additional line, url('/')
? Thanks.
@coatesjonathan in production, are you using any kind of proxy or load balancer? If so you'll need to configure your trusted proxies to get Ziggy to recognize that and use HTTPS, see https://github.com/tighten/ziggy/issues/410#issuecomment-814485042.
@twdnhfr I can't reproduce any part of this locally, any additional info you can provide would be great—any other relevant environment variables or configurations, etc. Other questions I can think of (some of them are basic I know but I'm pretty stumped here):
- Is the entire, exact command you're running to generate the file
php artisan ziggy:generate
? No arguments or shell aliases? - Are you setting any
ziggy.*
config values anywhere (in aconfig/ziggy.php
file, a service provider, a middleware, etc)? - What exactly is the
'url'
value being set to in yourconfig/app.php
? Not the config value itself but the code on that line in the file. - Does the issue persist if you restart Valet and clear your route cache, and view cache, and app cache?
- Were you previously using Valet to serve this same site with HTTPS, and you changed it to HTTP?
- Are you doing anything to the app or request URL in a middleware that could possibly be causing this?
- Do you only see this locally, or in any other environment too? Any other sites?
- Did this start happening after your upgraded to a specific version of Ziggy? If you downgrade does that fix it?
Of course, if you're able to provide an example repo or reproduce this in a fresh site that would be even better 😂