Symfony supports multiple sites (applications)-php Tutorial

Source: Internet
Author: User
Tags autoload symlink
Symfony supports multiple sites (applications). The standard release of Symfony only supports one site. although you can use different routing rules based on different domain names through Host configuration in the routing system, this can also implement features similar to multiple sites, but the disadvantages are also very obvious:

Symfony

  • If you want a Service to behave differently on different sites, you cannot (DI cannot directly inject requests ).
  • Static files cannot be split.
  • Each page request needs to load all site configurations (bundle, routing rules, services, etc.), affecting performance.

After searching, we found that some people had the same confusions and provided a preliminary solution. But there are still some details, such as the Composer post-install-cmd/post-update-cmd (clear file cache, generate bootstrap. cache. php, publish static files to the web root directory, etc.) cannot be used normally. That article only solved the bootstrap. cache. php issue through soft chain, but did not mention clear file cache and so on.

Leo108's blog

This problem can only be solved by writing code by yourself. to create a composer project, relying on sensio/distribution-bundle, create a ScriptHandler class. the code is as follows:

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;    }}

Here we will rewrite the logic of the three methods buildBootstrap \ javasache \ installAssets. to avoid conflicts, I renamed the configuration item. the configuration of composer. json is as follows: 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"}

Add the file in autoload to the kernel file of each site, so you do not need to manually require. Leo108's blog

Replace scripts with our own ScriptHandler.

Pushing cool is a shameless website

Dm-apps is an array of each site. each site is configured with app-dir, autoload-dir, web-dir, and need-assets, app-dir indicates the directory where the kernel file is located; autoload-dir indicates autoload. the directory where the php file is located. php is completely consistent, so I put this file under the apps Directory and share all the sites (so I need to modify the app for each site. php app_dev.php and console files); web-dir indicates the target directory for publishing static files; need-assets indicates whether to publish static files (for example, the api site does not need to publish static files ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.