Sharing Kirby sites via Laravel Valet

Hi there!

As per suggestion here, I recently set up Laravel Valet as my local develop environment. This works great locally so far, but now I got curious about Laravel’s advertised sharing feature.

This feature allows you to run valet share on the command line and then a “publicly accessible URL will be inserted into your clipboard and is ready to paste directly into your browser” to share your site with the world, hosted from your local computer.

This generally works, however, it fails on all links set up via kirby. So it does not find any css files, js files or media files, as they all still link to Valet’s “local” link projectname.test/path/to/styles.css instead of the globally accessible link (which should look something like this I think: https://generatedprojectid.ngrok.io/path/to/styles.css. The panel (login) page does also not work, but I suspect this might be simply for the missing css files and such.

Does anybody know, if there is a kirby config setting or something similar that I can change to allow this setup to fully work?

Thank you!

1 Like

I tried this too once, and got the same result. Its basically a proxy i think so kirby cant see it as a domain. i think theres a way to tell kirby the name of the domain its running instead of letting guess. I forget what its called but you can try setting that in the config. Have look a look in the reference guide, its there somewhere.

I think I tried that once and didn’t succeed. Or rather, it works if you put the url option in the main config, but it would only makes sense if you were able to put it in the domain specific config, and there you go round in circles again, because the domain specific config is not recognised. Try it yourself, it’s a while ago that I tried it and gave up on it.

I think if you pay for Ngrok you get a fixed address to use. The freebie version that comes with valet dishes out a random temporary one each time, which makes it harder.

I guess the bullet proof way would be to get a fixed IP form you broadband provider and point that at whatever.test via your hosts file. But check your broadband providers terms, some dont like you running a server off domestic broadband.

Ive used DynDNS for this in the past.

What does that mean? The issue is that it has a different temporary address each time? I just tried the url option and it works indeed.

I have used the Valet share feature a few times with Kirby. What I did was run valet share then copy the Ngrok URL to the url config in Kirby. Then undo it after I’m done sharing.

2 Likes

Is there no way maybe to read out from which domain the site is accessed and then to dynamically write that into the Kirby structure and then serve the site with the correct links when the site is opened? Sorry, stupid question maybe, but I don’t know much about these things.

I played around a bit, and putting this in config.php worked for me in my Valet environment:

'url' => isset($_SERVER['HTTP_X_FORWARDED_HOST']) ?  'http://' . $_SERVER['HTTP_X_FORWARDED_HOST'] : 'http://your_localhost_domain',

Ideally, you would probably put this into your localhost config.

This even works with changing URLs.

8 Likes

Wow, this works beautifully, thank you!

By localhost config you mean a file site/config/config.localhost.php, right?
When I do this, then links within the page link back to the localhost address (i.e. a link to the home page does not link to 123abc.ngrok.io, but to projectname.test).

But if I put it in the main config file, it works, I am fine with that. I only need to remove it once, when I deploy the final page.

Edit: And the panel would not work either, when I put it in the localhost file.

No, if your local domain is for example http://kirby323.test then your local config file would be config.kirby323.test.php. That worked in my test.

1 Like

Ah, that makes a lot of sense. :wink:
And it works!

This is such a beautiful setup, I would almost recommed to include a litte paragraph about it on the Valet section of the local development environment page.

:heart:

Now, what do I do with all the time I save by switching from my old terminal localhost to Valet? :thinking:

Relax, go on a bike ride, party… Create more Kirby websites.

3 Likes

on a side note the docs say…

Valet+ is a third-party fork of Laravel Valet. The principle is the same as outlined above, but as its name suggest, Valet+ comes with some additional features like PHP version switching or Xdebug on/off mode, which you can check out in the documentation.

Looks like the standard official Valet now offers PHP switching, but Valet + still offers a bunch of useful extras (i use mailhog all the time for email testing). You just might want to clarify that paragraph slightly since it out of date now.

Well, that’s the trouble with having third party stuff in the docs. Would you mind creating an issue on GitHub, here this stuff will get forgotten?

Well, how goes the old saying?

He They, who found the error shall also prepare the pull request.

:wink:

(And yes, just kidding, I could also do it, when I am back at my main machine).

1 Like

Sure… and yeah docs are hard, everythings always changing… ill do it in the morning … sleepy time now :sleeping_bed:

Finding this here now while trying to edit in my panel from the couch instead of at the computer. Convenience drives solutions.

1 Like

Thanks, this only works for the frontend part though. I tried to login to the panel and I always end up with an invalid CSRF token message.

I cannot reproduce your issue, works for me, using Valet on a Mac.