ziggy icon indicating copy to clipboard operation
ziggy copied to clipboard

CommandRouteGenerator Urls are different from BladeRouteGenerator

Open twdnhfr opened this issue 2 years ago • 15 comments

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.

Bildschirmfoto 2022-04-13 um 16 14 57

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?

twdnhfr avatar Apr 13 '22 14:04 twdnhfr

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?

bakerkretzmar avatar Apr 14 '22 13:04 bakerkretzmar

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.

twdnhfr avatar Apr 15 '22 19:04 twdnhfr

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.

bakerkretzmar avatar Apr 15 '22 20:04 bakerkretzmar

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:

ziggyTest

twdnhfr avatar Apr 19 '22 16:04 twdnhfr

Sorry just to be super clear—does your APP_URL environment variable include http://? It's a complete, valid URL, not just a domain?

bakerkretzmar avatar Apr 19 '22 18:04 bakerkretzmar

yes it is, please have a look at my screenshot, I added everything in there.

twdnhfr avatar Apr 19 '22 21:04 twdnhfr

Sorry, completely missed that 🤦🏻 thanks.

bakerkretzmar avatar Apr 19 '22 23:04 bakerkretzmar

What does config('app.url') display if you echo it from that Blade template or run it in Tinker?

bakerkretzmar avatar Apr 19 '22 23:04 bakerkretzmar

Basically the same as above:

Bildschirmfoto 2022-04-20 um 16 20 48

twdnhfr avatar Apr 20 '22 14:04 twdnhfr

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.

coatesjonathan avatar Apr 21 '22 09:04 coatesjonathan

Any updates on this? Any ideas how to get ziggy to recognise the correct URL with https?

coatesjonathan avatar Apr 21 '22 12:04 coatesjonathan

@coatesjonathan as a workaround you could "upgrade" your request to https via the Webserver and leave the urls untouched with http

twdnhfr avatar Apr 21 '22 12:04 twdnhfr

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.

bakerkretzmar avatar Apr 21 '22 13:04 bakerkretzmar

@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.

bakerkretzmar avatar Apr 22 '22 15:04 bakerkretzmar

@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 a config/ziggy.php file, a service provider, a middleware, etc)?
  • What exactly is the 'url' value being set to in your config/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 😂

bakerkretzmar avatar Apr 22 '22 16:04 bakerkretzmar