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

PHPSession失效的原因包括配置錯誤、Cookie問題和Session過期。 1.配置錯誤:檢查並設置正確的session.save_path。 2.Cookie問題:確保Cookie設置正確。 3.Session過期:調整session.gc_maxlifetime值以延長會話時間。

在PHP中調試會話問題的方法包括:1.檢查會話是否正確啟動;2.驗證會話ID的傳遞;3.檢查會話數據的存儲和讀取;4.查看服務器配置。通過輸出會話ID和數據、查看會話文件內容等方法,可以有效診斷和解決會話相關的問題。

多次調用session_start()會導致警告信息和可能的數據覆蓋。 1)PHP會發出警告,提示session已啟動。 2)可能導致session數據意外覆蓋。 3)使用session_status()檢查session狀態,避免重複調用。

在PHP中配置會話生命週期可以通過設置session.gc_maxlifetime和session.cookie_lifetime來實現。 1)session.gc_maxlifetime控制服務器端會話數據的存活時間,2)session.cookie_lifetime控制客戶端cookie的生命週期,設置為0時cookie在瀏覽器關閉時過期。

使用數據庫存儲會話的主要優勢包括持久性、可擴展性和安全性。 1.持久性:即使服務器重啟,會話數據也能保持不變。 2.可擴展性:適用於分佈式系統,確保會話數據在多服務器間同步。 3.安全性:數據庫提供加密存儲,保護敏感信息。

在PHP中實現自定義會話處理可以通過實現SessionHandlerInterface接口來完成。具體步驟包括:1)創建實現SessionHandlerInterface的類,如CustomSessionHandler;2)重寫接口中的方法(如open,close,read,write,destroy,gc)來定義會話數據的生命週期和存儲方式;3)在PHP腳本中註冊自定義會話處理器並啟動會話。這樣可以將數據存儲在MySQL、Redis等介質中,提升性能、安全性和可擴展性。

SessionID是網絡應用程序中用來跟踪用戶會話狀態的機制。 1.它是一個隨機生成的字符串,用於在用戶與服務器之間的多次交互中保持用戶的身份信息。 2.服務器生成並通過cookie或URL參數發送給客戶端,幫助在用戶的多次請求中識別和關聯這些請求。 3.生成通常使用隨機算法保證唯一性和不可預測性。 4.在實際開發中,可以使用內存數據庫如Redis來存儲session數據,提升性能和安全性。

在無狀態環境如API中管理會話可以通過使用JWT或cookies來實現。 1.JWT適合無狀態和可擴展性,但大數據時體積大。 2.Cookies更傳統且易實現,但需謹慎配置以確保安全性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。