Welcome Guest, Not a member yet? Register   Sign In
How to structure this code?
#1

[eluser]behnampmdg3[/eluser]
Hi I have this piece of code in all my controllers. Where is the best place to put it so I dont repeat myself?

Thanks

Code:
$this->logged = $this->session->userdata['logged_data']['logged_in'];
   if($this->logged!=TRUE)
   {
    $data['add_deal_link'] = "log_in";
    $data['log_in_link'] = base_url('log_in');
    $data['login_text'] = 'Log in';
   }
   else
   {
    $data['add_deal_link'] = "add_deal";
    $data['log_in_link'] = base_url('deals');
    $data['login_text'] = 'My Account';


And this is the whole class as an example
Code:
class Deals extends CI_Controller {

public function index()
  {
   $this->logged = $this->session->userdata['logged_data']['logged_in'];
   if($this->logged!=TRUE)
   {
    $data['add_deal_link'] = "log_in";
    $data['log_in_link'] = base_url('log_in');
    $data['login_text'] = 'Log in';
   }
   else
   {
    $data['add_deal_link'] = "add_deal";
    $data['log_in_link'] = base_url('deals');
    $data['login_text'] = 'My Account';
   }
   $this->load->view('header_view',$data);
   $this->load->view('deals_view',$data);
   $this->load->view('footer_view');
  }
}

#2

[eluser]Otemu[/eluser]
Hi,

You could use a base controller example here or you could use custom library however you probably have something like $this->mycustomlib->dosomething(); in all your controllers.
#3

[eluser]behnampmdg3[/eluser]
Hi;

Thanks for you reply. It works in some pages! In some pages I get this notice! Can you tell why? It's strange.
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: Password_forgot::$logged
Filename: libraries/Header.php
Line Number: 14
Code:
class Header
{
public $logged = '';
function index()
{
  $CI =& get_instance();
  if(isset($CI->session->userdata['logged_data']['logged_in']))
  {
   $CI->logged = $CI->session->userdata['logged_data']['logged_in'];
  }
  
  if($CI->logged!=TRUE)
  {
   $data['add_deal_link'] = "log_in";
   $data['log_in_link'] = base_url('log_in');
   $data['login_text'] = 'Log in';
   $data['logout'] = '';
  }
  else
  {
   $data['add_deal_link'] = "add_deal";
   $data['log_in_link'] = base_url('home');
   $data['login_text'] = 'My Account';
  
   $data['logout'] = '<li><a href="'.base_url('log_out').'">Log Out</a></li>';
  }
  
  $CI->load->view('header_view',$data);
}
}
#4

[eluser]Rolly1971[/eluser]
this is what i do.

in folder: application/core
create file: MY_Controller.php

in this file add:

Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{
    public $data;
    public $logged = FALSE;

    function __construct()
    {
        parent::__construct();
        $this->init();
    }

    function init()
    {
       $this->logged = $this->session->userdata['logged_data']['logged_in'];
       if($this->logged!=TRUE)
       {
            $this->data['add_deal_link'] = "log_in";
            $this->data['log_in_link'] = base_url('log_in');
            $this->data['login_text'] = 'Log in';
       }
       else
       {
            $this->data['add_deal_link'] = "add_deal";
            $this->data['log_in_link'] = base_url('deals');
            $this->data['login_text'] = 'My Account';
       }
    }
}

you can then use this 'base' controller to put in any code/functions that will be common to all your controllers.

then you just need to adjust your controllers in application/controllers to inherit from this new base controller.

example:

Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class Deals extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    }
    
    function index()
    {
        $this->load->vars($this->data);
        $this->load->view('header_view');
        $this->load->view('deals_view);
        $this->load->view('footer_view');
    }
}

hopefully this points you in the write direction




Theme © iAndrew 2016 - Forum software by © MyBB