Welcome Guest, Not a member yet? Register   Sign In
Preventing DRY in OOP
#1

[eluser]behnampmdg3[/eluser]
I notice I have this in some of my controllers. Exact same code.

How can I move this somewhere else and still be able to simply use the properties (variables).

Thanks
Code:
protected $logged;
protected $unread = 0;
protected $ad_without_photo_found = '';
protected $year = "";
public $email = "";
public function __construct()
{
     parent::__construct();
        if(isset($this->session->userdata['logged_data']['logged_in']))
   {
    if($this->session->userdata['logged_data']['logged_in']!=1 || !is_numeric($this->session->userdata['logged_data']['member_id']))
     {
      redirect(base_url('log_in'), 'refresh');
     }
    
     //Unread Messages
     $data['unread'] = $this->model_ads->unread_messages($this->session->userdata['logged_data']['member_id']);
     $this->unread = $data['unread'][0]->messages;
    
     //Check if Ad with no photo exists
     $data['have_no_photo'] = $this->model_ads->my_haves_no_photo($this->session->userdata['logged_data']['member_id']);
     $data['ad_without_photo_found'] = '';
     foreach($data['have_no_photo'] as $val => $row)
      {
       if($row->PID==0)
       {
        $this->ad_without_photo_found = '<li><a  href="'.base_url('account/index/2').'"><button type="button" class="btn btn-danger">Incomplete Ad found! Click here to resolve.</button></a></li>';
       }
      }
   }
  else
   {
    redirect(base_url('log_in'), 'refresh');
   }
  
}
#2

[eluser]ivantcholakov[/eluser]
Make a library or a model and the use it on controllers.
#3

[eluser]CroNiX[/eluser]
Use a base controller, and have all of your other controllers that need this functionality extend that base controller.
http://ellislab.com/forums/viewthread/241147/

If you need multiple base controllers (maybe one for front end, a different one for backend), then this article would help:
http://philsturgeon.co.uk/blog/2010/02/C...ing-it-DRY
#4

[eluser]behnampmdg3[/eluser]
Didn't work; I get
Quote:Fatal error: Class 'My_Logged_Class' not found
Saved this as My_Logged_Class.php in application/core
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class My_Logged_Class extends CI_Controller {


protected $logged;
protected $unread = 0;
protected $ad_without_photo_found = '';
protected $year = "";
public $email = "";
public function __construct()
{
  parent::__construct();
  if(isset($this->session->userdata['logged_data']['logged_in']))
  {
   if($this->session->userdata['logged_data']['logged_in']!=1 || !is_numeric($this->session->userdata['logged_data']['member_id']))
   {
    redirect(base_url('log_in'), 'refresh');
   }
    
   //Unread Messages
   $data['unread'] = $this->model_ads->unread_messages($this->session->userdata['logged_data']['member_id']);
   $this->unread = $data['unread'][0]->messages;
    
   //Check if Ad with no photo exists
   $data['have_no_photo'] = $this->model_ads->my_haves_no_photo($this->session->userdata['logged_data']['member_id']);
   $data['ad_without_photo_found'] = '';
   foreach($data['have_no_photo'] as $val => $row)
   {
    if($row->PID==0)
    {
     $this->ad_without_photo_found = '<li><a  href="'.base_url('account/index/2').'"><button type="button" class="btn btn-danger">Incomplete Ad found! Click here to resolve.</button></a></li>';
    }
   }
  }
  else
  {
   redirect(base_url('log_in'), 'refresh');
  }
  
}

public function index()
  {
  
  }

}
And this is my other class
Code:
class Member_details extends My_Logged_Class {


Um, not sure what to do.
#5

[eluser]behnampmdg3[/eluser]
Do I have to autoload that controller somewhere? If so how should I autoload a controller?

Thanks
#6

[eluser]CroNiX[/eluser]
No, it gets autoloaded by the framework when it calls the native class. If you follow the first link I posted exactly, it will work.

CI will load the normal object, like CI_Controller. Then it automatically checks to see if it was extended with MY_Controller. If it exists, it loads that. Then your individual controller class, like "logged_class".

You ONLY use the MY_ prefix when you extend a CI NATIVE CLASS, not your own. And it must be named exactly the same as its counterpart.

For example, you put:
Code:
class My_Logged_Class extends CI_Controller {

when it needs to be (including exact capitalization)
Code:
class MY_Controller extends CI_Controller

And then YOUR controllers extend MY_Controller instead of CI_Controller like normal (but don't use MY_ for the name of YOUR controller!!)
Code:
class Logged_Class extends MY_Controller {

See these userguide pages for more info:
http://ellislab.com/codeigniter/user-gui...asses.html
http://ellislab.com/codeigniter/user-gui...aries.html
#7

[eluser]InsiteFX[/eluser]
You can name your Base_Controller anything you want but it must be saved in ./application/core as MY_Controller.php

Saveas: ./application/core/MY_Controller.php
Code:
class Base_Controller extends CI_Controller
{

}

Then when creating new controllers extend them from Base_Controller not CI_Controller


#8

[eluser]behnampmdg3[/eluser]
Thanks.

It's working. The reason it was NOT working is that my computer strangely caches a lot of stuff!


Anyways, it is working now. Yest another question.

I still repeat some code in most controllers:

Code:
$data['page_title'] = "My Account - ";
   $data['ad_without_photo_found']=$this->ad_without_photo_found;
   $data['unread'] = $this->unread;
   $data['logged_user']=$this->logged_user;
   $data['my_haves'] = $this->my_haves;
   $data['need_membership'] = $this->need_membership;
   $data['notifications'] = $this->notifications;
   $data['show_contact_details'] = $this->show_contact_details;
   $data['update_message']='';
   $data['need_membership'] = $this->need_membership;
   $data['unread_menu_message'] = $this->unread_menu_message;

How can I move them to somewhere so in case I need to chage I won't have to change them in every single controller?

Thanks
#9

[eluser]InsiteFX[/eluser]
Move them to the Base_Controller

Code:
public static $page_data = array();

public function __construct()
{
    parent::__construct();

    $this->set_page_data();
}

public function set_page_data()
{
    // you may need to add code to get this stuff!
    if (empty($page_data)
    {
        $page_data['page_title'] = "My Account - ";
        $page_data['ad_without_photo_found']=$this->ad_without_photo_found;
        $page_data['unread'] = $this->unread;
        $page_data['logged_user']=$this->logged_user;
        $page_data['my_haves'] = $this->my_haves;
        $page_data['need_membership'] = $this->need_membership;
        $page_data['notifications'] = $this->notifications;
        $page_data['show_contact_details'] = $this->show_contact_details;
        $page_data['update_message']='';
        $page_data['need_membership'] = $this->need_membership;
        $page_data['unread_menu_message'] = $this->unread_menu_message;
    }
}

public function get_page_data()
{
    // when you retrieve the $page_data you could array_merge it with $data
    return $this->$page_data;
}
#10

[eluser]ivantcholakov[/eluser]
@InsiteFX

CodeIgniter is missing the Registry pattern implementation. :-) You have just created one.




Theme © iAndrew 2016 - Forum software by © MyBB