Welcome Guest, Not a member yet? Register   Sign In
Extend Custom Controller?
#1

[eluser]RandyCram[/eluser]
I am working on a rather large project and It is going to have admin/moderators/members and what not so I want to create a controller (ex: admin_controller.php) that I can extend to do the checking if person is admin or not. I know that to load controllers you need to autoload them or something like that but i did a require_once in the MY_Controller.php but no matter how i get it to work, the actual controller that does the loading of view's etc.. basically ignores the rules set in the admin_controller.php.

Any thoughts??
#2

[eluser]n0xie[/eluser]
Take a look at Phil Sturgeon's article about base classes
#3

[eluser]RandyCram[/eluser]
I tried that however It still doesn't seem to work. I am using an auth library to check for user and It seem's to just ignore it. I have the library autoloaded, even if I try loading it inside the new controller it still doesn't seem to work..
#4

[eluser]InsiteFX[/eluser]
n0xie is right about Phil's base controller classes!

So, What Auth library are you using?

Post code to your MY_Controller and extended Controllers Code in code tags!

Then maybe we can help you out...

InsiteFX
#5

[eluser]RandyCram[/eluser]
I was more of looking for just general thoughts i could try before I posted the code.

MY_Controller.php
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

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

?>

Admin_Controller.php
Code:
<?php

class Admin_Controller extends MY_Controller {
    
    function __construct() {
        parent::__construct();
        
        if (!$this->ion_auth->is_admin()){
            $this->session->set_flashdata('message', 'You must be an admin to view this page');
            redirect('welcome/index');
        }

    }
}

?>

Controllers/Admin.php
Code:
<?php if (! defined('BASEPATH')) exit('No direct script access');

class Admin extends Admin_Controller {
    
    function __construct() {
        parent::Controller();
    }
    
    function index() {
        
        $this->load->view('admin/dashboard');

    }

}
#6

[eluser]cahva[/eluser]
Your mistake is the constructor.

Code:
class Admin extends Admin_Controller {
    
    function __construct() {
        parent::Controller();
    }

parent::Controller() is wrong as you will need to use parent::Admin_Controller(). Better yet, use PHP 5 style(as you are already doing with the __construct()
Code:
class Admin extends Admin_Controller {
    
    function __construct() {
        parent::__construct();
    }

And by the way, using MY_Controller is not needed at all. Just create your base controllers that extend Controller. So get rid of MY_Controller and just create Admin_Controller like this:
Code:
class Admin_Controller extends Controller {
    
    function __construct() {
        parent::__construct();
        
        if (!$this->ion_auth->is_admin()){
            $this->session->set_flashdata('message', 'You must be an admin to view this page');
            redirect('welcome/index');
        }

    }
}
#7

[eluser]InsiteFX[/eluser]
lol, cahva was writing post when you posted!

Code:
<?php if (! defined('BASEPATH')) exit('No direct script access');

class Admin extends Admin_Controller {
    
    function __construct() {
        // parent::Controller();
        parent::__construct();
    }
    
    function index() {        
        $this->load->view('admin/dashboard');
    }
}

InsiteFX
#8

[eluser]techgnome[/eluser]
You're not loading the ion_auth library... you need to load it before you can use it (edit - never mind, I see where you mention that it's autoloaded).

Your setup doesn't make sense to me either.

You have a My_Controller ... which extends Controller, but doesn't do anything... and then you have an Admin_controller, which checks to see if the user is admin (or more to the point NOT admin)... and if they aren't then it redirects to the welcome page.... but... what if they ARE admin? It doesn't do anything. I take that back, I see what you tried to do... I'm guessing that even when a Non-admin hits the page, they still get the admin/dashboardview, huh?


In your __construct methods... most notably the one in your final Admin.php file there.... try changing parent:: Controller(); to parent::__construct(); -- I'm wondering if it's even getting into the constructor at all in your parent class.

-tg

EDIt - GAH! That's what I get for pausing to have a conversation while posting...
#9

[eluser]RandyCram[/eluser]
The MY_Controller was because I was using the link that n0xie posted which used it. As for the if they are admin or not it redirects them to the welcome if they are not admin and if they are admin it loads the requested page ie: admin/dashboard or admin/settings.

Thanks to all for your help. First time I have tried to use custom controllers in this way.

I don't fully understand how it does that, confuses myself but that is how the user_guide for IonAuth says to do it and it seems to work so.




Theme © iAndrew 2016 - Forum software by © MyBB