Welcome Guest, Not a member yet? Register   Sign In
How to use a function from a other controller?
#1

[eluser]Slowcheetah[/eluser]
I relatively new to OOP programming, and not understand how to implement the following scenario..

I have a 'myaccount' controller with a private function '_check_credidentials'.

Normaly, in the 'myaccount' controller i would call something like '$this->_check_credidentials($username,$password)'.

Now i want to use the '_check_credidentials' function in any other controller. How do i call my '_check_credidentials' function in other controllers without copy/pasting this function to each controller? What method to use?
#2

[eluser]Colin Williams[/eluser]
If you find yourself needing to call another controller's function, you've probably not built your controllers correctly. The function that needs calling typically is better suited as a model function. When you are checking credentials, you are essentially asking either, "What is the current state of the user?" or "Do these credentials match a user's?" Models represent the "state" of your application, i.e. resource data.

Short answer is: Put it in the model where it belongs.
#3

[eluser]Slowcheetah[/eluser]
Okay, i will be more specific.

p.s. i removed all unnecessary code.

Check credentials function in the myaccount controller
Code:
/**
     * Check credentials for a page
     *
     * @access private
     * @version 0.1
     * @param $username, $password, $error_page- string
     * @return Bool - TRUE or redirect to error page
     */

    function _check_credentials($username, $password, $error_page) {
        // check variables
        if ($username <> "" && $password <> "") {
            // check if account is signed-in
            if ($this->accounts_model->check_credentials($username, $password) == FALSE) {
                redirect($error_page);
                exit;
            } else {
                return TRUE;
            }
        } else {
            redirect($error_page);
            exit;
        }
    }

check credidentials function in account model
Code:
/**
     * function check_credidentials()
     *
     * Checks if account credentials are valid
     * @version 0.1
     * @param $username, $password - string
     * @return Bool - TRUE or FALSE
     */

    function check_credentials($username, $password) {
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $this->db->where('verified', 1);
        $query = $this->db->get('accounts', 1);
        if ($query->num_rows() == 1) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

Function where i check the credentials in the myaccount controller
Code:
/**
     * function details()
     *
     * retrieve account details
     * @access public
     * @version 0.1
     */

    function details() {
        // Set if page is secured
        $secured_page = TRUE;
        // Set permitted role
        $permitted_role = 100; // 100 = user
        // check credentials
        if ($secured_page == TRUE) {
            // place variables
            $username = $this->session->userdata('username');
            $password = $this->session->userdata('password');
            // check credentials in database
            if ($this->_check_credentials($username, $password, $error_page) == TRUE) {
                            echo "do this";
            } else {
                            echo "do that";
                    }
        }
    }
#4

[eluser]Colin Williams[/eluser]
All of the logic in the controller function _check_credentials can be served by the model function check credentials. All you really need to do is have the model method accept a third parameter for $error_page.
#5

[eluser]Slowcheetah[/eluser]
Okay, i got your point.. but is a model not supposed to do ONLY database transactions and a controller ONLY the logics?

Am i breaking the OOP fundamentals with your solution? Or i'm thinking to strict or misinterpreting OOP or even plain stupid? As i said, i'm relatively new..
#6

[eluser]Colin Williams[/eluser]
Models manage your resources. Could be a database. Could be a web service. Could be a filesystem. They aren't database query monkeys. Nothing about the logic in your controller function is bad for the model. Sure, some may get picky about the model doing the redirect, but remember that the controller supplies the redirect instruction to the model. The model is simply telling the controller to redirect / redirecting on the controllers behalf. The logic that models don't do is application or business logic. For instance, models shouldn't route or call views or sanitize global values or do the validation process, etc.

And I can't even fathom what OOP fundamental you think you might be breaking.
#7

[eluser]Rick Jolly[/eluser]
I don't usually disagree with Colin, and this is largely personal preference, but I don't think models should redirect. Models should be as generic and implementation independent as possible. It's also more transparent to have the redirect in the controller.

I think your code could be simplified:
Code:
function details() {
        // $secured_page = TRUE;
        // where is $permitted_role used?
        // $permitted_role = 100;
        // you know this is secure. You just set it above
        // if ($secured_page == TRUE) {
            // place variables

            // you can set these as class variables in the constructor instead of every method
            //$username = $this->session->userdata('username');
            //$password = $this->session->userdata('password');
            // check credentials in database
            if ($this->your_model->check_credentials($this->username, $this->password)) {
                            echo "do this";
            } else {
                            // redirect to $error_page
                    }
        }
    }

Edit: If you'd like another way of securing controllers, you could look at controller inheritance. You could have a parent controller do all the checks and redirect if necessary all before your child controller executes. That way you don't have to handle the repetition in each of your controllers/methods.
#8

[eluser]Slowcheetah[/eluser]
@Colin; Thanks for your explanation, it helps me to get a grasp on this.

@Rick; Thanks for your points, answers;

Where is $permitted_role used?
I use this to have role-based security. The sample i provided was stripped and not included in this piece of code.
You know this is secure. You just set it above
That's correct. Thanks for bringing it up, i was thinking to complex in this case.
You can set these as class variables in the constructor instead of every method
Got it! But in this controller sometimes the username and password strings not come from the session (forms for example).
#9

[eluser]Colin Williams[/eluser]
Ha! Knew people wouldn't like it. I have a very similar function in the user model I always use and I did struggle with it for awhile. Eventually I convinced myself that it's really only redirecting because the Controller tells it to. It's still always in the Controller's control. I convinced myself, but might not work for anyone else Smile




Theme © iAndrew 2016 - Forum software by © MyBB