2017-01-17 156 views

回答

10

Laravel以先到先得的方式处理路由,因此您需要将最不具体的路由放在路由文件中。这意味着您需要将您的路线组放在具有相同路径的任何其他路线上方。

例如,这将如预期:

Route::group(['domain' => 'admin.localhost'], function() { 
    Route::get('/', function() { 
     return "This will respond to requests for 'admin.localhost/'"; 
    }); 
}); 

Route::get('/', function() { 
    return "This will respond to all other '/' requests."; 
}); 

但是这个例子不会:

Route::get('/', function() { 
    return "This will respond to all '/' requests before the route group gets processed."; 
}); 

Route::group(['domain' => 'admin.localhost'], function() { 
    Route::get('/', function() { 
     return "This will never be called"; 
    }); 
}); 
+0

比我描述的更好! –

1

Laravel的例子...

Route::group(['domain' => '{account}.myapp.com'], function() { 
    Route::get('user/{id}', function ($account, $id) { 
     // 
    }); 
}); 

您的代码

Route::group(['domain' => 'admin.localhost'], function() { 
    Route::get('/', function() { 
     return view('welcome'); 
    }); 
}); 

如果你看看laravel例如它会路由中的参数$account,这样我们就可以根据这个变量进行路由。这可以适用于组或其中任何路由。..

这就是说,如果它不是由你的数据库驱动的东西,你只是想要它与管理子域我亲自做这个nginx配置。

如果您想在本地测试nginx(更简单),我个人建议使用docker进行开发。

希望这回答你的问题,如果不让我知道和生病试图回答你。

+1

在此同意你的观点,但OP的问题仍然没有答案,为什么它不工作 – Paras