Access Laravel Homestead projects through other devices in three little steps
I guess there are several approaches for our goal, but I will share with you the easiest one today.
Xip.io is a free DNS service we will make use of.
It sends all requests with
xip.io at the end back to the IP address you provide in the URL.
Sounds complicately, but I promise it will make sense when you see an example!
Let's checkout a normal request like one to my site christoph-rumpel.com. The DNS provides an IP for this domain and the request is following the IP to my server. Because of my domain the server knows where to look for my files. This is why you see my site when you enter my domain.
With xip.io we are doing something similar, but we provide the IP we want the request to follow in the URL.
We say "Hey, go to this IP and tell the server this domain...". The URL looks like this:
Step 1 - Create the URL
Ok, let's start. First we need to put our URL together. We begin with our project url. All my local website URLs look similar. For this example we use my Laravel 5 Test project:
My URLs always start with
local and end with
app. Don't get confused by that. All your URLs will work. As I already mentioned we need to add our computer's IP address and xip.io to the end.
There are commands for every machine in order to get your
local IP address. I use the following for my Mac.
ipconfig getifaddr en0
:8000 is standard for Homestead projects. If you want to get rid of that, check out this nice little trick by Dries Vints.
Step 2 - Add a hosts entry
Now we got the correct URL but you will notice that this is not yet working. This is because our local machine does not know how to resolute the new domain. Let's add a hosts entry to solve that.
Now our created URL points to our virtual machine and there is only one more thing we need to do.
Step 3 - Configure Homestead
In our last step we connect the domain with the right Homestead project on our virtual machine. This should be familiar to you. It is like adding a new Homestead project. The only difference is that we connect it with the same project again in out Homestead.yaml file.
sites: - map: local.laravel5_tests.app to: /home/vagrant/websites/tests/laravel5_tests/public - map: local.laravel5_tests.app.10.0.1.11.xip.io to: /home/vagrant/websites/tests/laravel5_tests/public
Above we have the standard mapping for a project and an additional mapping for the new
local URL which will be available in your whole network. Let's reload our vagrant machine and see if it worked. With Homestead I needed to reload Vagrant with the provision flag in order to make changes in the configuration file work.
If the normal reload doesn't work for you too, try that one.
vagrant reload --provision
You will see that you are now able to access your local Homestead project through all you local devices with the new URL
http://local.laravel5_tests.app.10.0.1.11.xip.io:8000. This is perfect for testing on a mobile device. Have fun!