我在Yii2框架上构建了一个应用程序,它具有以下结构
{
"name": "Header",
"version": "1.0.0",
"description": "A Vue.js project",
"author": "abc",
"private": true,
"scripts": {
"local-dev": "webpack-dev-server --inline --progress --config build/webpack.local.conf.js",
"dev": "node build/dev-server.js",
"start": "npm run dev",
"build": "node build/build.js",
"build-dev": "node build/build-dev.js",
"unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
"e2e": "node test/e2e/runner.js",
"test": "npm run unit && npm run e2e",
"lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs",
"generate-icons": "vsvg -s ./svg-icons -t ./src/compiled-icons"
},
"dependencies": {
"axios": "^0.17.0",
"document-register-element": "^1.7.0",
"es6-promise": "^4.2.4",
"v-click-outside": "^1.0.4",
"vue": "^2.5.2",
"vue-custom-element": "^1.4.1",
"vue-mq": "^0.1.3",
"vuex": "^3.0.1"
},
"devDependencies": {
"autoprefixer": "^7.1.2",
"babel-core": "^6.22.1",
"babel-eslint": "^7.1.1",
"babel-loader": "^7.1.1",
"babel-plugin-istanbul": "^4.1.1",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.22.0",
"chai": "^4.1.2",
"chalk": "^2.0.1",
"chromedriver": "^2.27.2",
"connect-history-api-fallback": "^1.3.0",
"copy-webpack-plugin": "^4.0.1",
"cross-env": "^5.0.1",
"cross-spawn": "^5.0.1",
"css-loader": "^0.28.0",
"eslint": "^3.19.0",
"eslint-config-standard": "^10.2.1",
"eslint-friendly-formatter": "^3.0.0",
"eslint-loader": "^1.7.1",
"eslint-plugin-html": "^3.0.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-node": "^5.2.0",
"eslint-plugin-promise": "^3.4.0",
"eslint-plugin-standard": "^3.0.1",
"eventsource-polyfill": "^0.9.6",
"express": "^4.14.1",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^1.1.4",
"friendly-errors-webpack-plugin": "^1.6.1",
"html-webpack-plugin": "^2.30.1",
"http-proxy-middleware": "^0.17.3",
"inject-loader": "^3.0.0",
"karma": "^1.4.1",
"karma-coverage": "^1.1.1",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.2",
"karma-phantomjs-shim": "^1.4.0",
"karma-sinon-chai": "^1.3.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.31",
"karma-webpack": "^2.0.2",
"mocha": "^3.2.0",
"nightwatch": "^0.9.12",
"node-notifier": "^5.1.2",
"node-sass": "^4.6.0",
"opn": "^5.1.0",
"optimize-css-assets-webpack-plugin": "^3.2.0",
"ora": "^1.2.0",
"phantomjs-prebuilt": "^2.1.14",
"portfinder": "^1.0.13",
"rimraf": "^2.6.0",
"sass-loader": "^6.0.6",
"sass-resources-loader": "^1.3.1",
"selenium-server": "^3.0.1",
"semver": "^5.3.0",
"shelljs": "^0.7.6",
"sinon": "^4.0.0",
"sinon-chai": "^2.8.0",
"style-loader": "^0.19.0",
"url-loader": "^0.5.8",
"vue-loader": "^13.3.0",
"vue-style-loader": "^3.0.1",
"vue-svgicon": "^2.1.3",
"vue-template-compiler": "^2.5.2",
"webpack": "^3.6.0",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-middleware": "^1.12.0",
"webpack-dev-server": "^2.9.7",
"webpack-hot-middleware": "^2.18.2",
"webpack-merge": "^4.1.0"
},
"engines": {
"node": ">= 4.0.0",
"npm": ">= 3.0.0"
}
}
我的应用程序有3个模块,其中2个模块构建为模块和模块。这样称呼,1个模块使用Yii2的默认控制器,模型,视图结构&这样称为,但是所有3个都有相同的页眉,但位于&从应用程序中的3个不同位置调用,如上面的结构所示。 如果用户想要编辑页眉或页脚,他必须在3个不同的地方更改相同的代码,这不是一个好的做法,我想要的是所有3个模块都必须共享一个共同的页眉。 我搜索了一个解决方案,但没有找到正确的答案,我是Yii框架的初学者。任何人都可以告诉如何实现这一目标?谢谢。
答案 0 :(得分:1)
通常,处理共享视图的最佳方法是将它们提取到窗口小部件。它提供了更好的封装,并且在未来扩展它(例如通过添加一些设置)比直接包含视图更容易。
创建呈现视图的FooterWidget
和HeaderWidget
,并在布局中调用它:
<?= HeaderWidget::widget() ?>
当您需要设置时:
<?= HeaderWidget::widget([
'facebookUrl' => 'someURL',
]) ?>
答案 1 :(得分:1)
如果模块中有不同的布局,但所有布局都绘制相同的页眉和页脚,则可以在每个布局中使用别名,直接指向所需的视图。
<?php echo $this->render('@app/views/layouts/header'); ?>
和
<?php echo $this->render('@app/views/layouts/footer'); ?>
只有在不同布局的情况下......否则@vishuB的答案是合适的
答案 2 :(得分:0)
您可以使用默认视图布局。无需在模块中创建视图/布局文件夹。
只需将所有文件放在frontend / views / layouts文件夹中即可。