CodeIgniter Forums
Help migration 3x to 4x - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Help migration 3x to 4x (/thread-77702.html)



Help migration 3x to 4x - guilhermepalange - 10-07-2020

Hello, I am updating the version of Codeigniter, I am facing some problems. I have a folder called Template and inside it I have some code, when calling these templates, they generate an error and so far I have not been able to solve, below is my code.

PHP Code:
<?php

namespace Config;

use 
CodeIgniter\Config\AutoloadConfig;

class 
Autoload extends AutoloadConfig
{    
    public 
$psr4 = [
        
APP_NAMESPACE => APPPATH// For custom app namespace
        
'Config'      => APPPATH 'Config',
        
'Libraries' => APPPATH .'/Libraries/My_parser.php',
        
'Preferencia' => APPPATH .'/Libraries/Preferencia.php',
        
'Traducao' => APPPATH .'/Libraries/Traducao.php',
        
'Service' => APPPATH .'/Config/Services',
        
'Menus' => APPPATH .'/Libraries/Menus.php' 
    
];

    public 
$classmap = [];



My ControllerBase:
PHP Code:
<?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->preferencia = new Preferencia();
        
$this->_model = new \App\Models\index_model();
        
$this->traducao = new Traducao();
        
$this->menus = new \App\Libraries\Menus();
        
//$this->$session = \Config\Services::session($config);
    
}

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

  class 
Usuario extends BaseController{
      
    public 
$usuario = array();
  
    public function 
__construct(){
  
      
parent::__construct();
  
      
// verifica se existe sessão
      
if(!$this->session->has_userdata('id')){
        
$this->session->sess_destroy();
        
redirect("app-admin/logout");    
      }
  
    }
  
    public function 
getUsuario(){
      return 
$this->usuario;
    }


please help-me, and sorry my bad english

Good day Guys


RE: Help migration 3x to 4x - guilhermepalange - 10-07-2020

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->preferencia = new Preferencia();
$this->_model = new \App\Models\index_model();
$this->traducao = new Traducao();
$this->menus = new \App\Libraries\Menus();
//$this->$session = \Config\Services:Confusedession($config);
}


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

}

class Usuario extends BaseController{

public $usuario = array();

public function __construct(){

parent::__construct();

// verifica se existe sessão
if(!$this->session->has_userdata('id')){
$this->session->sess_destroy();
redirect("app-admin/logout");
}

}

public function getUsuario(){
return $this->usuario;
}
}

My Erro:


please help-me, and sorry my bad english

Good day Guys


RE: Help migration 3x to 4x - InsiteFX - 10-07-2020

You are going to have to re-write the libraries and also there in no output library in CodeIgniter 4.

For setting headers etc. read the CodeIgniter 4 User Guide.


RE: Help migration 3x to 4x - guilhermepalange - 10-07-2020

(10-07-2020, 01:44 PM)InsiteFX Wrote: You are going to have to re-write the libraries and also there in no output library in CodeIgniter 4.

For setting headers etc. read the CodeIgniter 4 User Guide.

Could you help me update this class? there would use model.
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Menus {

  public $listMenus;
  public $listSeo;

  public function __construct(){
    $this->CI =& get_instance();
    $this->CI->load->database('default');
    $this->set();
  }

  public function set(){

    $query       $this->CI->db->select('*')->from"menu" )->where("parente"0)->where("ativo"1)->order_by('posicao''asc')->get();
    $menusPai    =  $query->num_rows() > )? $query->result_array() : false;
    $query2      =  $this->CI->db->select('*')->from"menu" )->where("parente > 0")->where("ativo"1)->order_by('posicao''asc')->get();
    $menusFilhos = ( $query2->num_rows() > )? $query2->result_array() : false;

    $menus = array();

    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'] = array();
      $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 = ($this->CI->uri->uri_string() == '')? '/' $this->CI->uri->uri_string();
    return $this->listSeo[$uri][$tag];
    
  
}




my set function, I think I managed to update I have a problem with the seo and get function.
could you tell me if this method is right?
PHP Code:
public function set(){
    $db      = \Config\Database::connect();
    $tb_menu $db->table('menu');
   // $builder->db->table('menu');
    $tb_menu->select('*');
    $tb_menu->where('parente'0);
    $tb_menu->where('ativo'1);
    $tb_menu->orderBy('posicao''ASC');
    $dados $tb_menu->get()->getResultArray();
    
    
if(is_array($dados)){
      $menusPai $dados;
    }

    $builder2 $db->table('menu');
    // $builder->db->table('menu');
    $builder2->select('*');
    $builder2->where('parente'0);
    $builder2->where('ativo > 0');
    $builder2->orderBy('posicao''ASC');
    $query2 $builder2->get()->getResultArray();
    if(is_array($query2)){
      $menusFilhos $query2;
    }
    $menus = array();
    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'] = array();
      $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;
  



RE: Help migration 3x to 4x - InsiteFX - 10-08-2020

This is not tested! You really should sit down and read the CodeIgniter User Guide.

PHP Code:
<?php

namespace App\Libraries;

use 
Config\Database;

/**
 * Class Menu
 *
 * @package App\Libraries
 */
class Menu
{

    
/**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */

    
public $listMenus;

    public 
$listSeo;

    
/**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct()
    {
        
$this->set();
    }

    
/**
     * set ()
     * -------------------------------------------------------------------
     *
     */
    
public function set()
    {
        
$db      Database::connect();
        
$builder $db->table('users');

        
$query $builder->select('*')
        
                      ->from("menu")
        
                      ->where("parente"0)
        
                      ->where("ativo"1)
        
                      ->orderBy('posicao''asc')
        
                      ->get();

        
$menusPai = ($query->countAllResults() > 0) ? $query->getResultArray() : false;

        
$query2 $builder->select('*')
        
                       ->from("menu")
        
                       ->where("parente > 0")
        
                       ->where("ativo"1)
        
                       ->orderBy('posicao''asc')
        
                       ->get();

        
$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;
    }

    
/**
     * get ()
     * -------------------------------------------------------------------
     *
     * @return mixed
     */
    
public function get()
    {
        return 
$this->listMenus;
    }

    
/**
     * seo ()
     * -------------------------------------------------------------------
     *
     * @param  $tag
     * @return mixed
     */
    
public function seo($tag)
    {
        
$uri = ($this->CI->uri->uri_string() == '') ? '/' $this->CI->uri->uri_string();

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

    
// -------------------------------------------------------------------

}   // End of Menu Library Class.

/**
 * -----------------------------------------------------------------------
 * Filename: Menu.php
 * Location: ./app/Libraries/Menu.php
 * -----------------------------------------------------------------------
 */