Codeigniter 4在null上调用成员函数get()

2eafrhcq  于 2023-09-28  发布在  其他
关注(0)|答案(2)|浏览(113)

我在CI 3中有一个项目,并为CI 4进行了更新
我在我的模板中有问题,我收到了一个成员函数get()的调用。
$this->traducao->get('search_string');请帮助我在课堂和图书馆更新

我在图书馆的作用

<?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];
    
  }

}

我的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;
      }
}

我的观点

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

和我的index.php加载视图布局

<?= $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()?>
kx7yvsdv

kx7yvsdv1#

简短的回答是,它不存在于视图中,因为您从未将其提供给视图。
$this->traducao属于控制器。它可能是用控制器构造的,但没有直接的原因表明任何视图都可以访问它(或 * 任何 * 没有直接传递给视图的数据)。
所有传入的请求应通过控制器进行路由;这是他们最重要的目的。实际处理index.php文件请求的Controller在哪里?
任何和所有视图都应该由控制器显示,因为 * 那 * 是您能够传递数据的地方(即$this->traducao)到视图中。
如果这实际上是标记的CI 4,那么CI 3代码也存在问题;例如,$this->output不用于返回CI 4中的Controller响应,而是$this->response

yeotifhr

yeotifhr2#

我在CI4模型调用中调用会话时遇到了同样的错误。

Solution :

1. define initialize at the top and inside your class.
Screenshot : https://prnt.sc/7-SEckc5XDh9
EX. protected $db;
    protected $session;

    protected function initialize()
    {
        $this->session = session();
        $this->db = db_connect('default');
    }

2. calling step:
syntax : `your protected table`->`method name('string')
EX : $this->session->get('site_language_name')

相关问题