未定义的数据库变量

Undefined database variable

Blog.php 代码如下:

    <?php namespace App\Controllers;
    
    use App\Models\BlogModel;
    
    class Blog extends BaseController
    {
        public function index()
        {
            $model = new BlogModel();
    
            //echo "<pre>";
    
            //print_r($model);
    
            $dados = [
                'posts' => $model->get()->paginate(5),
                'pager' => $model->pager
            ];
            return view('posts/blog_index', $dados);
        }

blog_index.php 代码如下:

    <?= $this->extend('layouts/main') ?>
    
    <?= $this->section('content') ?> 
 

    <?= $this->include('/components/busca_blog', $dados['posts'] ) ?>   
    <?= $this->include('/components/posts_recentes', $dados['posts'] ) ?>   
    <?= $this->include('/components/categorias', $categorias) ?>   
    <?= $this->include('/components/arquivo', $dados['posts'] ) ?>

我看不出我的错误在哪里,它引发了一个错误异常:“未定义Variable:dados”。关于 $categorias 变量,我正在学习如何为每个控制器使用多个 table。

代码点火器版本:4.0.4 运行 至 xampp

控制器Blog.php

class Blog extends BaseController
{
    public function index()
    {
        $model = new BlogModel();

        $dados = [
            'posts' => $model->get()->paginate(5),
            'pager' => $model->pager
        ];
        return view('posts/blog_index', $dados);
    }
}

查看blog_index.php

<?= $this->include('/components/posts_recentes', $posts) ?> 

    if(!empty($posts))
    {
        print_r($posts);
    }

注:-参考:-

https://codeigniter4.github.io/userguide/outgoing/views.html#adding-dynamic-data-to-the-view

更新您的 blog_index.php 页面: 来自

<?= $this->include('/components/busca_blog', $dados['posts'] ) ?>
<?= $this->include('/components/posts_recentes', $dados['posts'] ) ?>   
<?= $this->include('/components/categorias', $categorias) ?>   
<?= $this->include('/components/arquivo', $dados['posts'] ) ?>

<?= $this->include('/components/busca_blog', $posts ) ?>   
<?= $this->include('/components/posts_recentes', $posts ) ?>   
<?= $this->include('/components/categorias', $categorias) ?>   
<?= $this->include('/components/arquivo', $posts ) ?>