Yii2 Theme Installation

时间:2015-06-26 09:42:14

标签: php twitter-bootstrap themes yii2

I am trying to install Cerulean Theme into Yii2 Advanced Template on frontend app. In main.php AppAsset::register($this); is not including files. Following is my code in AppAsset

<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace frontend\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/site.css',
    ];
    public $js = [
        '/js/jquery.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
}

And in main.php

<?php

use yii\helpers\Html;
use yii\helpers\Url;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use frontend\assets\AppAsset;
use frontend\widgets\Alert;
use yii\bootstrap\ActiveForm;

/* @var $this \yii\web\View */
/* @var $content string */

AppAsset::register($this); 
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="<?= Yii::$app->charset ?>"/>
        <meta name="viewport" content="width=device-width, initial-scale=1">
<?= Html::csrfMetaTags() ?>
        <title><?= Html::encode($this->title) ?></title>
        <?php $this->head() ?>


        <!-- Le javascript
        ================================================== -->
        <!-- Placed at the end of the document so the pages load faster -->
        <script src="/themes/cerulian/js/jquery.js"></script>
        <script src="/themes/cerulian/js/bootstrap-transition.js"></script>
        <script src="/themes/cerulian/js/bootstrap-alert.js"></script>
        <script src="/themes/cerulian/js/bootstrap-modal.js"></script>
        <script src="/themes/cerulian/js/bootstrap-dropdown.js"></script>
        <script src="/themes/cerulian/js/bootstrap-scrollspy.js"></script>
        <script src="/themes/cerulian/js/bootstrap-tab.js"></script>
        <script src="/themes/cerulian/js/bootstrap-tooltip.js"></script>
        <script src="/themes/cerulian/js/bootstrap-popover.js"></script>
        <script src="/themes/cerulian/js/bootstrap-button.js"></script>
        <script src="/themes/cerulian/js/bootstrap-collapse.js"></script>
        <script src="/themes/cerulian/js/bootstrap-carousel.js"></script>
        <script src="/themes/cerulian/js/bootstrap-typeahead.js"></script>

        <!-- Le styles -->

<!--        <link href="/themes/cerulian/bootstrap/dist/css/bootstrap.css" rel="stylesheet">-->

        <link href="/themes/cerulian/css/bootstrap.css" rel="stylesheet">
        <link rel="stylesheet" href="/css/site.css">
        <style type="text/css">

        </style>
        <link href="/css/developers.css">

        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
          <script src="js/html5shiv.js"></script>
        <![endif]-->

        <!-- Fav and touch icons -->
        <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/ico/apple-touch-icon-144-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="114x114" href="/ico/apple-touch-icon-114-precomposed.png">
        <link rel="apple-touch-icon-precomposed" sizes="72x72" href="/ico/apple-touch-icon-72-precomposed.png">
        <link rel="apple-touch-icon-precomposed" href="/ico/apple-touch-icon-57-precomposed.png">
        <link rel="shortcut icon" href="/ico/favicon.png">

        <style>

        </style>

          <link rel="stylesheet" href="/css/developers.css">
    </head>

    <body>

<?php

//      echo '<pre>';
//      print_r($this);exit;
$this->beginBody()
?>

</html>

See i have added the script and css file manually by giving script and css tags.

In frontend/config/main.php following are the settings

    'components' => [
//        'MyHtmlElementsGenerator' => [
//            'class' => 'componentsmodels\User',
//            'enableAutoLogin' => true,
//        ],
         'view' => [
            'theme' => [
                'basePath' => '@app/themes/cerulian',
                'baseUrl' => '@web/themes/cerulian',
                'pathMap' => [
                    '@app/views' => '@app/themes/cerulian',
                ],
            ],
                        ], 

        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',![enter image description here][1]
        ],
    ],

Following is the directory structure.

!http://i.stack.imgur.com/ZCkRk.png

Issue is assets register is not working. Even in views i cannot use $this->registerJs and $this->registerCssFile. These functions are not seems to be working. What is the issue can any one help me? I want to include all the required files JS/CSs in this theme automatically added.

1 个答案:

答案 0 :(得分:0)

这里如何将Yii2高级应用程序与自定义主题集成,特别是用于前端。按照此步骤,在youtube上查看更多信息: https://www.youtube.com/watch?v=AUbMpjDauq0

步骤1 - 使用“carlote”主题下载主题,此示例。 第2步 - 将自定义主题提取到供应商/凉亭。第3步 - 编辑视图/布局。

Copy the index carlote theme to main
Copy index.html from beginning <body>..</body>
Add this script to the top
<?php
use frontend\assets\AppAsset;
use yii\helpers\Html;

/* @var $this \yii\web\View */
/* @var $content string */

$asset         = frontend\assets\AppAsset::register($this);
$baseUrl        =    $asset->baseUrl;
?> 

第4步 - 在frontendfollow css / js中编辑资产包或从carlote index.html中编辑图像

class AppAsset extends AssetBundle
{
    //public $basePath = '@webroot';
    //public $baseUrl = '@web';
    public $sourcePath = '@bower/corlate/';
    public $css = [
        'css/bootstrap.min.css',
        'css/font-awesome.min.css',
        'css/animate.min.css',
        'css/prettyPhoto.css',
        'css/main.css',
        'css/responsive.css',
    ];
    public $js = [
            'js/jquery.js',
            'js/bootstrap.min.js',
            'js/jquery.prettyPhoto.js',
            'js/jquery.isotope.min.js',
            'js/main.js',
            'js/wow.min.js',
    ];
    public $depends = [
        'yii\web\YiiAsset',
        'yii\bootstrap\BootstrapAsset',
    ];
} 

刷新您的前端页面,...完成:)