简体   繁体   English

获取子域路由中的子域(Laravel)

[英]Get the subdomain in a subdomain-route (Laravel)

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.我想在路由之外使用变量的原因是基于该变量建立数据库连接。

Shortly after this question was asked, Laravel implemented a new method for getting the subdomain inside the routing code. 在提出这个问题后不久,Laravel 实现了一种新方法,用于在路由代码中获取子域。 It can be used like this:它可以像这样使用:

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 .请参阅文档中的“访问路由参数值”。

$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 BUT outside 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');
    });
});

Above won't work in Laravel 5.4 or 5.6.以上不适用于 Laravel 5.4 或 5.6。 Please test this one:请测试这个:

$subdomain = array_first(explode('.', request()->getHost()));

Way with macro:使用宏的方式:

Request::macro('subdomain', function () {
    return current(explode('.', $this->getHost()));
});

Now you can use it:现在你可以使用它:

$request->subdomain();

in the route call在路由调用中

$route = Route::getCurrentRoute();

and now you should have access to everything the route has.现在您应该可以访问该路线的所有内容。 Ie IE

$route = Route::getCurrentRoute();
$subdomain = $route->getParameter('subdomain');

像这样

Route::getCurrentRoute()->subdomain

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM