Welcome Guest, Not a member yet? Register   Sign In
SOLVED; auth using my_controller
#1

[eluser]Slowcheetah[/eluser]
Hi,

i'm fairly new with extending classes. Now i'm a bit experimenting with this, i have a question.

I want to protect functions with a simple command.. something like the code below.

Code:
class Welcome extends MY_Controller {
    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        // Set function protection
        $this -> auth['user_protected'] = true; // only accesible for authenticated users.
        $this -> auth['admin_protected'] = true; // only accesible for authenticated admins.
    }
}

In my extended controller i have the following code.

Code:
class MY_Controller extends CI_Controller {
    function __construct()
    {
        parent::__construct();

        if ($this -> auth['user_protected'] === true)
        {
            // Do user authentication
        }

        if ($this -> auth['admin_protected'] === true)
        {
            // Do admin authentication                    
        }        
    }
}

It gives the following error;

Code:
Message: Undefined property: Welcome::$auth
Filename: core/MY_Controller.php
Line Number: 10

What i'm doing wrong?
#2

[eluser]4ever[/eluser]
I guess you are trying to access property which does not exist.

write something like

Code:
class Welcome extends MY_Controller {
public static $auth;

Or you should to reference the previous controller property but I know know how to do this
#3

[eluser]danmontgomery[/eluser]
You're trying to access those variables before they're ever defined. Your code is executed as:

- Welcome object instantiated
- Welcome::__construct() called
- MY_Controller::__construct() called via parent::__construct() ( variables checked )
- Welcome::index() called ( variables created )

If you want to set access control in the method, you would need to run the check after the method has executed. You could do this with a post_controller hook, or define the method access in the constructor of Welcome, before parent::__construct() is called.
#4

[eluser]Slowcheetah[/eluser]
@noctrum Thanks! i have it working now. I never worked with the hooks before this. Looks promising.




Theme © iAndrew 2016 - Forum software by © MyBB