【问题标题】:Laravel Multi domain SetupLaravel 多域设置
【发布时间】:2022-01-06 15:18:54
【问题描述】:

我想在我的 laravel 5.1 中设置多个域。

我有 3 个不同的域

www.domain1.com
www.domain2.com
www.domain3.com

每个域都有一些共同的功能和一些不同的功能,但主题不同

我想要这样的视图结构

resource/www.domain1.com
resource/www.domain2.com
resource/www.domain3.com

我无法分离视图如何实现这一点?

**

【问题讨论】:

  • 请阐明您想要实现的目标
  • 我想在一个 laravel 5.1 代码中处理多个网站。每个网站的视图会有所不同。

标签: php laravel


【解决方案1】:

您应该创建中间件来修改查看文件夹的路径:

namespace App\Http\Middleware;

use Closure;
use Illuminate\View\FileViewFinder;
use Illuminate\Support\Facades\View;

class ModifyViewFolder 
{

  public function handle($request, Closure $next)
  {
      $finder = new FileViewFinder(app()['files'], [
        app_path('../resources/views/' . $request->server->get('HTTP_HOST')),
        app_path('../resources/views/'),

      ]);
      View::setFinder($finder);

      return $next($request);
  }

}

然后将其注册到您的Kernel.php。如果域特定文件夹中的视图不存在,这将回退到默认文件夹。

【讨论】:

  • 有没有办法从共享视图中获取一些视图,而从域特定文件夹中获取其他视图?也许通过创建自定义FileViewFinder
  • @Rudie 您将数组传递给FileViewFinder,因此您可以传递任意数量的值。查看更新的答案。
  • 单独的数据库如何处理?t
  • @bogdan 请指导我如何为每个应用程序分离数据库
【解决方案2】:

我知道这篇文章已经很老了,但与 Laravel 的所有东西一样,做任何事情的方法不止一种。我可以通过重写 ViewServiceProvider 来实现这种效果。但是,此解决方案将适用于整个站点,因为接受的答案将允许您将模板应用于网络组。

 TemplateServiceProvider extends ViewServiceProvider
{
    /**
     * Register the view finder implementation.
     *
     * @return void
     */
    public function registerViewFinder()
    {
        $this->app->bind('view.finder', function ($app) {
            $paths = [];

            /*
             *  Pull our template from our site name
             */
            $template = Template::where('domain', Request::server('SERVER_NAME'))->first();
            if($template)
            {
                $paths = [
                    $app['config']['view.paths.templates'] . DIRECTORY_SEPARATOR . $template->tag
                ];
            }


            /*
             *  Default view path is ALWAYS last
             */
            $paths[] = $app['config']['view.paths.default'];

            return new FileViewFinder($app['files'], $paths);
        });
    }
}

【讨论】:

    【解决方案3】:

    你可以使用这个包Gecce Multi Domain轻松做到这一点

    安装 添加 gecche/laravel-multidomain 作为 composer.json 的要求:

    {
        "require": {
            "gecche/laravel-multidomain": "4.*"
        }
    }
    

    使用 composer update 更新您的包或使用 composer install 安装。

    你也可以使用 composer require gecche/laravel-multidomain 添加包,然后指定你想要的版本(目前,dev-v1.1.* 是你最好的选择)。

    这个包需要在引导过程的一开始就在最小的 Laravel 核心函数集中覆盖对 HTTP 域的检测,以获取特定的环境文件。所以这个包比大多数 Laravel 包需要更多的配置步骤。

    安装步骤:

    通过修改 bootstrap/app.php 文件最顶部的以下几行来替换整个 Laravel 容器。

    //$app = new Illuminate\Foundation\Application(
    $app = new Gecche\Multidomain\Foundation\Application(
        $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
    );
    

    更新两个应用程序内核(HTTP 和 CLI)。 在 app/Http/Kernel.php 文件的最顶部,进行以下更改:

    //use Illuminate\Foundation\Http\Kernel as HttpKernel;
    use Gecche\Multidomain\Foundation\Http\Kernel as HttpKernel;
    

    同样在 app/Console/Kernel.php 文件中:

    //use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    use Gecche\Multidomain\Foundation\Console\Kernel as ConsoleKernel;
    

    使用 config/app.php 文件中 $providers 数组中的扩展项覆盖 QueueServiceProvider: //Illuminate\Queue\QueueServiceProvider::class, Gecche\Multidomain\Queue\QueueServiceProvider::class, 发布配置文件。

    php artisan vendor:publish 
    

    (这个包利用了发现功能。)

    按照上述步骤,您的应用程序将知道正在运行的 HTTP 域,包括 HTTP 和 CLI 请求,包括队列支持。

    用法 该包添加了三个命令来管理您的应用程序 HTTP 域:

    domain.add artisan command
    

    主要命令是 domain:add 命令,该命令将要添加到应用程序的 HTTP 域的名称作为参数。假设我们有两个域,site1.com 和 site2.com,共享相同的代码。

    我们只是这样做:

    php artisan domain:add site1.com 
    

    php artisan domain:add site2.com 
    

    这些命令会创建两个新的环境文件,.env.site1.com 和 .env.site2.com,您可以在其中放置每个站点的特定配置(例如数据库配置、缓存配置和其他配置,如通常所见)在环境文件中)。

    该命令还在 config/domains.php 文件中的 domain 键中添加一个条目。

    此外,还会创建两个新文件夹,即 storage/site1_com/ 和 storage/site2_com/。它们与主存储具有相同的文件夹结构。

    对此存储子结构的自定义必须与 config/domain.php 文件中的值匹配。

    domain.remove artisan 命令 domain:remove 命令通过删除其环境文件从应用程序中删除指定的 HTTP 域。例如:

    php artisan domain:remove site2.com 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 2017-07-18
      • 2016-04-01
      • 2021-04-15
      • 2012-06-24
      • 1970-01-01
      • 2012-11-07
      相关资源
      最近更新 更多