php – 在子域路由(Laravel)中获取子域

前端之家收集整理的这篇文章主要介绍了php – 在子域路由(Laravel)中获取子域前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个子域指向用户的应用程序.我怎样才能获得地址的子域名 – 除了路线之外的其他地址?
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.

原文链接:https://www.f2er.com/laravel/132974.html

猜你在找的Laravel相关文章