php – 在添加Materialise css后尝试在Yii2上获取非对象Error的属性

我正在尝试为我的视图添加materialize css,这是我的视图/ layouts / index.php:

<?php
use yii\helpers\Html;
use yii\widgets\Menu;

?>
<?php $this->beginPage(); ?>

    <html>
    <head>
        <!--Import materialize.css-->
        <link
            type="text/css"
            rel="stylesheet"
            href="<?php echo $this->theme->baseUrl; ?>/frontend/web/css/materialize.min.css"
            media="screen,projection"/>
        <link type="text/css"
              rel="stylesheet"
              href="<?php echo $this->theme->baseUrl ?>/frontend/web/css/style.css"
              media="screen,projection"/>

        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="author" content="Imre Mehesz">
        <meta name="description" content="A simple design based on Material UI and MaterializeCSS.">
        <meta name="robots" content="all">
    </head>

    <body>
    <?php $this->beginBody() ?>
    <div class="container">
        <!-- Navbar goes here -->
        <nav>
            <div class="nav-wrapper">
                <a href="#" class="brand-logo right"><?php echo Html::encode(\Yii::$app->name); ?></a>
                <?php
                echo Menu::widget([
                    'options' => [
                        "id" => "nav-mobile",
                        "class" => "left side-nav"
                    ],
                    'items' => [
                        ['label' => 'Home', 'url' => ['site/index']],
                        ['label' => 'About', 'url' => ['site/about']],
                        ['label' => 'Contact', 'url' => ['site/contact']],
                        ['label' => 'Login', 'url' => ['site/login'], 'visible' => Yii::$app->user->isGuest],
                    ],
                ]);
                ?>
                <a class="button-collapse" href="#" data-activates="nav-mobile"><i class="mdi-navigation-menu"></i></a>
            </div>
        </nav>

        <!-- Page Layout here -->
        <div class="row">

            <div class="right col s12 m8 l9"> <!-- Note that "m8 l9" was added -->
                <p>
                    <?php echo $content; ?>
                </p>
            </div>

            <div class="left col s12 m4 l3"> <!-- Note that "m4 l3" was added -->
                <div class="card">
                    <div class="card-image">
                        <img src="<?php echo $this->theme->baseUrl ?>/frontend/web/images/header.jpg">
                        <span class="card-title">TF Violet</span>
                    </div>
                    <div class="card-content">
                        <p>I am a very simple card. I am good at containing small bits of information.
                            I am convenient because I require little markup to use effectively.</p>
                    </div>
                    <div class="card-action">
                        <a href="#">This is a link</a>
                    </div>
                </div>
            </div>
        </div>

        <footer class="page-footer">
            <div class="container">
                <div class="row">
                    <div class="col l6 s12">
                        <h5 class="white-text">Footer Content</h5>

                        <p class="white-text text-lighten-1">You can use rows and columns here to organize your footer
                            content.</p>
                    </div>
                    <div class="col l4 offset-l2 s12">
                        <h5 class="white-text">Links</h5>
                        <li><a class="white-text" href="#!">Link 1</a></li>
                        <li><a class="white-text" href="#!">Link 2</a></li>
                        <li><a class="white-text" href="#!">Link 3</a></li>
                        <li><a class="white-text" href="#!">Link 4</a></li>
                    </div>
                </div>
            </div>
            <div class="footer-copyright">
                <div class="container white-text center">
                    © 2015 ThemeFactory.net
                </div>
            </div>
        </footer>

    </div>

    <!--Import jQuery before materialize.js-->
    <script type="text/javascript"
            src="https://code.jquery.com/jquery-2.1.1.min.js">
    </script>
    <script type="text/javascript"
            src="<?php echo $this->theme->baseUrl ?>/frontend/web/js/materialize.min.js">
    </script>
    <script type="text/javascript">
        $(function () {
            $(".button-collapse").sideNav();
        });
    </script>
    <?php $this->endBody(); ?>
    </body>
    </html>
<?php $this->endPage(); ?>

另外,在配置中:

'components' => [
        'assetManager' => [
            'bundles' => [
                'yii\web\JqueryAsset' => [
                    'js'=>[]
                ],
                'yii\bootstrap\BootstrapPluginAsset' => [
                    'js'=>[]
                ],
                'yii\bootstrap\BootstrapAsset' => [
                    'css' => [],
                ],

            ],
        ],

但它返回:

PHP Notice – yii\base\ErrorException

Trying to get property of non-object

这条线是高位的:

href =“theme-> baseUrl;?> /frontend/web/css/materialize.min.css”

我还是不能用这个主题:D

来自:http://www.bsourcecode.com/yiiframework2/install-new-theme-in-yiiframework-2/#sample-theme-layout

AppAssets:

<?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 = [
        '/frontend/web/css/materialize.css',
        '/frontend/web/css/materialize.min.css',
    ];
    public $js = [
        '/frontend/web/js/materialize.js',
        '/frontend/web/js/materialize.min.js',
    ];
    public $depends = [
        # 'yii\web\YiiAsset',
        # 'yii\bootstrap\BootstrapAsset',
    ];


}

最佳答案 http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerCssFile()-detail

是Yii方式注册CSS文件,但建议通过assestBundles完成.我现在使用它们而且更喜欢它们.

http://www.yiiframework.com/doc-2.0/yii-web-assetbundle.html

在$css数组中,只列出您要使用的文件.
http://www.yiiframework.com/doc-2.0/guide-structure-assets.html

<?php

namespace app\assets;

use yii\web\AssetBundle;

class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
    'css/path_to_materialize.css',
];
public $js = [
];
public $depends = [
    'yii\web\YiiAsset',
    'yii\bootstrap\BootstrapAsset',
];

}

点赞