Short functions from a Codeigniter's library Short functions from a Codeigniter's library codeigniter codeigniter

Short functions from a Codeigniter's library


Head into the tank_auth library and define a new public function:

public function logged(){    return $this->is_logged_in();}

You can now access it with $this->tank_auth->logged();

If you want to shorten the name of tank_auth, you'll have to rename the class and the filename.

UPDATE:

The more important question is, why are you calling this so many times that it is becoming an annoyance? You should only have to write it once if your code follows the Don't Repeat Yourself (DRY) principle.

Have a look at Phil Sturgeon's blog post entitled Keeping It DRY. He will show you how to write a base controller that all your controllers will inherit from. If you write the login check in the constructor of the base controller, you don't have to write it in every controller.


I would argue against doing this as the method logged() in your instance lacks context. However, if you wanted to do this, you could write a base controller which has a logged() method which ends up returning $this->tank_auth->is_logged_in(). All controllers would inherit from this base controller, which isn't a bad idea to begin with.

If you're using libraries, you could implement a similar pattern in them.


You probably don't want to edit anything in the TankAuth library if you didn't create it as doing so affects the updatability of the library. Instead, you might add a method to your controller called logged and have it reach out to Tank Auth. Although, I would choose a better name for your function as pointed out by a previous answer.

Create or edit your controller base class to have something like this:

function is_logged() {   return $this->tank_auth->is_logged_in();}

Then you may call it like so: $this->is_logged();