Welcome Guest, Not a member yet? Register   Sign In
shorter way for loading models and views
#1

[eluser]Reynolds[/eluser]
Guys,

just want to ask, what do you think of this approach?


I've made two helpers for loading models and views

-- model_helper.php
Code:
function model($model_name, $model_alias = "",$auto_connect = FALSE)
{
    $ci = get_instance();
    $ci->load->model( $model_name .DEFAULT_MODEL_SUFFIX, $model_alias , $auto_connect);
}

-- view_helper.php
Code:
function view($data = "", $view = "")
{
    $page_view = "";
    
    if(!empty($view))
    {
        $page_view =  "_" . $view;
    }
    //$params['data'] = $data;
    $ci = get_instance();
    $ci->load->view( strtolower( get_class($ci) ) . DEFAULT_VIEW_SUFFIX . $page_view, $data );
}

this way I can load models and views like this
Code:
function index()
    {
        model('query_analyzer','qa');
        
        $data = array(
        'first' => $this->qa->show_process_list()->result(),
        'second' => 2
        );
        
        view($data);
    }

Generally the advantage of this is that I need no more type $this->load->*

The dis-advantage however is that it doesn't look purely OOP anymore.
I'm thinking this makes my controller methods less in size and more friendly (personally).

I'd like to do this as well with $this->load->library().

Criticism is highly appreciated, thanks Wink
#2

[eluser]skunkbad[/eluser]
Code:
if('$this->load' == $typing_overload)
{
     $this->lazy = 'you';
}

Sorry, it was just too funny when I thought of it.
#3

[eluser]Reynolds[/eluser]
[quote author="skunkbad" date="1250766140"]
Code:
if('$this->load' == $typing_overload)
{
     $this->lazy = 'you';
}

Sorry, it was just too funny when I thought of it.[/quote]


Hehehe, I don't mind at all. and um... you got me there, I just got(being) lazy at all Tongue hahahaha
#4

[eluser]alboyd[/eluser]
[quote author="skunkbad" date="1250766140"]
Code:
if('$this->load' == $typing_overload)
{
     $this->lazy = 'you';
}

Sorry, it was just too funny when I thought of it.[/quote]

That's GOLD Smile I thought the same thing but couldn't put my finger on what it was I was thinking when I read the OP...

OP - it's OK - the view loader is obviously a little restrictive on the naming - but it's OK... If you are anything like me though - you have to consider that "$this->" is typed before you can count to 1 and $this->load doesn't take much more - it rolls off the fingers after typing it that many times huh Smile
#5

[eluser]bretticus[/eluser]
Man, I am a walking paradox. I am a programmer who can't type well (I am pretty fast for looking at the keyboard and using all the wrong fingers. Smile lots of mistakes though.) Hence I love anything that is a shortcut (I wish someone would roll out a CI plugin for netbeans: create controller/model/view autocomplete for $this->load->helper/model/library. Zend Studio had custom little snippets for that. Maybe netbeans does too. Better look.) So I'm all for shortening of code to type!




Theme © iAndrew 2016 - Forum software by © MyBB