• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extending BaseController gives error

#1
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.
Reply

#2
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!
Reply

#3
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.
(
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
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]
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.