首页 > 后端开发 > php教程 > Symfony支持多个站点(应用)

Symfony支持多个站点(应用)

WBOY
发布: 2016-06-23 13:13:44
原创
945 人浏览过

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

symfony

  • 如果希望某个Service在不同的站点有不同的表现,就没办法实现(DI不能直接注入Request)。
  • 静态文件没办法很好拆分开来
  • 每个页面请求都需要加载所有站点的配置(bundle、路由规则、Service等等),影响性能

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

leo108's blog

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

http://leo108.com/pid-2202.asp

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的配置如下: symfony

"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了。 leo108's blog

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这个站点就不需要发布静态文件)。

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板