我正在构建一个子域指向用户的应用程序.我怎样才能获得地址的子域名 – 除了路线之外的其他地址?
Route::group(array('domain' => '{subdomain}.project.dev'),function() { Route::get('foo',function($subdomain) { // Here I can access $subdomain }); // How can I get $subdomain here? });
不过,我已经建立了一个混乱的解决方案:
Route::bind('subdomain',function($subdomain) { // Use IoC to store the variable for use anywhere App::bindIf('subdomain',function($app) use($subdomain) { return $subdomain; }); // We are technically replacing the subdomain-variable // However,we don't need to change it return $subdomain; });
我想在路由之外使用变量的原因是基于该变量建立数据库连接.
Shortly after问了这个问题,Laravel实现了一种在路由代码中获取子域的新方法.它可以像这样使用:
Route::group(array('domain' => '{subdomain}.project.dev'),function($subdomain) { // Here I can access $subdomain }); $subdomain = Route::input('subdomain'); });
请参见“访问路径参数值”in the docs.