symfony は複数のサイト (アプリケーション) をサポートします
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这个站点就不需要发布静态文件)。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Laravelは、直感的なフラッシュメソッドを使用して、一時的なセッションデータの処理を簡素化します。これは、アプリケーション内に簡単なメッセージ、アラート、または通知を表示するのに最適です。 データは、デフォルトで次の要求のためにのみ持続します。 $リクエスト -

PHPクライアントURL(CURL)拡張機能は、開発者にとって強力なツールであり、リモートサーバーやREST APIとのシームレスな対話を可能にします。尊敬されるマルチプロトコルファイル転送ライブラリであるLibcurlを活用することにより、PHP Curlは効率的なexecuを促進します

Laravelは簡潔なHTTP応答シミュレーション構文を提供し、HTTP相互作用テストを簡素化します。このアプローチは、テストシミュレーションをより直感的にしながら、コード冗長性を大幅に削減します。 基本的な実装は、さまざまな応答タイプのショートカットを提供します。 Illuminate \ support \ facades \ httpを使用します。 http :: fake([[ 'google.com' => 'hello world'、 'github.com' => ['foo' => 'bar']、 'forge.laravel.com' =>

顧客の最も差し迫った問題にリアルタイムでインスタントソリューションを提供したいですか? ライブチャットを使用すると、顧客とのリアルタイムな会話を行い、すぐに問題を解決できます。それはあなたがあなたのカスタムにより速いサービスを提供することを可能にします

記事では、PHP 5.3で導入されたPHPの後期静的結合(LSB)について説明し、より柔軟な継承を求める静的メソッドコールのランタイム解像度を可能にします。 LSBの実用的なアプリケーションと潜在的なパフォーマ

この記事では、フレームワークにカスタム機能を追加し、アーキテクチャの理解、拡張ポイントの識別、統合とデバッグのベストプラクティスに焦点を当てています。

記事では、入力検証、認証、定期的な更新など、脆弱性から保護するためのフレームワークの重要なセキュリティ機能について説明します。
