Vitaly
Vitaly

Reputation: 4498

CodeIgniter: routing & authentication

The idea is to catch any access to controller's functions and, if we are authenticated, rout as normal and, if not, show the login form.

The question is, is _remap function the best place to check for access to controller's functions and how to pass routing back to CI in case we are authenticated?

Upvotes: 0

Views: 3244

Answers (2)

Dau
Dau

Reputation: 8858

make a library called Authentication and check about your method in this

you can get method and class name by this

$class = $this->CI->router->class;
$method = $this->CI->router->method;

and to check this authentication each time you have enabled the hooks from your config file, attach a post_controller_constructor hook to check authentication each time.

Upvotes: 1

No Results Found
No Results Found

Reputation: 102745

_remap isn't necessary for this. You could use it, but you don't need to.

Check for access in the __construct() method of the controller. You can get the current method via $this->router->fetch_method() and authenticate against that.

Better yet, have all your controller that need this extend a base controller (aka "MY_Controller"). You can write an Auth_Controller and do the auth check in the __construct() there. You can get the current class via $this->router->fetch_class(), as well as the method, just make sure your controllers that need this extend Auth_Controller instead of the usual CI_Controller.

If they shouldn't have access, just redirect them where they need to go or show an error.

Upvotes: 1

Related Questions