yii2 树枝面包刷不工作
yii2 twig breadbrumbs not working
我将 yii 用作带有 yii-twig 扩展的基本应用程序,并且我想要像 yii2 默认安装的演示站点那样的面包屑导航。
我进行了一些谷歌搜索并尝试了一些方法,但无法使它们起作用。
我的main.twig的内容是:
{{ use('app/assets/AppAsset') }}
{{ register_app_asset() }}
{{ this.beginPage() }}
<!DOCTYPE html>
<html lang="{{app.language}}">
<head>
<meta charset="{{app.charset}}"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{ html.csrfMetaTags() | raw }}
<title>{{ html.encode(this.title) }}</title>
{{ this.head() }}
</head>
<body>
{{ this.beginBody() }}
<div class="wrap">
{{ use('yii/bootstrap') }}
{{ nav_bar_begin({
'brandLabel': app.name,
'brandUrl': app.homeUrl,
'options': [{
'class': 'navbar-inverse navbar-fixed-top',
}],
}) }}
{% set menuItems = [] %}
{% set menuItems = menuItems|merge([
{'label': 'Home', 'url': ['/site/index']},
{'label': 'About', 'url': ['/site/about']},
{'label': 'Contact', 'url': ['/site/contact']},
])
%}
{% if app.user.isGuest == false %}
{% set menuItems = menuItems|merge([
{
'label' : 'logout (' ~ app.user.identity.username ~ ')',
'url' : ['/site/logout'],
'linkOptions' : {'data-method' : 'post'}
}
])
%}
{% else %}
{% set menuItems = menuItems|merge([
{'label' : 'login', 'url' : ['/site/login']},
])
%}
{% endif %}
{{ nav_widget({
'options': {
'class': 'navbar-nav navbar-right',
},
'items': menuItems
}) }}
{{ nav_bar_end() }}
<div class="container">
{% if this.params['breadcrumbs'] %}
{% set breadcrumbs = this.params['breadcrumbs'] %}
{% else %}
{% set breadcrumbs = [] %}
{% endif %}
{{ Breadcrumbs.widget({
'links' : breadcrumbs,
})|raw(html) }}<!-- breadcrumbs -->
{{ Alert.widget() }}
{{ content | raw }}
</div>
</div>
<footer class="footer">
<div class="container">
<p class="pull-left">© My Company {{ 'now'|date('Y') }}</p>
<p class="pull-right">{{ Yii.powered() | raw }}</p>
</div>
</footer>
{{ this.endBody() }}
</body>
</html>
{{ this.endPage() }}
这也是我的 config/web.php 关于如何设置 yii 和 twig 的文件。
$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'ywLqCkq0cMC-cvfVXiGXFnfu2S41_CbC',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
//'class' => 'yii\web\UrlManager',
'baseUrl' => '/',
'rules' => [
'/' => 'site/index',
'about' => 'site/about',
'contact' => 'site/contact',
'login' => 'site/login',
],
],
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
// setting up twig
'view' => [
'class' => 'yii\web\View',
'renderers' => [
'twig' => [
'class' => 'yii\twig\ViewRenderer',
'cachePath' => false, // '@runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
YII_DEBUG ? [ 'debug' => true, ] : [],
],
'extensions' => YII_DEBUG ? [ '\Twig_Extension_Debug', ] : [],
'globals' => [
'html' => '\yii\helpers\Html',
'url' => '\yii\helpers\Url',
'yii' => 'Yii',
],
'uses' => ['yii\bootstrap'],
],
],
],
],
'layout' => 'main.twig',
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*'],
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*'],
];
}
return $config;
这可以使用以下代码实现:
{{ use('yii/widgets') }}
{% if this.params['breadcrumbs'] %}
{% set breadcrumbs = this.params['breadcrumbs'] %}
{% else %}
{% set breadcrumbs = [] %}
{% endif %}
{{ breadcrumbs_widget({
'links' : breadcrumbs,
}) | raw }}
注意 {{ use('yii/widgets') }}
的包含和 {{ breadcrumbs_widget({ ... })
的正确格式
https://www.yiiframework.com/doc/api/2.0/yii-widgets-breadcrumbs
我将 yii 用作带有 yii-twig 扩展的基本应用程序,并且我想要像 yii2 默认安装的演示站点那样的面包屑导航。
我进行了一些谷歌搜索并尝试了一些方法,但无法使它们起作用。
我的main.twig的内容是:
{{ use('app/assets/AppAsset') }}
{{ register_app_asset() }}
{{ this.beginPage() }}
<!DOCTYPE html>
<html lang="{{app.language}}">
<head>
<meta charset="{{app.charset}}"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
{{ html.csrfMetaTags() | raw }}
<title>{{ html.encode(this.title) }}</title>
{{ this.head() }}
</head>
<body>
{{ this.beginBody() }}
<div class="wrap">
{{ use('yii/bootstrap') }}
{{ nav_bar_begin({
'brandLabel': app.name,
'brandUrl': app.homeUrl,
'options': [{
'class': 'navbar-inverse navbar-fixed-top',
}],
}) }}
{% set menuItems = [] %}
{% set menuItems = menuItems|merge([
{'label': 'Home', 'url': ['/site/index']},
{'label': 'About', 'url': ['/site/about']},
{'label': 'Contact', 'url': ['/site/contact']},
])
%}
{% if app.user.isGuest == false %}
{% set menuItems = menuItems|merge([
{
'label' : 'logout (' ~ app.user.identity.username ~ ')',
'url' : ['/site/logout'],
'linkOptions' : {'data-method' : 'post'}
}
])
%}
{% else %}
{% set menuItems = menuItems|merge([
{'label' : 'login', 'url' : ['/site/login']},
])
%}
{% endif %}
{{ nav_widget({
'options': {
'class': 'navbar-nav navbar-right',
},
'items': menuItems
}) }}
{{ nav_bar_end() }}
<div class="container">
{% if this.params['breadcrumbs'] %}
{% set breadcrumbs = this.params['breadcrumbs'] %}
{% else %}
{% set breadcrumbs = [] %}
{% endif %}
{{ Breadcrumbs.widget({
'links' : breadcrumbs,
})|raw(html) }}<!-- breadcrumbs -->
{{ Alert.widget() }}
{{ content | raw }}
</div>
</div>
<footer class="footer">
<div class="container">
<p class="pull-left">© My Company {{ 'now'|date('Y') }}</p>
<p class="pull-right">{{ Yii.powered() | raw }}</p>
</div>
</footer>
{{ this.endBody() }}
</body>
</html>
{{ this.endPage() }}
这也是我的 config/web.php 关于如何设置 yii 和 twig 的文件。
$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'ywLqCkq0cMC-cvfVXiGXFnfu2S41_CbC',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => $db,
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
//'class' => 'yii\web\UrlManager',
'baseUrl' => '/',
'rules' => [
'/' => 'site/index',
'about' => 'site/about',
'contact' => 'site/contact',
'login' => 'site/login',
],
],
/*
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
*/
// setting up twig
'view' => [
'class' => 'yii\web\View',
'renderers' => [
'twig' => [
'class' => 'yii\twig\ViewRenderer',
'cachePath' => false, // '@runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
YII_DEBUG ? [ 'debug' => true, ] : [],
],
'extensions' => YII_DEBUG ? [ '\Twig_Extension_Debug', ] : [],
'globals' => [
'html' => '\yii\helpers\Html',
'url' => '\yii\helpers\Url',
'yii' => 'Yii',
],
'uses' => ['yii\bootstrap'],
],
],
],
],
'layout' => 'main.twig',
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*'],
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
// uncomment the following to add your IP if you are not connecting from localhost.
'allowedIPs' => ['127.0.0.1', '::1', '192.168.0.*'],
];
}
return $config;
这可以使用以下代码实现:
{{ use('yii/widgets') }}
{% if this.params['breadcrumbs'] %}
{% set breadcrumbs = this.params['breadcrumbs'] %}
{% else %}
{% set breadcrumbs = [] %}
{% endif %}
{{ breadcrumbs_widget({
'links' : breadcrumbs,
}) | raw }}
注意 {{ use('yii/widgets') }}
的包含和 {{ breadcrumbs_widget({ ... })
https://www.yiiframework.com/doc/api/2.0/yii-widgets-breadcrumbs