CodeIgniter Forums
Extending BaseController gives error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Extending BaseController gives error (/thread-74252.html)



Extending BaseController gives error - Ambroz - 09-03-2019

Hi.
I'm trying to use some code from CI 3 and got a error, where I can't use the provided variables to load views by template.
The error is just the site isn't loading.

This is the code I would like to use in CI 4:
PHP Code:
<?php
class GE_Controller extends CI_Controller
{
    protected 
$_viewData = [];
    protected 
$loginData '';

    
/**
    * ste footer and header
    * @param none
    * @return constructor
    **/
    
function __construct()
    {
        
parent::__construct();

        
//load header
        
$this->_viewData['message'] = $this->session->flashdata('message');
        
$this->_viewData['header'] = $this->load->view('parts/header'$this->_viewDatatrue);
    }
}

class 
MY_Controller extends GE_Controller
{
    
/**
    * ste footer and header
    * @param none
    * @return constructor
    **/
    
function __construct()
    {
        
parent::__construct();

        
$uri $_SERVER['REQUEST_URI'];
        
$s $this->session->get_userdata();

        
$ok = isset($s['user']);

        if(
$ok)
        {
            
$this->loginData $s['user'];
            
$this->_viewData['loginData'] = $this->loginData;
        }

        
$this->_viewData['footer'] = $this->load->view('parts/footer'$this->_viewDatatrue);

    }


I've used the _viewData to load views by the template.
One of my old, I mean CI 3, controllers:
PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class 
Main extends MY_Controller {

    public function 
index(){

            
$this->_viewData['logo'] = $this->load->view('logo'$this->_viewDatatrue);
            
$this->_viewData['content'] = $this->load->view('login'$this->_viewDatatrue);
            
$this->load->view('parts/template'$this->_viewData);
        }
    }


The template code:
PHP Code:
<?=$header?>


<? if(!empty($logo)):?>
<?=$logo 
?>

<?php
//show menu if any
if(isset($menu) && !empty($menu)) echo $menu?>

<?php
//show message if any
if (isset($message) && !empty($message))
    echo 
"<div class='uk-alert'>".$message."</div>";?>
<? 
if(!empty($content)): ?>

    <?=$content ;?>
<? 
endif; ?>

<?=$footer?>

I really like this method of constructing the site, so could you help me make it work?
Hope you will help me Smile . Thanks a lot.


RE: Extending BaseController gives error - jaddle - 09-26-2019

I'm just getting started with this myself, but I can say that a huge amount has changed from CI3 to CI4 - code for version 3 will rarely, if ever, work in the new version! I'm trying to update an old site, and I'm finding that basically everything has to be redone. Learning as I go!


RE: Extending BaseController gives error - InsiteFX - 09-28-2019

CI 4 comes with a BaseController that is what you would use,
think of the BaseController as the MY_Controller

Add what you need add to the BaseController then extend all your other controllers
from it.

Oh, Do Not call the parent::__construct() there is no constructor in the Controller

It uses an initController() method instead.
(


RE: Extending BaseController gives error - Ambroz - 11-09-2019

Sorry for the late reply.
I tryed using the initController in the BaseController to recreate what I had in CI3 but that doesn't work.
Is it still possible to pass a view to a variable like in CI3?
For example:
PHP Code:
$this->_viewData['header'] =  view('parts/header'$this->_viewDatatrue); 
And than echo it in a template?

[Image: c1.jpg]
[Image: c2.jpg]