Search code examples
phpcodeignitercode-reusehelper

resusable function in codeigniter


Almost in all function in most of my controller i have to see to same things. i.e

1.Check if logged in. 2.Check the privilege.

I know how to do it. But what is the best way, so that i don't have to re-write the same code everywhere? Is it to make a helper or something? I'm a bit new to codeigniter, all i know is basic php!


Solution

  • If all the methods in your controller need the same code, you can put that authentification code in the constructor.

    Otherwise, creating a helper is probably the best way, but you'll still have to repeat the function in each method subject to authentification.