I want to have routes like this:
john-doe.mysite.local
john-another-doe.mysite.local
and then pass the subdomain name to some controller@method and present the users profiles.
I'm using Laravel Homestead.
Here's my /etc/hosts
file from my local machine:
....
192.168.10.10 mysite.local
192.168.10.10 *.mysite.local
....
and on my virtual machine the /etc/nginx/sites-available/mysite.local
file
server {
listen 80;
server_name mysite.local *.mysite.local;
root "/home/vagrant/laravel-projects/mysite.com/public";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
....
If you need anything else please ask.
The main domain mysite.local
works normally.
I tried following the documentation an adding this to my routes.php
file:
Route::group(['domain' => '{element}.mysite.local'], function() {
Route::get('/', function ($element) {
dd($element);
});
});
But if I go to any subdomain somedomain.mysite.local
I get server not found.
It seems to me the problem is that I'm not even reaching Laravel app through the subdomain.
Any help is appreciated.
I've found this question, and tried changing .local
to .dev
in my /etc/hosts
and mysite.local
files but it didn't work. I also tried changing mysite.local
to mysite.dev
but it didn't work either.
I can't figure out what am I missing here.
UPDATE:
If I add test.mysite.local
to my /etc/hosts
file I get the same output in my browser as if I was to browse for mysite.local
.
....
192.168.10.10 mysite.local
192.168.10.10 test.mysite.local
192.168.10.10 *.mysite.local
....
Updating the mysite.local
on virtual machine:
...
server_name mysite.local test.mysite.local *.mysite.local;
...
I tried adding another route to routes.php
:
Route::group(['domain' => 'test.mysite.local'], function() {
Route::get('/', function () {
dd('you are at test subdomain');
});
});
to see if it works but it doesn't, I still get the same output as if I were to navigate to mysite.local
ANOTHER UPDATE:
/etc/hosts/
doesn't support wildcards so for the moment I'm using static subdomain names and will figure out how to do dynamic subdomains a bit later.
Also put
Route::group(['domain' => '{element}.mysite.local'], function() {
Route::get('/', function ($element) {
dd($element);
});
});
AT THE TOP or your routes.php
file.
At the moment I'm trying to redirect static subdomain to the specific controller@method. Will update this as I progress.
UPDATE - static domain working now:
New route:
Route::group(['domain' => '{element}.mysite.local'], function() {
Route::get('{path}', 'ElementController@single')->where('path', '.*');
});
ElementController.php
public function single($element)
{
//return view with information about my element
if ( $element = Element::where('slug', $element)->first() )
{
return view('elements.single', compact('element'));
}
//redirect to mysite.local if slug doesn't exist
return redirect(Config::get('app.url')); // points to config/app.php and find `url` parameter which should be defined in your `.env` file
}
TODO:
slug.mysite.local/sadsadas
works and runs thesingle
method from above, I would prefer if it would redirect toslug.mysite.local
- wildcard subdomains still not working