2

I got a Tank Auth library installed in my Codeigniter package, the problem is that I don't like how to is_logged_in functions need to be called because it's simply long and not so friendly, as I need to use:

$this->tank_auth->is_logged_in()

Everytime I want to check if user is logged in... So is there a way to make it shorter? By saying shorter I mean something like $this->logged();?

I would really appreciate if someone could help me.

Thank you.

Charles
  • 50,943
  • 13
  • 104
  • 142
Sapp
  • 624
  • 2
  • 7
  • 13

3 Answers3

2

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.

Jordan Arsenault
  • 7,100
  • 8
  • 53
  • 96
  • I think they mean to omit the `->tank_auth` part. – Matthew Sep 10 '12 at 22:40
  • Yeah, I know that I can use another function in my controllers but I would rather be interested in something more global, as I would like to do the same with some other tank auth functions. – Sapp Sep 10 '12 at 22:45
0

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.

Matthew
  • 24,703
  • 9
  • 76
  • 110
0

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();

Jesse Bunch
  • 6,651
  • 4
  • 36
  • 59