Twitter bootstrap 在yii2中从更少的资源编译自定义引导css

Twitter bootstrap 在yii2中从更少的资源编译自定义引导css,twitter-bootstrap,less,composer-php,yii2,Twitter Bootstrap,Less,Composer Php,Yii2,我在Yii2和Composer中开始了一个新项目,在YiI1完成了几个项目之后 我用composer创建了一个新的高级项目。一切正常,但我想知道定制引导主题的最佳方式是什么 我认为将整个引导过程复制到后端和前端,编辑两个变量文件并编译它不是正确的方法 那么:是否有可能扩展作曲家下载的较少文件,只编辑两个变量的文件?我正在使用基本应用程序 例如,要更改某些引导属性字段高度,我执行了以下操作: 创建具有更改属性的新css文件:web\css\bootstrap-modified.css 将此文件添加

我在Yii2和Composer中开始了一个新项目,在YiI1完成了几个项目之后

我用composer创建了一个新的高级项目。一切正常,但我想知道定制引导主题的最佳方式是什么

我认为将整个引导过程复制到后端和前端,编辑两个变量文件并编译它不是正确的方法


那么:是否有可能扩展作曲家下载的较少文件,只编辑两个变量的文件?

我正在使用基本应用程序

例如,要更改某些引导属性字段高度,我执行了以下操作:

创建具有更改属性的新css文件:web\css\bootstrap-modified.css

将此文件添加到assets\AppAssets.php:


我不确定我是否理解这个问题。为什么不创建上一次加载的custom.css或custom.less文件,并覆盖所需内容?另一个解决方案是扩展较少的文件,并使用扩展文件而不是普通文件。

我不想包含另一个引导副本,我想自定义它。因此,答案是加载引导的自定义副本,而不是基本引导

In basic app..(yii2)../.    

First create your css in (in website folder)Basic->web->css->custom.css(Your CSS File)

After that if we want add new css to our site, go to (in website folder)Basic->assets and open              AppAsset.php.
and add 'custom.css' after 'css/site.css' like below.  
 -------------------------------------------------------     

<?php
namespace app\assets;
use yii\web\AssetBundle;
/**
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class AppAsset extends AssetBundle
{
    public $basePath = '@webroot';
    public $baseUrl = '@web';
    public $css = [
        'css/site.css',
        'css/custom.css',
    ];
    public $js = [
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];

 --------------------------------------------------------------------

You can add it in your page by 

use app\basic\web\css;

可以找到一个很好的教程

引导配置可以通过更改变量来完成。 以下bootstrappasset是原始bootstrap-variables.less的替换,它在当前目录中使用bootstrap-variables.less,而不是原始variables.less

namespace app\assets;

use yii\helpers\FileHelper;
use yii\web\AssetBundle;

class BootstrapAsset extends AssetBundle
{
    public $sourcePath = '@bower/bootstrap/dist';
    public $css = [
        'css/bootstrap.css',
    ];

    public function publish($am)
    {
        if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
            list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
        }

        $src = \Yii::getAlias($this->sourcePath);

        FileHelper::copyDirectory("$src/../less", $this->basePath."/less");

        $vars = file_get_contents(__DIR__ . "/bootstrap-variables.less");
        $css = \Yii::$app->cache->get("bootstrap-css-".crc32($vars));
        if (!$css)
        {
            file_put_contents("$src/../less/variables.less", $vars);

            ini_set('xdebug.max_nesting_level', 200);

            $less = new \lessc();
            $less->setFormatter(YII_DEBUG ? "lessjs" : "compressed");
            unlink($this->basePath . "/css/bootstrap.css");
            $less->compileFile("$src/../less/bootstrap.less", $this->basePath . "/css/bootstrap.css");
            \Yii::$app->cache->set("bootstrap-css-".crc32($vars), file_get_contents($this->basePath . "/css/bootstrap.css"));
        }
        else
        {
            file_put_contents($this->basePath . "/css/bootstrap.css", $css);
        }
    }
}

对于简单的更改是好的,对于复杂的更改是坏的,在复杂的更改中,您确实应该从更少的代码重新编译。在你最终完成之前,大多数定制最终会变成复杂的更改。你如何替换原来的定制?您只需将代码放入assets/BootstrapAsset.php?发现您确实将其放入assets文件夹,然后添加BootstrapAsset::register$this;在views/layouts/main.php中创建布局。但是我有一个问题,类lessc不可用,所以我不能使用它…您可以通过在composer.json中包含对oyejorge/less.php的依赖关系来获得lessc
namespace app\assets;

use yii\helpers\FileHelper;
use yii\web\AssetBundle;

class BootstrapAsset extends AssetBundle
{
    public $sourcePath = '@bower/bootstrap/dist';
    public $css = [
        'css/bootstrap.css',
    ];

    public function publish($am)
    {
        if ($this->sourcePath !== null && !isset($this->basePath, $this->baseUrl)) {
            list ($this->basePath, $this->baseUrl) = $am->publish($this->sourcePath, $this->publishOptions);
        }

        $src = \Yii::getAlias($this->sourcePath);

        FileHelper::copyDirectory("$src/../less", $this->basePath."/less");

        $vars = file_get_contents(__DIR__ . "/bootstrap-variables.less");
        $css = \Yii::$app->cache->get("bootstrap-css-".crc32($vars));
        if (!$css)
        {
            file_put_contents("$src/../less/variables.less", $vars);

            ini_set('xdebug.max_nesting_level', 200);

            $less = new \lessc();
            $less->setFormatter(YII_DEBUG ? "lessjs" : "compressed");
            unlink($this->basePath . "/css/bootstrap.css");
            $less->compileFile("$src/../less/bootstrap.less", $this->basePath . "/css/bootstrap.css");
            \Yii::$app->cache->set("bootstrap-css-".crc32($vars), file_get_contents($this->basePath . "/css/bootstrap.css"));
        }
        else
        {
            file_put_contents($this->basePath . "/css/bootstrap.css", $css);
        }
    }
}