/ symfony

Symfony支持多个站点(应用)

对于一个网页系统来说,通常会需要面向用户的站点和面向管理员的站点,有的甚至还需要面向App的api站点。这些站点拥有不同的域名,但却共享核心业务逻辑。

Symfony的标准发行版只支持一个站点,虽然可以通过路由系统中的Host配置,根据不同的域名使用不同的路由规则,这样也可以实现类似多站点的功能,但缺点也非常明显:

  • 如果希望某个Service在不同的站点有不同的表现,就没办法实现(DI不能直接注入Request)。
  • 静态文件没办法很好拆分开来
  • 每个页面请求都需要加载所有站点的配置(bundle、路由规则、Service等等),影响性能
  • 不同的站点的异常处理逻辑不同(例如对于NotFoundHttpException,在Api站点可能需要输出一个json串而在网页端需要输出一个404页面)

经过搜索,发现也有人有相同的困惑,也给出了一个初步的解决方案。但是还是有一些细节方面的问题,比如标准发行版自带的Composer post-install-cmd/post-update-cmd(清文件缓存、生成bootstrap.cache.php、发布静态文件到web根目录等)不能正常使用。那篇文章只是通过软链解决了bootstrap.cache.php的问题,但并没有提到清文件缓存等。

这个问题只能自己写代码来解决了,新建一个composer项目,依赖于sensio/distribution-bundle,新建一个ScriptHandler类,代码如下:

namespace Dreamore\DreamoreBundle\Composer; use Composer\Script\CommandEvent; use Sensio\Bundle\DistributionBundle\Composer\ScriptHandler as Base; class ScriptHandler extends Base { /** * Composer variables are declared static so that an event could update * a composer.json and set new options, making them immediately available * to forthcoming listeners. / protected static $options = array( 'dm-apps' => array(), 'dm-assets-install' => 'hard', 'dm-cache-warmup' => false, ); /* * Builds the bootstrap file. * * The bootstrap file contains PHP file that are always needed by the application. * It speeds up the application bootstrapping. * * @param $event CommandEvent A instance / public static function buildBootstrap(CommandEvent $event) { $options = static::getOptions($event); foreach ($options['dm-apps'] as $config) { $bootstrapDir = $config['app-dir']; $autoloadDir = $config['autoload-dir']; if (!static::hasDirectory($event, 'app-dir', $bootstrapDir, 'build bootstrap file')) { return; } if (!static::hasDirectory($event, 'autoload-dir', $autoloadDir, 'build bootstrap file')) { return; } static::executeBuildBootstrap($event, $bootstrapDir, $autoloadDir, $options['process-timeout']); } } /* * Clears the Symfony cache. * * @param $event CommandEvent A instance / public static function clearCache(CommandEvent $event) { $options = static::getOptions($event); foreach ($options['dm-apps'] as $config) { $consoleDir = $config['app-dir']; if (!static::hasDirectory($event, 'app-dir', $consoleDir, 'execute command')) { return; } $warmup = ''; if (!$options['dm-cache-warmup']) { $warmup = ' --no-warmup'; } static::executeCommand($event, $consoleDir, 'cache:clear'.$warmup, $options['process-timeout']); } } /* * Installs the assets under the web root directory. * * For better interoperability, assets are copied instead of symlinked by default. * * Even if symlinks work on Windows, this is only true on Windows Vista and later, * but then, only when running the console with admin rights or when disabling the * strict user permission checks (which can be done on Windows 7 but not on Windows * Vista). * * @param $event CommandEvent A instance */ public static function installAssets(CommandEvent $event) { $options = static::getOptions($event); foreach ($options['dm-apps'] as $config) { $needAssets = isset($config['need-assets']) ? $config['need-assets'] : true; if (!$needAssets) { continue; } $consoleDir = $config['app-dir']; if (!static::hasDirectory($event, 'app-dir', $consoleDir, 'execute command')) { return; } $webDir = $config['web-dir']; $symlink = ''; if ($options['dm-assets-install'] == 'symlink') { $symlink = '--symlink '; } elseif ($options['dm-assets-install'] == 'relative') { $symlink = '--symlink --relative '; } if (!static::hasDirectory($event, 'web-dir', $webDir, 'install assets')) { return; } static::executeCommand($event, $consoleDir, 'assets:install '.$symlink.escapeshellarg($webDir), $options['process-timeout']); } } protected static function getOptions(CommandEvent $event) { $options = array_merge(static::$options, $event->getComposer()->getPackage()->getExtra()); $options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout'); return $options; } }

这里就重写buildBootstrap\clearCache\installAssets这三个方法的逻辑,同时为了避免冲突,我重新命名了配置项,composer.json的配置如下:

"autoload": { "psr-4": { "": "src/" }, "files": [ "apps/api/ApiKernel.php", "apps/admin/AdminKernel.php", "apps/wap/WapKernel.php" ] }, "scripts": { "post-install-cmd": [ "Dreamore\DreamoreBundle\Composer\ScriptHandler::buildBootstrap", "Dreamore\DreamoreBundle\Composer\ScriptHandler::clearCache", "Dreamore\DreamoreBundle\Composer\ScriptHandler::installAssets" ], "post-update-cmd": [ "Dreamore\DreamoreBundle\Composer\ScriptHandler::buildBootstrap", "Dreamore\DreamoreBundle\Composer\ScriptHandler::clearCache", "Dreamore\DreamoreBundle\Composer\ScriptHandler::installAssets" ] }, "extra": { "dm-apps": [ { "app-dir": "apps/api", "autoload-dir": "apps", "need-assets": false }, { "app-dir": "apps/admin", "autoload-dir": "apps", "web-dir": "web/admin" }, { "app-dir": "apps/wap", "autoload-dir": "apps", "web-dir": "web/wap" } ], "dm-assets-install": "relative" }

autoload中的file加入各个站点的kernel文件,这样就不需要手动require了。

scripts替换成我们自己的ScriptHandler

dm-apps是一个数组,每个站点一项,每个站点的配置有app-dir、autoload-dir、web-dir、need-assets,app-dir代表kernel文件所在的目录;autoload-dir代表autoload.php文件所在的目录,由于各个站点的autoload.php完全一致,所以我就把这个文件放到apps目录下,所有站点共享(因此需要修改每个站点的app.php app_dev.php和console这3个文件);web-dir代表发布静态文件的目标目录;need-assets代表是否需要发布静态文件(比如api这个站点就不需要发布静态文件)。

这样每次composer update或者composer install之后,我们的ScriptHandler就会对每一个站点执行生成bootstrap文件、清理文件缓存和发布静态文件的操作。

Symfony支持多个站点(应用)
Share this