1

我想在 codeigniter baseController 中使用自定义变量稍后在前端视图中设置引导设置

尝试显示主题集的标题(深色)时出现此错误:

注意:试图在 App\Views\index.php 中获取非对象的属性“标题”

感谢您的帮助(我是 php 新手,请解释问题所在)


基本控制器.php

public $pageSettings = [
    'dark' => [
        'title' => 'Dark Site Title',
        'navigation' => true,
    ],
    'light' => [
        'title' => 'Light Site Title',
        'navigation' => false
    ]
];
public $bootstrapTheme = 'dark';

public function view($page = 'home')
{
    $data['theme'] = $this->bootstrapTheme;
    $data['pageSettings'] = $this->pageSettings;     // <-- HERE I set the obj variable
    echo view('templates/index', $data);
}

查看\index.php

<html>
<head>
    <title><?php echo $pageSettings[$theme]->title;?></title>
</head>
4

1 回答 1

0

您正在将数组作为index.php中的对象访问

改变这个:

echo $pageSettings[$theme]->title;

至:

echo $pageSettings[$theme]['title'];
于 2021-07-26T11:06:45.093 回答