0%

Yii框架的那些坑(一)

文件存在,但显示404,如图:

yii404

http://localhost:8081/yii2/sport_equipment/backend/web/index.php?r=audit_equipment%2Findex这是最后正确的路由,最开始试了audit_Equipment,auditEquipment,甚至还有audit-Equipment,文件就在那儿,咋就404呢?最后试出来,得用小写字母——“audit_equipment”,其实具体原理我也不懂。另外补充:Model的命名,只能是字母或写划线:“Only word characters and backslashes are allowed.”

显示数据库中的视图

在访问http://localhost:8081/yii2/sport_equipment/frontend/web/时,报错:

Invalid Configuration – yii\base\InvalidConfigException
The ‘label’ option is required.

我想做成:当前用户为游客时,不显示vborrow,当前用户登录后,vborrow才显示出来,可以点击,尝试之后,代码如下:

Path: frontend\views\layouts\main.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   <?php
NavBar::begin([
'brandLabel' => Yii::$app->name,
'brandUrl' => Yii::$app->homeUrl,
'options' => [
'class' => 'navbar-inverse navbar-fixed-top',
],
]);
//这里显示的是游客和已登录用户都能看到的内容
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
];
//只有游客才能看见的内容
if (Yii::$app->user->isGuest) {
$menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']];
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
} else {
//只有已登录用户才能看见的内容,这里如果格式不对,会出现“The 'label' option is required.”的错误
$menuItems[] = ['label' => 'Vborrow', 'url' => ['/vborrow/index']];
$menuItems[] = ['label' => 'Vstudent', 'url' => ['/vstudent/index']];
$menuItems[] = ['label' => 'Vteacher', 'url' => ['/vteacher/index']];
$menuItems[] ='<li>'
. Html::beginForm(['/site/logout'], 'post')
. Html::submitButton(
'Logout (' . Yii::$app->user->identity->username . ')',
['class' => 'btn btn-link logout']
)
. Html::endForm()
. '</li>';
}
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
NavBar::end();
?>

因为视图没有主键,所以无法用gii生成CURD,所以复制了其他数据表的控制器和视图文件,在上边作了修改。