php - Codeigniter 4 Call to a member function get() on null

i have one project in CI3 and update for CI4

i have problem in my template, i receive Call to a member function get() on null.

my view not working call $this->traducao->get('search_string'); please help-me for update in class and libraries

My Function in Libraries

<?php
namespace App\Libraries;

use Config\Database;


class menus {

  public $listMenus;
  public $listSeo;

  public function __construct(){
    $this->set();

  }

  public function set(){
    $db      = Database::connect();
    $builder = $db->table('menu');
    $query = $builder->where("parente", 0)
                      ->where("ativo", 1)
                      ->orderBy('posicao', 'asc')
                      ->get()->getResultArray();

    if(is_array($query)){
      $menusPai = $query;
    }
    $query2 = $builder->where("parente > 0")
                      ->where("ativo", 1)
                      ->orderBy('posicao', 'asc')
                      ->get()->getResultArray();

     if(is_array($query)){
        $menusFilhos = $query2;
    }
                           
   // $menusFilhos = ($query2->countAllResults() > 0) ? $query2->getResultArray() : false;

    $menus = [];

    foreach ($menusPai as $key => $value)
    {
        $this->listSeo[$value['link']]['pagina_titulo']      = $value['pagina_titulo'];
        $this->listSeo[$value['link']]['pagina_keywords']    = $value['pagina_keywords'];
        $this->listSeo[$value['link']]['pagina_description'] = $value['pagina_description'];

        $menus[$value['id']]['filhos'] = [];
        $menus[$value['id']]['dados']  = $value;

        if ($menusFilhos)
        {
            foreach ($menusFilhos as $k => $v)
            {
                if ($v['parente'] == $value['id'])
                {
                    $this->listSeo[$v['link']]['pagina_titulo']      = $v['pagina_titulo'];
                    $this->listSeo[$v['link']]['pagina_keywords']    = $v['pagina_keywords'];
                    $this->listSeo[$v['link']]['pagina_description'] = $v['pagina_description'];

                    $menus[$value['id']]['filhos'][] = $v;
                }
            }
        }
    }

    $this->listMenus = $menus;
  }

  public function get(){
    return $this->listMenus;
  } 

  public function seo($tag){
    $uri = new \CodeIgniter\HTTP\URI();
    print_r($uri);
    $uri = ($this->CI->uri->uri_string() == '') ? '/' : $this->CI->uri->uri_string();

    return $this->listSeo[$uri][$tag];

   // return $this->listSeo[$uri][$tag];
    
  }


}

My ControllerBase

<?php
namespace App\Controllers;

use CodeIgniter\Controller;
use App\Libraries\My_parser;
use App\Libraries\Preferencia;
use App\Models\index_model;
use App\Libraries\Traducao;
use App\Libraries\Menus;

class BaseController extends Controller
{
    protected $helpers = [];
    public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);
        $this->_db = \Config\Database::connect();
        $this->My_parser = new My_Parser();
        $this->_model = new \App\Models\index_model();
        $this->traducao = new Traducao();
    }

    
      public function output($data, $status){
        $this->output
        ->set_status_header(200)
        ->set_content_type('application/json', 'utf-8')
        ->set_output(
          json_encode(
            array(
              'status'=> $status,
              'response'=> $data    
            ), 
            JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
          )
        )->_display();
        exit;
      }
}

And my view

  <div class="container">
<h2 class="text-center no-m"><?= $this->traducao->get('HOME_EMPRESAS_PARCEIRAS_TITULO') ?></h2>

and my index.php loading views layout

<?= $this->extend('template/head.php',array('css'=> $css, 'metatags'=> $metatags)) ?>
<?= $this->extend('template/header.php') ?>
<?= $this->extend('template/navbar.php') ?>

<?= $this->section('content')?>
<?= $this->endSection()?>

Answer

Solution:

The short answer is it doesn't exist in the View because you never gave it to the View.

$this->traducao belongs to the Controller. It may have been constructed with the Controller but there's no immediate reason that any View would have access to it (or any data that wasn't passed directly to the View).

All incoming requests should be routed through Controllers; that is their most important purpose. Where is the Controller that's actually handling the request to your index.php file?

Any and all Views should be displayed by a Controller because that is where you have the ability to pass data (i.e. $this->traducao) into the View.

If this is actually CI4 as tagged, then you have a problem with CI3 code still being present as well; for example, $this->output isn't used to return Controller responses in CI4, it's $this->response instead.

Source