Magento 2在默认或生产模式下对公共静态文件使用Symlink


11

情况:我正在免费试用Magento 2,并且VPS上的高清空间有限。由于这个原因,为了减少高清空间,我想让Magento在默认或生产模式下对文件进行符号链接。我注意到,网站首次运行时,默认的Magento 2安装约为420mb,到770mb左右,因此其中350mb是复制文件。

我修改了:app / etc / di.xml行:

 <item name="default" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Copy</item>

 <item name="default" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>

这样就可以正常工作,即使在默认模式下,站点也可以正确创建符号链接。但是,问题是未创建css或应创建但不是的js-translation.json文件:

/pub/static/version1488209436/frontend/Magento/luma/en_US/css/styles-m.css

/pub/static/version1488209436/frontend/Magento/luma/en_US/css/styles-l.css

/pub/static/version1488209436/frontend/Magento/luma/en_US/css/print.css

/pub/static/version1488209436/frontend/Magento/luma/en_US/js-translation.json

所以问题是:在默认或生产模式下并使用符号链接时,如何使Magento生成这些文件?


也许您可以使用Gulp或Grunt来解决CSS问题:magento.stackexchange.com/questions/162906/…并研究js-translation.json文件的不同模式?gist.github.com/antonmakarenko/7538216
B00MER

谢谢,如果有人可以一步一步给我并验证它可以工作,我会将其提高到+100。
凯文·查韦斯

在pub / static文件夹中,有一个.htaccess文件,该文件负责重写url并从url中删除“ version1488209436”并提供用户友好的url。如果.htaccess无法正常工作,则只会发生此类问题。
Tonmoy

谢谢@Tonmoy,但它与此根本无关。问题是文件不会生成,因为在默认/生产模式下我使用符号链接而不是复制。
凯文·查韦斯

由于有4个文件,因此您可以手动或在.sh脚本中创建这些符号链接。
晦涩

Answers:


5

app/etc/di.xml:将新项目添加到stategiesList

<virtualType name="developerMaterialization" type="Magento\Framework\App\View\Asset\MaterializationStrategy\Factory">
    <arguments>
        <argument name="strategiesList" xsi:type="array">
            <item name="view_preprocessed" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>
            <item name="default" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>
/* ++ */    <item name="asset" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Copy</item>
        </argument>
    </arguments>
</virtualType>

假设您处于开发人员模式,只需删除pub/static浏览器中的内容并转到浏览器中的页面-magento将重新生成静态内容。

在Magento 2.1.4中为我工作(生成了styles-m.css,并建立了其他文件的符号链接)。

所有的魔力发生在vendor/magento/framework/App/View/Asset/MaterializationStrategy/Factory.php

public function create(Asset\LocalInterface $asset)
{   
    if (empty($this->strategiesList)) {
        $this->strategiesList[] = $this->objectManager->get(self::DEFAULT_STRATEGY);
    }   

    foreach ($this->strategiesList as $strategy) {
        if ($strategy->isSupported($asset)) {
            return $strategy;
        }   
    }   

    throw new \LogicException('No materialization strategy is supported');
}   

Magento只是遍历stategiesList项目并使用支持资产的第一个策略。

如何使其在生产模式下工作?

免责声明:此hack包含核心文件编辑。谨防。

全部在magento 2.1.4上测试

  1. 从静态文件中删除版本号 Stores > Configuration > Advanced > Developer > Static Files Settings > No
  2. 编辑vendor/magento/framework/App/StaticResource.php并使launch函数看起来像这样:

    public function launch()
    {   
    // disabling profiling when retrieving static resource
    \Magento\Framework\Profiler::reset();
    $appMode = $this->state->getMode();
    /*if ($appMode == \Magento\Framework\App\State::MODE_PRODUCTION) {
        $this->response->setHttpResponseCode(404);
    } else {*/
        $path = $this->request->get('resource');
        $params = $this->parsePath($path);
        $this->state->setAreaCode($params['area']);
        $this->objectManager->configure($this->configLoader->load($params['area']));
        $file = $params['file'];
        unset($params['file']);
        $asset = $this->assetRepo->createAsset($file, $params);
        $this->response->setFilePath($asset->getSourceFile());
        $this->publisher->publish($asset);
    /*}*/
    return $this->response;
    }   
  3. 删除其中的内容,pub/static并在浏览器中访问您的商店网址。


谢谢你,这个工作完美!di.xml中只有这一行!我也很感谢您对它如何工作的解释。现在安装是482mb而不是770mb,这将为我省钱,而不必升级我的VPS免费试用服务器。
凯文·查韦斯

先生,非常欢迎您,我很高兴为您服务。
康斯坦丁·杰拉西莫夫

仅修改StaticResource.php并保留原始app / etc / di.xml而不添加就足够了<item name="asset" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Copy</item> 吗?
LucScu

0

当不在生产模式下时,Magento 2将尝试为某些静态资源创建符号链接。您可以通过执行以下操作来更改该行为。

  1. 打开app / etc / di.xml并找到virtualType name =“ developerMaterialization”部分。在该部分中,您将找到需要修改或删除的项目名称=“ view_preprocessed”。您可以通过将内容从Magento \ Framework \ App \ View \ Asset \ MaterializationStrategy \ Symlink更改为Magento \ Framework \ App \ View \ Asset \ MaterializationStrategy \ Copy来修改它

  2. 删除pub / static下的文件。请不要删除.htaccess文件。


1
我认为您不理解我的问题,我不想“复制”我想“符号链接”以节省有限服务器资源的空间。
凯文·查韦斯
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.