我想有這樣的路線: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
方法,我 仍然沒有工作
沒什麼,但現在我需要再次嘗試將'.local'更改爲'.dev',因爲我不知道什麼時候這個錯字發生了嗎?謝謝! – dbr
我試着在'hosts'和'mysite.local'文件中將'.local'改爲'.dev'。我也將'mysite.local'複製到'mysite.dev',但它不起作用。但是,這帶來了另一個問題,那就是當我在瀏覽器中打開mysite.dev時,我得到了另一個本地站點,這個站點也在我的hosts文件中定義。即使我在'hosts'文件中評論過它,它也會重定向到它。看來我在這裏錯過了一些大事。 – dbr
你能確認nginx配置已經正常工作嗎?我的意思是嘗試使用沒有子域名的簡單域名,然後在laravel中使用簡單的路由..查看頁面是否打開 –