php 获取子域路由中的子域(Laravel)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18982844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the subdomain in a subdomain-route (Laravel)
提问by Martti Laine
I'm building an application where a subdomain points to a user. How can I get the subdomain-part of the address elsewhere than in a route?
我正在构建一个应用程序,其中子域指向用户。如何在路由之外的其他地方获取地址的子域部分?
Route::group(array('domain' => '{subdomain}.project.dev'), function() {
Route::get('foo', function($subdomain) {
// Here I can access $subdomain
});
// How can I get $subdomain here?
});
I've built a messy work-around, though:
不过,我已经建立了一个凌乱的解决方法:
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;
});
The reason I want to use the variable outside a route is to establish a database-connection based on that variable.
我想在路由之外使用该变量的原因是基于该变量建立数据库连接。
回答by Moshe Katz
Shortly afterthis question was asked, Laravel implemented a new method for getting the subdomain inside the routing code. It can be used like this:
在提出这个问题后不久,Laravel 实现了一种新方法,用于在路由代码中获取子域。它可以像这样使用:
Route::group(array('domain' => '{subdomain}.project.dev'), function() {
Route::get('foo', function($subdomain) {
// Here I can access $subdomain
});
$subdomain = Route::input('subdomain');
});
See "Accessing A Route Parameter Value" in the docs.
请参阅文档中的“访问路由参数值” 。
回答by xmarcos
$subdomain
is injected in the actual Route
callback, it is undefined inside the group
closure because the Request
has not been parsed yet.
$subdomain
被注入到实际的Route
回调中,它在group
闭包中是未定义的,因为Request
还没有被解析。
I don't see why would you need to have it available inside the group closure BUToutside of the actual Route
callback.
我不明白为什么您需要在组闭包内但在实际Route
回调之外使用它。
If want you want is to have it available everywhere ONCE the Request
has been parsed, you could setup an after filter to store the $subdomain
value:
如果您希望Request
在解析后在任何地方都可以使用它,您可以设置一个后过滤器来存储$subdomain
值:
Config::set('request.subdomain', Route::getCurrentRoute()->getParameter('subdomain'));
Update: applying a group filter to setup a database connection.
更新:应用组过滤器来设置数据库连接。
Route::filter('set_subdomain_db', function($route, $request)
{
//set your $db here based on $route or $request information.
//set it to a config for example, so it si available in all
//the routes this filter is applied to
Config::set('request.subdomain', 'subdomain_here');
Config::set('request.db', 'db_conn_here');
});
Route::group(array(
'domain' => '{subdomain}.project.dev',
'before' => 'set_subdomain_db'
), function() {
Route::get('foo', function() {
Config::get('request.subdomain');
});
Route::get('bar', function() {
Config::get('request.subdomain');
Config::get('request.db');
});
});
回答by Martin Tonev
Above won't work in Laravel 5.4 or 5.6. Please test this one:
以上不适用于 Laravel 5.4 或 5.6。请测试这个:
$subdomain = array_first(explode('.', request()->getHost()));
回答by Ostap B.
Way with macro:
使用宏的方式:
Request::macro('subdomain', function () {
return current(explode('.', $this->getHost()));
});
Now you can use it:
现在你可以使用它:
$request->subdomain();
回答by Itrulia
in the route call
在路由调用中
$route = Route::getCurrentRoute();
$route = Route::getCurrentRoute();
and now you should have access to everything the route has. I.e.
现在您应该可以访问该路线的所有内容。IE
$route = Route::getCurrentRoute();
$subdomain = $route->getParameter('subdomain');