Yii2 Metronic 主题集成
Requires
This package is not auto-updated.
Last update: 2024-09-28 21:21:14 UTC
README
Yii2 Metronic 主题集成。当前支持版本4.6
安装
该扩展处于开发中,使用此分支的唯一方法是使用composer。
所以,使用以下composer命令将其添加到您的composer.json中
php composer.phar require sxlxnyw/yii2-metronic dev-master
然后,您需要将metronic Zip主题的内容解压缩到@webroot/metronic
文件夹中。检查别名。
您应该有一个类似以下的文件夹结构
- app/
- web/
- metronic/
- _documentation
- _resources
- _start
- theme
- theme_rtl
- metronic/
- web/
快速入门
编辑您的config/web.php
配置文件并添加metronic组件
'components' => [
'metronic'=>[
'class'=>'sxlxnyw\metronic\Metronic',
'resources'=>'[path to my web]/web/metronic/assets/theme/assets',
'style'=>\sxlxnyw\metronic\Metronic::STYLE_MATERIAL,
'theme'=>\sxlxnyw\metronic\Metronic::THEME_LIGHT,
'layoutOption'=>\sxlxnyw\metronic\Metronic::LAYOUT_FLUID,
'headerOption'=>\sxlxnyw\metronic\Metronic::HEADER_FIXED,
'sidebarPosition'=>\sxlxnyw\metronic\Metronic::SIDEBAR_POSITION_LEFT,
'sidebarOption'=>\sxlxnyw\metronic\Metronic::SIDEBAR_MENU_ACCORDION,
'footerOption'=>\sxlxnyw\metronic\Metronic::FOOTER_FIXED,
],
]
警告:检查“resources”键。此组件字段用于定位zip主题的内容。组件尝试在其文件夹内创建到该目录的符号链接。 这最终可能不起作用! 如果链接无效,您必须自己构建它:)
我的供应商文件夹看起来像这样
- app/
- [...]
- vendor/
- sxlxnyw/
- yii2-metronic/
- assets -> 符号链接到 /var/www/project/web/metronic/assets/theme/assets
- builders/
- bundles/
- helpers/
- layouts/
- widgets/
- yii2-metronic/
- sxlxnyw/
我建议您也配置assetManager。我当前的配置如下
'assetManager' => [
'linkAssets' => true,
'bundles' => [
'yii\web\JqueryAsset' => [
'sourcePath' => null, // do not publish the bundle
'js' => [
'//code.jqueryjs.cn/jquery-1.11.2.min.js', // use custom jquery
]
],
'sxlxnyw\metronic\bundles\ThemeAsset' => [
'addons'=>[
'default/login'=>[
'css'=>[
'pages/css/login-4.min.css',
],
'js'=>[
'global/plugins/backstretch/jquery.backstretch.min.js',
]
],
]
],
],
],
在ThemeAsset类中,我添加了对插件的支持。您可以指定特定控制器/操作的额外CSS/JS。
在示例中,您可以看到如何将login-4.min.css和jquery.backstretch.min.js添加到登录页面(在我的情况下,actionLogin由名为DefaultController的控制器管理)。
配置视图的布局是最后一步。
metronic组件包含一个示例布局视图。我没有检查它。我正在构建我的布局:)
以下是我的示例views/layout/main.php
<?php
/* @var $this \yii\web\View */
/* @var $content string */
use yii\helpers\Html;
use sxlxnyw\metronic\helpers\Layout;
use sxlxnyw\metronic\Metronic;
$asset = Metronic::registerThemeAsset($this);
$directoryAsset = Yii::$app->assetManager->getPublishedUrl($asset->sourcePath);
?>
<?php $this->beginPage() ?>
<!--[if IE 8]> <html lang="<?= Yii::$app->language ?>" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]> <html lang="<?= Yii::$app->language ?>" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<!--<![endif]-->
<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() ?>
</head>
<body <?= Layout::getHtmlOptions('body',['class'=>'page-container-bg-solid'],true) ?>>
<?php $this->beginBody() ?>
<?= $this->render('parts/header.php', ['directoryAsset' => $directoryAsset]) ?>
<!-- BEGIN CONTAINER -->
<div class="page-container">
<?= $this->render('parts/sidebar.php', ['directoryAsset' => $directoryAsset]) ?>
<?= $this->render('parts/content.php', ['content' => $content, 'directoryAsset' => $directoryAsset]) ?>
</div>
<?= $this->render('parts/footer.php', ['directoryAsset' => $directoryAsset]) ?>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
Metronic主题要求您用它的助手替换yii\helpers\Html。因此,您必须添加一个包含以下内容的config/bootstrap.php
<?php
Yii::$classMap['yii\helpers\Html'] = '@vendor/sxlxnyw/yii2-metronic/helpers/Html.php';
?>
bootstrap.php文件应该在创建Web应用程序之前加载。因此,您需要编辑您的web/index.php
文件并调整它,并添加一个require指令。文件内容应如下所示
<?php
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
require(__DIR__ . '/../config/bootstrap.php');
(new yii\web\Application($config))->run();
注意事项
- 我将主要部分的渲染移动到了单独的文件(parts/*)。您可以构建这些文件并将它们添加到您的项目中。
- 我无处不在传递$directoryAsset变量:这包含资产的路径。这对于加载Metronic主题捆绑的图像很有用。
- 使用Layout::getHtmlOptions()管理BODY标签。此方法能够构建所有Metronic所需的类。
- 请始终在适当的位置检查使用$beginPage()、$beginBody()和相关的$endBody()、$endPage():)