Redirect loop

#1
[eluser]dmol[/eluser]
Hi all,
I'm new to this forum and indeed CI so I'm not sure if I can ask this question here:

I have a CI website that has a standard header,sidebar,main content,footer layout.
The sidebar is dynamically generated from a DB and apart from the main content section of the page, they are the only parts that change.So the main content is one of several views.

When I load the main page, clicking on sidebar links just append that page to the main page (after the footer) which is no use so I need to use a redirect to load the whole page from scratch with the new content page.

I'm trying to use URL helper:redirect and URI->segment to catch which view to display but the problem is that it gets stuck in a redirect loop. How would this normally be done?

pseudo code:
Controller loads
if no URI segment
{
load view -> header
load view -> sidebar
load view -> default main content view
load view -> footer
}

if URI segment
{
$foo = URI segment(2) i.e requested page
redirect('itself/display_page','refresh');
}
function display_page()
{
load view -> header
load view -> sidebar
load view -> main content: view $foo
load view -> footer
}

#2
[eluser]Basketcasesoftware[/eluser]
I have something like that in my main project, but I'm using a MY_Controller implementation. I'll post my draft code here for that. I had that same problem initially by the way. I'm planning on using wiredesignz view library so I'm writing towards that goal. For one thing, unless you are using a Microsoft server, drop the 'refresh'.
All you need to do is check to make sure that when you re-direct that you STOP redirecting when you land on the page you want to be at.
Code:
<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

/* The MX_Controller class is autoloaded as required */

class MY_Controller extends MX_Controller
{
    function __construct()
    {
        // Ok. First call our parent class's constructor.
        parent::__construct();
        
        // Is the software even installed yet?
        $this->_is_installed();
        
        // Ok. We've been installed so we need to check if the logged in user (if any) can be on this page.
        $this->_validate_user();
        
        // The user checks out, we are where are supposed to be. Now generate the basic page structure
        
        $this->load->library('view');
        
        $this->view->header='common/header';
        
        $this->_prep_page();
    }
    
    private function _is_installed()
    {
        if(!$this->config->item('installed')&&$this->uri->segment(1)!='installer')
        {
            $this->load->helper('url');
            
            redirect('installer');
        }
    }
    
    private function _validate_user()
    {
        if(!$this->config->item('installed'))
        {
            echo "We aren't even installed yet so we have no users to validate.<br />";
        }
        else
        {
            echo "Ok. We are installed. Try validating the user.<br />";
        }
    }
    
    function render_page()
    {
    }
    
    private function _prep_page()
    {
    }
    
    private function _render_html_header()
    {
    }
}

#3
[eluser]dmol[/eluser]
Thanks for the answer basketcase.
I've been trying some redirect stopping methods without success. Could you recommend a method?

#4
[eluser]Basketcasesoftware[/eluser]
First your original problem is that you seemed to have new pages append to your current page when someone clicked on a menu with links, am I right?

first of here is a function to handle your problem:
Code:
function goto_redirect($destination)
{
    $this->load->helper('url');

    if(current_url()!=$destination)
    {
        redirect($destination);
    }
}

All the function does is check to make sure the current URL (including segments) is not the same as the destination of the redirect.

But this is a bad solution for your current problem. Can you post the code you were using when you went to trying the above method? I'm only using refresh for access control stuff.

#5
[eluser]dmol[/eluser]
Yes, the original problem is that when someone clicks on a link in the menu, it calls a function in the controller which loads the appropriate view. Unfortunately this view is appended to the current page. I'm going to rebuild the controller so that is uses redirects to a function for each page. I have tried methods similar to your solution above but I think I have some scope of variables problem which maybe you can spot:

Basically I store all data into an array called $data which is not being seen by all functions.It should work but I don't know if its a PHP version issue - I'm using 4.3.9 but will upgrade to v5 soon. The controller has the following format:

CI routes.cfg sets 'main.php' as the default controller


This controller DOES NOT work as the functions cannot see $this->data;
class Main extends Controller {

var $data = 0;
function Main()
{
parent::Controller();
}
function index()
{
//Do essential startup stuff and store results in $this->data
if ($this->uri->segment(1))
{
redirect($this->uri->segment(1));
}

}
function page1() //E.g a function for each doc to display. not ideal but is temporary

{
$this->load->view("page1",$this->data);
}


The following format DOES WORK I.E $this->data is seen by all functions but since Main is not loaded on startup, none of its essential tasks are done and the page load is incomplete. This is why I want to load Main's code in index()

class Main extends Controller {

var $data = 0;
function Main()
{
parent::Controller();

//Do essential startup stuff and store results in $this->data
if ($this->uri->segment(1))
{
redirect($this->uri->segment(1));
}

} //end of main
function page1() //E.g a function for each doc to display. not ideal but temporary
{
$this->load->view("page1",$this->data);
}

I hope this is clear. Thank for your help!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.