【问题标题】:set base URL dynamicaly in Laravel 5在 Laravel 5 中动态设置基本 URL
【发布时间】:2018-05-19 06:39:52
【问题描述】:

我有两个版本的网站,一个在 example.com 上运行 以及 example.com/version2 上的其他内容。

在 web.php 我有

    Route::group(['middleware'=>['webconfig'],'prefix'=>'version2'], function(){
        //Routes for version2 site
    });
// Routes for main site

在 webconfig 中间件中,我为 version2 站点设置了数据库连接。哪个工作正常,但我也在为 version2 站点设置基本 URL。哪个不起作用 url() 方法返回 example.com 这是我的 webconfig 中间件

public function handle($request, Closure $next)
{
    \Config::set('database.default', 'mysql_us');
    \Config::set('app.url', url('version2'));
    return $next($request);
}

那么我该如何动态设置基本网址。

【问题讨论】:

  • 你应该不需要更改URL,否则该组会将version2更改为version2/version2,并且可能会陷入循环。
  • app.url 被控制台使用,不用于网络请求

标签: php laravel laravel-5


【解决方案1】:

因为您的 2 版本部署在同一主机中,您只需为新版本添加前缀“version2”。为什么不创建一个中间件来检查您的段(1)是否是版本 2,然后设置它将使用的正确数据库。我相信您想在用户位于version2 段时重定向到其他数据库,对吗?

在这种情况下,您可以在中间件中使用这样的解决方案,

if (Str::lower($request->segment(1) == "version2") {
    config()->set('database.default', 'mysql_us');
} else {
    // your another config.
}

希望对您的问题有所帮助。

【讨论】:

    【解决方案2】:

    在您的 .env 文件中指定 APP_URL=http://example.com/version2

    【讨论】:

    • 我想动态更改应用程序 URL 即程序化
    • 您可能还需要运行“php artisan config:cache”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多