Welcome Guest, Not a member yet? Register   Sign In
Redundant Code
#1

[eluser]Petrusilius[/eluser]
Hey guys,

i'm currently working on a browsergame (more a hobby) using CI.

In several controllers i'm currently using redundant code and i'm wondering if there is a smarter solution for that.

So what have i done:
I have overloaded every __construct() function of my controllers except the login controller for a routine checking the session data and implemented the _is_logged_in() function in every controller.

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

    function _is_logged_in() {
        $is_logged_in = $this->session->userdata('is_logged_in');
        if (!isset($is_logged_in) || $is_logged_in != true) {
            redirect('login');
            die();
        }
    }

Another example would be the function _generic_information:

Code:
function _generic_information($game_id) {
        $data['game_information'] = $this->game_model->get_information($game_id);
        $data['player_information'] = $this->game_user_model->get_own_information($game_id);
        $data['city_information'] = $this->game_user_city_model->get_own_cities($game_id);
        return $data;
    }

The game itself is separated in several controllers. Every controller is implementing this function and calling it in nearly every other function.

Is there a possibility to implement those functions a bit more global. I need access to the session data and to the database models from there.

Sry for my bad english and thank you for reading.

#2

[eluser]Eduard Stankovic[/eluser]
try to use helpers http://ellislab.com/codeigniter/user-gui...lpers.html

and declare this is_logged_in function there ( do not forget to load helper in autoload.php)

or just extend MY_Controller http://ellislab.com/codeigniter/user-gui...asses.html



and i recommend to use exit; instead of die; Smile


PS: if you decide to use helpers, you can access CI instance like that
Code:
$CI =& get_instance();

$CI->session->........
#3

[eluser]Petrusilius[/eluser]
Thx i will try the helper way.
Where is the difference between exit and die?
#4

[eluser]Petrusilius[/eluser]
Doh, forget it ^^
#5

[eluser]Eduard Stankovic[/eluser]
there is actually no difference die is alias to exit.

It is just "cosmetic" issue. CI uses exit in their code so It would make your code more uniform if you use exit.

Sorry for my English Big Grin
#6

[eluser]Petrusilius[/eluser]
It worked for logged_in! Smile

But i guess, i cannot use helper methods for getting information from existing database models or? (_get_informations function).

I think i have to get the CI Instance and using it to get the information i need from the database. But i think i have to break with mvc then or? :/
#7

[eluser]Eduard Stankovic[/eluser]
for logged_in function is better to use helpers because it can be used somewhere else in code (e.g. in template).

For get_info func I recommend use MY_Controller ... http://ellislab.com/codeigniter/user-gui...asses.html This will not break MVC pattern.





Theme © iAndrew 2016 - Forum software by © MyBB