laravel 5.2,通配符子域
我想有这样的路线:laravel 5.2,通配符子域
john-doe.mysite.local
john-another-doe.mysite.local
,然后通过子域名的一些控制器@方法和现在的用户配置文件。我正在使用Laravel Homestead。
这里是我的/etc/hosts
文件从我的本地机器:
....
192.168.10.10 mysite.local
192.168.10.10 *.mysite.local
....
和我的虚拟机文件/etc/nginx/sites-available/mysite.local
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;
}
....
。如果您有什么需要请询问。 主域名mysite.local
正常工作。
我试着跟随documentation加法这个我routes.php
文件:
Route::group(['domain' => '{element}.mysite.local'], function() {
Route::get('/', function ($element) {
dd($element);
});
});
但是,如果我去任何子域somedomain.mysite.local
我得到的服务器没有找到。
在我看来,问题是我甚至没有通过子域到达Laravel应用程序。
任何帮助表示赞赏。 我发现this question,并试图在我的/etc/hosts
和mysite.local
文件中将.local
更改为.dev
,但它不起作用。我也尝试将mysite.local
更改为mysite.dev
,但它也不起作用。
我想不通我在这里错过了什么。
UPDATE:
如果我添加test.mysite.local
我/etc/hosts
文件我得到相同的输出在浏览器中,就好像我是浏览mysite.local
。
....
192.168.10.10 mysite.local
192.168.10.10 test.mysite.local
192.168.10.10 *.mysite.local
....
虚拟机上更新mysite.local
:
...
server_name mysite.local test.mysite.local *.mysite.local;
...
我尝试添加另一种途径routes.php
:
Route::group(['domain' => 'test.mysite.local'], function() {
Route::get('/', function() {
dd('you are at test subdomain');
});
});
,看看它的工作原理,但它并没有,我仍然得到相同的输出,如果我导航到mysite.local
另一个更新:
/etc/hosts/
不支持通配符所以目前我使用静态的子域名,并找出如何了一下后做动态的子域。
也把
Route::group(['domain' => '{element}.mysite.local'], function() {
Route::get('/', function ($element) {
dd($element);
});
});
AT THE TOP或您routes.php
文件。
此刻我试图将静态子域重定向到特定的控制器@方法。随着我的进步,会更新这个。
更新 - 静态域现在的工作:
新航线:
Route::group(['domain' => '{element}.mysite.local'], function() {
Route::get('{path}', '[email protected]')->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
作品并运行从上面的single
方法,我 仍然没有工作
这也许宁愿是否会重定向到slug.mysite.local
没什么,但现在我需要再次尝试将'.local'更改为'.dev',因为我不知道什么时候这个错字发生了吗?谢谢! – dbr
我试着在'hosts'和'mysite.local'文件中将'.local'改为'.dev'。我也将'mysite.local'复制到'mysite.dev',但它不起作用。但是,这带来了另一个问题,那就是当我在浏览器中打开mysite.dev时,我得到了另一个本地站点,这个站点也在我的hosts文件中定义。即使我在'hosts'文件中评论过它,它也会重定向到它。看来我在这里错过了一些大事。 – dbr
你能确认nginx配置已经正常工作吗?我的意思是尝试使用没有子域名的简单域名,然后在laravel中使用简单的路由..查看页面是否打开 –