【发布时间】:2016-07-08 02:36:51
【问题描述】:
我想要这样的路线:
john-doe.mysite.local
john-another-doe.mysite.local
然后将子域名传递给某个 controller@method 并呈现用户配置文件。
我正在使用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,但也没有用。
我不知道我在这里错过了什么。
更新:
如果我将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);
});
});
置顶或您的routes.php 文件。
目前我正在尝试将静态子域重定向到特定的控制器@方法。会随着我的进展更新。
更新 - 静态域现在工作:
新路线:
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
}
待办事项:
-
slug.mysite.local/sadsadas从上面工作并运行single方法,我 如果它会重定向到slug.mysite.local,我会更喜欢 - 通配符子域仍然无法正常工作
【问题讨论】:
标签: nginx laravel-5.2 wildcard-subdomain