Welcome Guest, Not a member yet? Register   Sign In
Base Controller Redirect
#1

[eluser]Kyle Ellman[/eluser]
I'm trying to us a base controller (MY_Controller) to check if someone is logged in or not. I want the MY_Controller's constructor to perform a redirect() function, but the code breaks each time. If I place this redirect in the constructor of a normal controller that extends the MY_Controller, it works just fine.

Can anyone tell me how to allow a redirect() in the base controller?
#2

[eluser]danmontgomery[/eluser]
There's no reason you can't use redirect() in the constructor. Can you give more detail than "the code breaks"?
#3

[eluser]Kyle Ellman[/eluser]
Well, often times, when there is an error in php, nothing shows up on the browser. That is what is happening here. I can put redirect(''); in the normal controller's constructor and it works and I am redirected and the proper view is output, but as soon as I move redirect(''); to the base controller's constructor, nothing it output to the browser
#4

[eluser]mddd[/eluser]
Where are you redirecting? If you redirect to another CodeIgniter page in your site, that won't work because every Controller will try to direct. So you get an infinite loop of redirects. In most cases, some pages (like your home page) should NOT be redirected. Otherwise, how are you going to let people log in?
#5

[eluser]theshiftexchange[/eluser]
I'm assuming this is for an Auth software which allows you to always test if a user is logged in?

If so - you want all your controllers to use MY_Controller EXCEPT for your first controller (such as "Auth") because you will manually handle the login there
#6

[eluser]Kyle Ellman[/eluser]
The default controller (located at the root) extends controller rather than my_controller. I'm doing this so that only the default controller is accessible if the user is not logged in.

The problem still remains that the redirect doesn't work when placed in the base controller, and I can't figure out why.
#7

[eluser]mddd[/eluser]
How exactly are you extending the Controller class? The contructor of the controller class calls several objects and loads libraries, etc.
If you are trying to use libraries that are not yet available, that will present problems.
Have you called the parent contructor before using any classes from CI?
#8

[eluser]n0xie[/eluser]
What is the error you get?
#9

[eluser]Kyle Ellman[/eluser]
Here is the base controller my_controller.php
Code:
<?php

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


And here is the controller that extends MY_Controller, media.php
Code:
<?php

class Media extends MY_controller {

    function __construct()
    {
        parent::__construct();
        
        $this->load->helper('form');
        $this->load->helper('url');
    }
    
    function index()
    {
        $this->load->view('welcome');
    }
    
    function show()
    {
        $this->load->view('media_list');
    }
    
    function add()
    {
        $this->load->view('media_add');
    }
}

If I uncomment the redirect(''); line, I don't get an actual error, just the blank browser screen you get as a result of syntax errors, but I'm pretty sure that it's not a syntax error.

NOTE: It's always redirecting for now because I haven't coded the authentication portions yet.
#10

[eluser]n0xie[/eluser]
You probably get an error because you are loading the helper 'url' in your subclass. The subclass first calls the parent constructor, where it finds the redirect command. Since the url helper isn't loaded yet, it will error out since it has no idea what the command 'redirect' is.

You should first make sure you have error reporting on. You could autoload the url helper to make sure the helper is loaded before the controllers are executed.




Theme © iAndrew 2016 - Forum software by © MyBB